#!/usr/bin/perl -w # Copyright © 2006 Jeremy English # Script to pull images out of flickr's rss 2.0 files. # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that # the above copyright notice appear in all copies and that both that # copyright notice and this permission notice appear in supporting # documentation. No representations are made about the suitability of this # software for any purpose. It is provided "as is" without express or # implied warranty. # # Created: 12-January-2006 use strict; my $progname =$0; $progname =~ s@.*/@@g; my $verbose = 0; #Pull feeds listed #Pull the image source tags #If the image dosen't exist in the destination folder then download it sub pull_pixs { my ($url) = @_; my $expirey = 30; #only download every 30 minutes; my $now = time; $_ = $url; s@/+$@@; s@^http://@@gi; s@[^-_a-z\d]@_@gi; my $file = $_; # my $hfile = "$html_cache_dir/$_"; my $hfile = "$_"; # lifted from cheesegrater.pl @ www.jwz.org # check the expirey, and if we've checked this URL recently, don't # re-download the content. # { my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($hfile); if (!defined ($mtime)) { print STDERR "$progname: $hfile does not exist\n" if ($verbose > 3); } elsif ($size < 512) { print STDERR "$progname: $hfile is only $size bytes\n" if ($verbose > 3); } else { my $minutes_ago = int (($now - $mtime) / 60); if ($minutes_ago < $expirey) { print STDERR "$progname: $hfile: modified < $expirey ($minutes_ago) " . "minutes ago.\n" if ($verbose > 2); return $file; } else { print STDERR "$progname: $hfile last modified $minutes_ago " . "minutes ago.\n" if ($verbose > 3); } } } my @cmd = ("wget", "-O", $hfile, $url); if (system(@cmd) == 0) { #pull images open(IN, "< $hfile"); while(){ my $image = ""; if (/ *< *media:content *url *= *"([^"]+).*/){ $image = $1; if ($image ne ""){ my $local_name = $image; $local_name =~ s@.*/@@g; if (! (-e $local_name )){ my @cmd = ("wget", $image); system(@cmd); } } } } close(IN); } return $file; } sub usage { print STDERR "usage: $progname [urls...]\n"; exit 1; } sub main { my @urls = (); while ($_ = $ARGV[0]) { shift @ARGV; if ($_ eq "--verbose"){ $verbose++; } elsif (m/^-v+$/) { $verbose += length($_)-1; } elsif (m/^-./){ usage; } elsif (m/^http:/) { push @urls, $_; } else { usage; } } usage unless ($#urls >= 0); foreach (@urls){ pull_pixs($_); } exit 0; } main; exit 0;