Created
November 22, 2014 02:27
-
-
Save appnus/31c36ef8f6f7ee31e382 to your computer and use it in GitHub Desktop.
4chan Image Downloader
This file contains hidden or 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
#!/usr/bin/env php | |
<?php error_reporting(E_ALL); set_time_limit(0); | |
// ref: https://github.com/4chan/4chan-API | |
if (php_sapi_name() !== "cli" || $argc !== 2) { | |
echo "Usage: php ", $argv[0], " [board]"; | |
exit(1); | |
} | |
$aurl = "http://a.4cdn.org"; | |
$exts = array(".jpg", ".jpeg", ".png", ".gif", ".bmp"); | |
$api = file_get_contents("{$aurl}/{$argv[1]}/catalog.json"); | |
$json = json_decode($api); | |
if (!is_dir($bin = "images")) { | |
mkdir($bin, 0755); | |
} | |
foreach ($json as $page) { | |
foreach ($page->threads as $thread) { | |
$api2 = file_get_contents("{$aurl}/{$argv[1]}/thread/{$thread->no}.json"); | |
$json2 = json_decode($api2); | |
foreach ($json2->posts as $post) { | |
if (isset($post->tim) && isset($post->ext)) { | |
if (in_array($post->ext, $exts)) { | |
$fn = $post->tim.$post->ext; | |
download("http://i.4cdn.org/{$argv[1]}/{$fn}", "{$bin}/{$fn}"); | |
} | |
} | |
} | |
} | |
} | |
function download ($url, $fn) { | |
if (!file_exists($fn)) { | |
echo "Downloading: ", $url, PHP_EOL; | |
file_put_contents($fn, file_get_contents($url)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment