Skip to content

Instantly share code, notes, and snippets.

@petrkle
Created September 12, 2012 12:11
Show Gist options
  • Save petrkle/3706215 to your computer and use it in GitHub Desktop.
Save petrkle/3706215 to your computer and use it in GitHub Desktop.
Simple tumblr image downloader.
<?php
define('IMGDIR','./img');
require('sites.php');
foreach($sites as $site){
$rss = get_rss('http://'.$site.'.tumblr.com/rss');
if(count($rss)>0){
if(!is_dir(IMGDIR.'/'.$site)){
mkdir(IMGDIR.'/'.$site,0700,true);
}
foreach($rss as $item){
if(preg_match('/\.jpg$/',$item['img'])){
$fn = IMGDIR.'/'.$site.'/'.$item['date'].'.jpg';
if(!is_file($fn)){
print "$fn\n";
$fp = fopen($fn,'w');
fwrite($fp,file_get_contents($item['img']));
fclose($fp);
}
}
}
}
}
function get_rss($url){
print "$url\n";
$entries = array();
$dom = new DOMDocument();
@$dom->loadXML(file_get_contents($url));
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//item");
foreach ($nodes as $node) {
$subdom = new DOMDocument();
@$subdom->loadXML('<desc>'.html_entity_decode($node->nodeValue).'</desc>');
$subxpath = new DOMXPath($subdom);
if(is_object($subxpath->query("//desc/img")->item(0))){
$img = $subxpath->query("//desc/img")->item(0)->getAttribute('src');
$guid = $xpath->query(".//guid",$node)->item(0)->nodeValue;
$date = strtotime($xpath->query(".//pubDate",$node)->item(0)->nodeValue);
array_push($entries,array('img'=>$img,'guid'=>$guid,'date'=>$date));
}
}
return $entries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment