[PHP/OOP] 4chan Image downloader
If you're not a regular visitor of 4chan, this'll probably of no use to you. But if you are and you get sick of manually downloading all the images posted in a thread, then this is for you!
This script searches the source of a thread for image links and proceeds to download them to your server.
The script itself is quite efficient, but due to the fact that it needs to keep running till all the images are saved, it may take up to 10 minutes for the script to finish.
The reason why I wrote the script in OOP is because the class is part of an entire 4chan archive script, which I have yet to finish.
Anyway, the script. Put the script anywhere on your web server, call it "4chan_images.php" then create a directory called "images" (This can be changed). Then run the script and everything should be self-explanatory.
PHP Code:
<?php
class image_downloader {
public $url;
protected function get_source() {
if(($source = @file_get_contents($this->url)) == false) {
die("Could not open thread.");
}
return $source;
}
protected function get_file_link() {
$source = $this->get_source();
preg_match_all("/File :(.*?)\<blockquote\>/is", $source, $matches_image);
while($i < count($matches_image[1])) {
preg_match("/\<a href\=\\\"(.*?)\\\" target\=/is", $matches_image[0][$i], $images_array[$i]);
$i ++;
}
return $images_array;
}
public function save_file() {
$images_array = $this->get_file_link();
$i = 1;
$errors = 0;
while($i < count($images_array)) {
$extension = preg_replace("/.*\./is", "", $images_array[$i][1]);
if(($handle_external = fopen($images_array[$i][1], "r"))) {
while($line = fread($handle_external, 8192)) {
$file .= $line;
}
//Change the images between parenthesis to change the diretory, don't forget the "/".
$handle_internal = fopen("http://forum.ragezone.com/images/".rand(0000000000001,9999999999999).".$extension", "w");
fclose($handle_external);
fwrite($handle_internal, $file);
fclose($handle_internal);
unset($handle_external, $line, $file, $handle_internal);
} else {
$errors ++;
}
$i ++;
}
echo $errors . " image(s) not saved.<br/><br/><a href='4chan_images.php'><<back</a>";
}
}
set_time_limit(0);
//Remove the line below if you run PHP in safe-mode.
ini_set("max_execution_time", "0");
if(!isset($_POST['submit'])) {
?>
<form name="link" method="post" action="4chan_images.php">
Link to thread:<br/>
<input type="text" size="55" name="link" />
<input type="submit" name="submit" />
</form>
<?php
} else {
$downloader = new image_downloader();
$downloader -> url = $_POST['link'];
$downloader -> save_file();
}
?>
Note: If you run this script in safe-mode, it'll time-out on large images.
EDIT: Why the hell does ragezone automatically change "images" to "http://forum.ragezone.com/images"?
Re: [PHP/OOP] 4chan Image downloader
I guess it changes to {..}.com/images/ for the layouts or something :P:
Further;
Nice script =]
What does OOP stand for ?
Re: [PHP/OOP] 4chan Image downloader
Quote:
Originally Posted by
EliteGM
I guess it changes to {..}.com/images/ for the layouts or something :P:
Further;
Nice script =]
What does OOP stand for ?
Object-oriented programming, i'm going to guess
Re: [PHP/OOP] 4chan Image downloader
Yes, OOP stands for object oriented programming.
Re: [PHP/OOP] 4chan Image downloader
Ah, that's classes etc. right?