Skip to content

Instantly share code, notes, and snippets.

@mmalecki
Created April 23, 2011 10:40
Show Gist options
  • Save mmalecki/938537 to your computer and use it in GitHub Desktop.
Save mmalecki/938537 to your computer and use it in GitHub Desktop.
4chan thread's images downloader (downloads each image from given thread, invocation like: php 4ctd.php thread_url dir_to_save_images)
<?php
$url = $argv[1];
$prefix = (isset($argv[2])) ? $argv[2] : '';
$fprefix = (isset($argv[3])) ? $argv[3] : '';
$f = file_get_contents($url) or die('URL invalid');
if (!is_dir($prefix))
mkdir($prefix);
$img = array();
preg_match_all('|http://images.4chan.org/[a-z]+/src/([0-9]+\..{0,3})|', $f, $img);
$img[0] = array_unique($img[0]);
$img[1] = array_unique($img[1]);
echo 'Found ' . count($img[0]) . " images.\n";
foreach ($img[0] as $key => $url) {
$path = $prefix . '/' . $fprefix . $img[1][$key];
if (!file_exists($path)) {
echo "Geting {$img[1][$key]}...";
echo ($f = @file_get_contents($url)) ? ' Success.' : 'Failure.', "\n";
file_put_contents($path, $f);
}
else
echo "Ommiting {$img[1][$key]} as it exists.\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment