create-package-feed.pl: Limit items in feed to 100
[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 = 1000;
14 my $max_advertised_files = 100;
15
16 #< Config END >
17
18 my @Array = ();
19 my $i     = 0;
20 my $sec;
21 my $min;
22 my $hour;
23 my $mday;
24 my $mon;
25 my $year;
26 my $wday;
27 my $yday;
28 my $isdst;
29 my $target;
30 my $target_sha1;
31 my $target_uri;
32 my $target_time;
33 my $target_line;
34
35 #
36 # 1st & 2nd directory should NOT contain ANY 'FILES'. (expecting only 'Directory')
37 #
38 opendir(D1, $scan_dir) or die "Can't open 1st directory! /";
39 while (my $fi1 = readdir(D1)) {
40     next if ($fi1 =~ m/^\./);
41
42     next if ($fi1 eq 'OldFiles' or $fi1 eq 'pkgsrc');
43     opendir(D2, $scan_dir . $fi1 . '/')
44         or die "Can't open 2nd directory! /$fi1";
45     while (my $fi2 = readdir(D2)) {
46         next if ($fi2 =~ m/^\./);
47
48 ## start listing /OS/Version/FILE
49         opendir(D3, $scan_dir . $fi1 . '/' . $fi2 . '/')
50             or die "Can't open 3rd directory! /$fi1/$fi2";
51         while (my $fi3 = readdir(D3)) {
52             next if ($fi3 =~ m/^\./);
53             $target = $scan_dir . $fi1 . '/' . $fi2 . '/' . $fi3;
54             next if (!-e $target);    # skip if file is not exist
55
56             # Get SHA-1 hash
57             my $filedata;
58             unless (open $filedata, $target) { next; }
59             my $sha1 = Digest::SHA1->new;
60             $sha1->addfile($filedata);
61             close $filedata;
62             $target_sha1 = $sha1->hexdigest;
63
64             # URI and Time
65             $target_uri  = $fi1 . '/' . $fi2 . '/' . $fi3;
66             $target_time = (stat $target)[9];
67
68             # RSS line
69             $target_line =
70                 '<item><title><![CDATA[' . $target_uri . ']]></title>';
71             $target_line .=
72                   '<description><![CDATA['
73                 . $target_uri
74                 . ' (SHA-1: '
75                 . $target_sha1
76                 . ')]]></description>';
77             $target_line .=
78                   '<link>'
79                 . $base_dlurl
80                 . $target_uri
81                 . '</link><guid>'
82                 . $base_dlurl
83                 . $target_uri
84                 . '</guid>';
85             $target_line .= '<pubDate>';
86             ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
87                 gmtime($target_time);
88             $target_line .=
89                   "$days[$wday], $mday $months[$mon] "
90                 . ($year + 1900)
91                 . " $hour:$min:$sec GMT";
92             $target_line .= '</pubDate></item>';
93
94             # Add it to Array
95             $Array[$i] = ([$target_time, $target_line]);
96             $i++;
97             die "maxlimit $maxlimit reached!" unless ($i < $maxlimit);
98         }
99         closedir D3;
100 ## end listing /OS/Version/FILE
101
102     }
103     closedir D2;
104
105 }
106 closedir D1;
107
108 # Result = Full XML Codes
109 my $result =
110     '<?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>';
111 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime();
112 $result .=
113       "$days[$wday], $mday $months[$mon] "
114     . ($year + 1900)
115     . " $hour:$min:$sec GMT";
116 $result .= '</pubDate>';
117
118 # Sort Array
119 my @resArray = sort { @$a[0] <=> @$b[0] } @Array;
120 $i--;
121 while ($max_advertised_files-- > 0 && $i >= 0) {
122     $result .= $resArray[$i][1];
123     $i--;
124 }
125 $result .= '</channel></rss>';
126
127 # Save it.
128 open(XMLF, "> $save_rss_file") or die "Failed to write XML file";
129 print XMLF $result;
130 close(XMLF);