bpakk: Change `-w` option to `-s`/`--size`
[sunny256-utils.git] / getpic
blob77c071a5132f800ca44108663873ac08407fb283
1 #!/usr/bin/env perl
3 # getpic
4 # File ID: 8f4bbf9c-5d3a-11df-b794-90e6ba3022ac
5 # (C)opyleft 1998 Oyvind A. Solheim <sunny@pils.kvarteret.uib.no>
6 # License: GNU General Public License version 2 or later.
8 $Debug = 1;
9 $LastMd5 = substr(`md5sum /dev/null`,0,32);
10 $Extension = ""; # To avoid "Use of uninitialized value" warning.
11 $Prefix = "";
13 print "md5sum /dev/null=\"$LastMd5\"\n" if $Debug;
14 # Syntax: getpic url [prefix] [ext]
16 print "argv = $#ARGV\n" if $Debug;
18 if ($#ARGV < 1) {
19 print "\nSyntax: $0 url sleeptime_in_seconds [prefix] [ext]\n\n";
20 exit 1;
23 $Url = $ARGV[0];
24 $SleepTime = $ARGV[1];
25 $Prefix = $ARGV[2] if $#ARGV > 1;
26 $Extension = $ARGV[3] if $#ARGV > 2;
28 print "\$Url = \"$Url\"\n" if $Debug;
29 print "\$SleepTime = \"$SleepTime\"\n" if $Debug;
30 print "\$Prefix = \"$Prefix\"\n" if $Debug;
31 print "\$Extension = \"$Extension\"\n" if $Debug;
33 # die "Invalid sleeptime: $SleepTime" if ($SleepTime == 0);
34 $LastFile = "last.$Prefix$Extension.dat";
35 $| = 1 if $Debug;
37 for (;;) {
38 $FileName = $Prefix . &Utc . $Extension;
39 $GetCommand = sprintf("wget -nv %s -O %s", $Url, $FileName);
40 # print "Executing \"$GetCommand\"..." if $Debug;
41 system($GetCommand);
42 # print "returned $?\n" if $Debug;
43 $FileMd5 = substr(`md5sum $FileName`,0,32);
44 if ("$FileMd5" eq "d41d8cd98f00b204e9800998ecf8427e") {
45 print "******* $FileName has zero size, removing it. ********\n";
46 unlink($FileName) || warn "$FileName: Can't unlink file: $!";
47 sleep 5;
48 next;
50 $LastMd5 = `cat $LastFile`;
51 # print "\$FileMd5=\"$FileMd5\"\n" if $Debug;
52 # print "\$LastMd5=\"$LastMd5\"\n" if $Debug;
53 if ("$FileMd5" eq "$LastMd5") {
54 print "Removing $FileName.\n" if $Debug;
55 unlink $FileName || warn "$FileName: Can't unlink file: $!";
56 } else {
57 print "********* KEEPING $FileName *********\n" if $Debug;
58 system("echo -n $FileMd5 >$LastFile");
59 # open(LastFP,">$LastFile") or die "$0: $LastFile: Cannot create file: $!";
60 # print LastFP "$FileMd5";
61 # close(LastFP) || warn "$0: $LastFile: Can't close file: $!";
63 last if (-e "stop");
64 if ($SleepTime) {
65 print "Sleeping for $SleepTime seconds..." if $Debug;
66 sleep $SleepTime;
67 print "continuing.\n" if $Debug;
69 print "------------------------------------------------------------------------\n";
71 unlink "stop";
72 exit 0;
74 sub Utc {
75 $FromSystem=time();
76 @TA=gmtime($FromSystem);
77 $UtcTime=sprintf("%04u%02u%02uT%02u%02u%02uZ", $TA[5]+1900, $TA[4]+1, $TA[3], $TA[2], $TA[1], $TA[0]);
78 return $UtcTime;
81 __END__
83 =pod
85 =head1 LICENCE
87 This program is free software; you can redistribute it and/or modify it
88 under the terms of the GNU General Public License as published by the
89 Free Software Foundation; either version 2 of the License, or (at your
90 option) any later version.
92 This program is distributed in the hope that it will be useful, but
93 WITHOUT ANY WARRANTY; without even the implied warranty of
94 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
95 See the GNU General Public License for more details.
97 You should have received a copy of the GNU General Public License along
98 with this program; if not, write to the Free Software Foundation, Inc.,
99 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
101 =cut
103 #### End of file getpic ####