-
Notifications
You must be signed in to change notification settings - Fork 1
/
reddit_wall.php
executable file
·50 lines (46 loc) · 1.54 KB
/
reddit_wall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
error_reporting(0);
$i = 0;
while($i < 1) { //infinate loop
$daysago = date("Y-m-d", mktime(0,0,0, date("m"), date("d")-2, date("Y")));
$localdirectory = "/Users/USERNAME/Pictures/Backgrounds/reddit/"; //CHANGE THIS!!!
$alreadyexist = array();
if ($handle = opendir($localdirectory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$datemod = date("Y-m-d", filemtime($localdirectory.$file));
$alreadyexist[] = $file;
if($datemod == $daysago) {
echo "Deleting Old File - ".$file."\n";
unlink($localdirectory.$file);
};
}
}
closedir($handle);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://www.reddit.com/r/wallpapers/top/.json');
$result = curl_exec($ch);
$json_o=json_decode($result);
$images = array("png", "jpg");
foreach($json_o->data->children as $child)
{
if(in_array(substr($child->data->url, -3, 3),$images)) {
$fullfilename = $child->data->name.".".substr($child->data->url, -3, 3);
if(in_array($fullfilename,$alreadyexist)) {
echo "We already have ".$child->data->name.".".substr($child->data->url, -3, 3)."\n";
} else {
echo "Downloading - ".$fullfilename."\n";
$stringData = file_get_contents($child->data->url);
$myFile = $localdirectory.$fullfilename;
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
};
};
};
echo "Sleeping 1 hour\n";
sleep(3600);
};
?>