Add perl script to generate an RSS feed for the packages
[privoxy.git] / utils / create-package-feed.pl
1 #!/usr/local/bin/perl
2 #< LICENSE: WTFPL >
3 use warnings;
4 use strict;
5 use Digest::SHA1;
6 my @months=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
7 my @days=qw(Sun Mon Tue Wed Thu Fri Sat Sun);
8
9 #< Config START >
10 my $scan_dir='/xxxxxxxxxxxxxxxxxxxxxx/sf-download/';
11 my $base_dlurl='https://www.privoxy.org/sf-download-mirror/';
12 my $save_rss_file='/xxxxxxxxxxxxxxxxxxxxxx/release.xml';# e.g., release.rss
13 my $maxlimit=10;
14 #< Config END >
15
16 my @Array=();
17 my $i=0;
18 my $sec;my $min;my $hour;my $mday;my $mon;my $year;my $wday;my $yday;my $isdst;
19 my $target;my $target_sha1;my $target_uri;my $target_time;my $target_line;
20
21
22 # 1st & 2nd directory should NOT contain ANY 'FILES'. (expecting only 'Directory')
23
24 opendir (D1,$scan_dir) or die "Can't open 1st directory! /";
25 MOUT: while (my $fi1=readdir(D1)){
26 next if ($fi1 =~ m/^\./);
27
28 opendir (D2,$scan_dir.$fi1.'/') or die "Can't open 2nd directory! /$fi1";
29 while (my $fi2=readdir(D2)){
30 next if ($fi2 =~ m/^\./);
31
32 ## start listing /OS/Version/FILE
33 opendir (D3,$scan_dir.$fi1.'/'.$fi2.'/') or die "Can't open 3rd directory! /$fi1/$fi2";
34 while (my $fi3=readdir(D3)){
35 next if ($fi3 =~ m/^\./);
36 $target=$scan_dir.$fi1.'/'.$fi2.'/'.$fi3;next if (! -e $target);# skip if file is not exist
37
38 # Get SHA-1 hash
39 my $filedata;
40 unless (open $filedata,$target){next;}
41 my $sha1 = Digest::SHA1->new;$sha1->addfile($filedata);close $filedata;
42 $target_sha1=$sha1->hexdigest;
43
44 # URI and Time
45 $target_uri=$fi1.'/'.$fi2.'/'.$fi3;
46 $target_time=(stat $target)[9];
47
48 # RSS line
49 $target_line='<item><title><![CDATA['.$target_uri.']]></title>';
50 $target_line.='<description><![CDATA['.$target_uri.' (SHA-1: '.$target_sha1.')]]></description>';
51 $target_line.='<link>'.$base_dlurl.$target_uri.'</link><guid>'.$base_dlurl.$target_uri.'</guid>';
52 $target_line.='<pubDate>';
53 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime($target_time);$target_line.="$days[$wday], $mday $months[$mon] ".($year+1900)." $hour:$min:$sec GMT";
54 $target_line.='</pubDate></item>';
55
56 # Add it to Array
57 $Array[$i]=([$target_time,$target_line]);$i++;
58 if ($i>=$maxlimit){last MOUT;}
59 }
60 closedir D3;
61 ## end listing /OS/Version/FILE
62
63 }
64 closedir D2;
65
66 }
67 closedir D1;
68
69 # Result = Full XML Codes
70 my $result='<?xml version="1.0" encoding="utf-8"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"><channel><title>Privoxy Releases</title><link>https://www.privoxy.org/announce.txt</link><description><![CDATA[Privoxy Releases RSS feed]]></description><pubDate>';
71 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime();$result.="$days[$wday], $mday $months[$mon] ".($year+1900)." $hour:$min:$sec GMT";
72 $result.='</pubDate>';
73 # Sort Array
74 my @resArray=sort {@$a[0]<=>@$b[0]} @Array;$i--;
75 while($i>=0){$result.=$resArray[$i][1];$i--;}  
76 $result.='</channel></rss>';
77 # Save it.
78 open(XMLF,"> $save_rss_file") or die "Failed to write XML file";  
79 print XMLF $result;
80 close(XMLF);