3 ############################################################################
4 # create-package-feed.pl
6 # Generates an RSS feed for the released files.
9 # create-package-feed.pl "path/to/release/files" "path/to/rss/outputfile"
11 # Copyright (c) 2016 "Unknown", Fabian Keil <fk@fabiankeil.de>
13 # Permission to use, copy, modify, and distribute this software for any
14 # purpose with or without fee is hereby granted, provided that the above
15 # copyright notice and this permission notice appear in all copies.
17 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 ############################################################################
29 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
30 my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
32 my $base_dlurl = 'https://www.privoxy.org/sf-download-mirror/';
33 my $max_advertised_files = 100;
35 sub generate_rss_item($$$$) {
36 my ($target, $target_uri, $target_time, $target_sha256) = @_;
39 my $escaped_target_uri = $target_uri;
40 $escaped_target_uri =~ s@ @%20@g;
44 '<item><title><![CDATA[' . $target_uri . ']]></title>';
46 '<description><![CDATA['
50 . ')]]></description>';
59 $rss_item .= '<pubDate>';
60 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
62 $rss_item .= sprintf("%s, %s %s %d %.2d:%.2d:%.2d GMT",
63 $days[$wday], $mday, $months[$mon], ($year + 1900),
65 $rss_item .= '</pubDate></item>';
71 sub get_sha256_sum($) {
74 open(my $fd, "<", $file)
75 or die "Can't open '$file' to generate checksum $!";
76 my $sha256 = Digest::SHA->new("SHA-256");
77 $sha256->addfile($fd);
80 return $sha256->hexdigest;
83 sub get_released_files($) {
94 opendir(my $D1, $scan_dir) or die "Can't open 1st directory! /";
95 while (my $fi1 = readdir($D1)) {
96 next if ($fi1 =~ m/^\./);
97 next if ($fi1 eq 'OldFiles' or $fi1 eq 'pkgsrc');
99 opendir(my $D2, $scan_dir . $fi1 . '/')
100 or die "Can't open 2nd directory! /$fi1";
101 while (my $fi2 = readdir($D2)) {
102 next if ($fi2 =~ m/^\./);
104 # Start listing /OS/Version/FILE
105 opendir(my $D3, $scan_dir . $fi1 . '/' . $fi2 . '/')
106 or die "Can't open 3rd directory! /$fi1/$fi2";
107 while (my $fi3 = readdir($D3)) {
108 next if ($fi3 =~ m/^\./);
109 $target = $scan_dir . $fi1 . '/' . $fi2 . '/' . $fi3;
110 next if (!-e $target); # skip if file is not exist
112 $target_uri = $fi1 . '/' . $fi2 . '/' . $fi3;
113 $target_time = (stat $target)[9];
115 $Array[$i] = ([$target_time, $target, $target_uri]);
125 return sort { @$a[0] <=> @$b[0] } @Array;
128 sub generate_feed($) {
129 my $scan_dir = shift;
131 # Result = Full XML Codes
132 my $result = '<?xml version="1.0" encoding="utf-8"?>
133 <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
135 <title>Privoxy Releases</title>
136 <link>https://www.privoxy.org/announce.txt</link>
137 <description><![CDATA[Privoxy Releases RSS feed]]></description>
139 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime();
141 "$days[$wday], $mday $months[$mon] "
143 . " $hour:$min:$sec GMT";
144 $result .= '</pubDate>';
147 my @resArray = get_released_files($scan_dir);
148 my $i = @resArray - 1;
149 while ($max_advertised_files-- > 0 && $i >= 0) {
150 my $target_time = $resArray[$i][0];
151 my $target = $resArray[$i][1];
152 my $target_uri = $resArray[$i][2];
154 my $target_sha256 = get_sha256_sum($target);
156 my $rss_item = generate_rss_item($target, $target_uri, $target_time, $target_sha256);
158 $result .= $rss_item;
161 $result .= ' </channel>
168 my $scan_dir = shift(@ARGV)
169 or die "Local package directory not specified (first argument)\n";
170 my $save_rss_file = shift(@ARGV)
171 or die "RSS output file path not specified (second argument)\n";
173 my $result = generate_feed($scan_dir);
175 open(my $XMLF, ">", $save_rss_file) or die "Failed to write XML file";