#!/usr/bin/env perl use strict; use warnings; use Getopt::Std; use File::Find; # 2005 Alf Eaton alf@hubmed.org http://hublog.hubmed.org # Written for Mac OS X, should be quite easily modifiable to run elsewhere # Modified for WinXP/Cygwin 2005 Peter Li # Downloads photos from flickr's 'interesting' listing. Nice for changing # screensaver or desktop wallpaper images. By default, uses the cygwin utility # djpeg to scale down images that are much larger than necessary for most # desktops (i.e. images greater than 1MB by default). To disable this scaling, # run with option -m 0. # # Run 'ntrstng-cyg -h' for help with command line arguments. # # To run cron from cygwin: # Setup cron: 'cygrunsrv -I cron -p /usr/sbin/cron -a -D' # Make sure '/var/run/cron.pid' is owned by SYSTEM # Start cron: 'cygrunsrv -S cron' # Verify cron is running: 'ps -a' # Enter crontab 'crontab -e' # E.g.: '0 0 * * * ntrstng-cyg -sp3' # Crontab format guide: 'man 5 crontab' # # Google 'wallpaper changer powertoy' for desktop wallpaper rotation my $days = 2; my $max_file = 1000000; my $pages = 1; my $curl_args = '--insecure --progress-bar'; my $base_dir; ###################################### # Parse command line options, initialize parameters my %options = (); getopts('d:him:o:p:qsz', \%options); print_help() if $options{h}; $days = $options{d} if defined($options{d}); $max_file = $options{m} if defined($options{m}); $pages = $options{p} if defined($options{p}); $options{q} = 1 if $options{s}; # Run quiet if --silent $curl_args .= ' --silent' if $options{q}; $curl_args .= ' --compressed' if $options{z}; if (defined($options{o})) { $base_dir = $options{o}; } else { # Hopefully get the WinXP user home directory from environment vars... my $homedrive = $ENV{HOMEDRIVE}; my $homepath = $ENV{HOMEPATH}; unless ($homepath and $homedrive) { throw_homeerr(\%options); } { local $/ = ':'; chomp($homedrive); } my $homedir = "/cygdrive/$homedrive" . join('/', split(/\\/, $homepath)); $base_dir = "$homedir/My Documents/My Pictures/flickr/"; } my $img_dir = "$base_dir/interesting"; my $arch_dir = "$base_dir/archive"; # Perhaps create directories, but can't do anything if file already exists by # same name if ($options{i}) { unless (-e "$img_dir") { system("mkdir -p '$img_dir' 2>/dev/null"); } unless (-e "$arch_dir") { system("mkdir -p '$arch_dir' 2>/dev/null"); } } # If directories still don't exist, hang unless (-d "$img_dir") { throw_direrr($img_dir, \%options); } unless (-d "$arch_dir") { throw_direrr($arch_dir, \%options); } # Move old images to archive dir system("mv '$img_dir/'*.jpg '$arch_dir' 2>/dev/null"); # Build URLs for interesting photos, retrieve user-specified number of pages my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time-(60*60*24*$days)); # Current time, minus the specified number of days ago my $baseurl = sprintf "http://flickr.com/explore/interesting/%4d/%02d/%02d", 1900+$year, $mon+1, $mday; for (my $i = 1; $i <= $pages; $i++) { # Retrieve page of interesting photo links my $url = "$baseurl/page$i/"; print "$url\n" unless $options{q}; my $page = `curl --silent '$url'` or last; # Parse photo URLs from page, retrieve photos while ($page =~ m|\s*.*?\s*|gis){ my $img_url = "$3" . '_o.jpg'; my $file = "$img_dir/$1-$2.jpg"; print "$1-$2\n" unless $options{q}; if (system('curl', $img_url, split(/ /, $curl_args), '--output', $file)) { last; } } print "\n" unless $options{q}; } # Search through directory for files that are way too big, cut them in # half until they are below $max_file if ($max_file > 0) { print $max_file; print "Reducing files...\n" unless $options{q}; find(sub{ if (/.+\.jpg/) { while (-s > $max_file) { print "\t$_\n" unless $options{q}; unless (system("djpeg -scale 1/2 '$_' | cjpeg 1> $_.tmp 2>/dev/null")) { rename("$_.tmp", $_); } else { last; } } } }, $img_dir); } sub print_help { print <<' '; Usage: ntrstng-cyg [option] Download interesting photos from flickr Example: ntrstng-cyg -p3 -d DAYS Specify number of days delay in downloading photo calendar (default: 2) -h Show help -i Create program directories if they do not already exist -m MAXFILE Specify maximum file size for downloaded images in bytes (Requires command-line utility djpeg) (default: 1000000, use 0 to disable rescaling) -o DIR Specify base directory for downloaded images (default: My Documents/flickr) -p PAGES Specify number of pages of photos to download (default: 1) -q Run quietly: report only errors -s Run silently: do not report errors -z Use compression for download exit; } sub throw_homeerr { my ($options) = @_; die <<' ' unless $options->{s}; ntrstng-cyg: Could not determine user home directory from environment vars. Use the -o flag to override default image directory. exit 1; } sub throw_direrr { my ($errdir, $options) = @_; unless ($options->{s}) { if ($options->{i}) { die "ntrstng-cyg: failed to create directory '$errdir'\n"; } else { die "ntrstng-cyg: '$errdir' does not exist\nUse the -i flag to create program directories automatically\n"; } } exit 1; }