Created
September 2, 2014 10:34
-
-
Save druu/41e9543ce96ced5a69f1 to your computer and use it in GitHub Desktop.
Imgur Image extractor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ImgurHook extends Hook { | |
protected $dependencies = array('phpQuery'); | |
protected static $re_frontpage_posts = '~class="post".+?src="//([^"]+?)" title="([^<"]+?)[<"]~misu'; | |
protected static $re_single_post = array( | |
'image' => '~<img src="//([^"]+?)" alt~misu', | |
'title' => "~id='image-title'>(.+?)</h2>~misu" | |
); | |
public function run($args, $options) { | |
@list($method, $args) = explode(' ', $args, 2); | |
$method = strlen($method) ? $method: 'frontpage'; | |
if (is_callable(array('self', $method))) { | |
$matches = self::fetch_images($args); | |
return call_user_func_array(array('self', $method), array($matches)); | |
} | |
else { | |
throw new Exception('Unrecognised method: "' . $method . '()"'); | |
} | |
} | |
protected static function imagemime($ext) { | |
switch (strtolower($ext)) { | |
case 'gif': | |
return 'image/gif'; break; | |
case 'png': | |
return 'image/png'; break; | |
case 'jpg': | |
case 'jpeg': | |
default: | |
return 'image/jpeg'; | |
} | |
} | |
protected static function frontpage( $matches ) { | |
$ct = 0; | |
$output = ''; | |
foreach ($matches[1] as $index => $thumbnail) { | |
$output .= self::render_image($thumbnail, $matches[2][$index]); | |
if($ct++ > 3) break; | |
} | |
return $output; | |
} | |
protected static function fetch_images ($id = null) { | |
$url = "http://www.imgur.com" . (is_null($id) ? '' : '/' . $id); | |
$html = file_get_contents($url); | |
if (!$html) { | |
throw new Exception("Failed to fetch Imgur Homepage"); | |
} | |
$matches = array(array()); | |
if(is_null($id)) { | |
preg_match_all(self::$re_frontpage_posts, $html, $matches); | |
} | |
else { | |
preg_match(self::$re_single_post['image'], $html, $image); | |
preg_match(self::$re_single_post['title'], $html, $title); | |
$matches[1][0] = $image[1]; | |
$matches[2][0] = $title[1]; | |
} | |
if (!count($matches[1]) || !count($matches[2])) { | |
throw new Exception("Failed to fetch Images"); | |
} | |
return $matches; | |
} | |
protected static function render_image($thumbnail, $title) { | |
$rev = strrev($thumbnail); | |
$url = 'http://' . strrev(str_replace('.b', '.', $rev)); | |
$ext = strrev(preg_replace('~\..*~', '', $rev)); | |
$output = '<h1>' . html_entity_decode($title) .'</h1>'; | |
$output .= '<img src="data:' . self::imagemime($ext) . ";base64," . base64_encode(file_get_contents($url)) . '" />'; | |
return $output; | |
} | |
protected static function image($matches) { | |
return self::render_image($matches[1][0] ,$matches[2][0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello good sir,
Can you tell me how to use this code? I really need an Imgur image extractor. Thanks!