-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzits-gen-rss.pl
executable file
·122 lines (108 loc) · 3.59 KB
/
zits-gen-rss.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/perl
# Replacement for http://zitscomics.com/feed/ which no more contains
# the actual comics since 19th of February 2015.
#
# Can be used as command-type source in Liferea. (Actually it was
# written for exactly that purpose.)
#
# Author: Axel Beckert <abe@deuxchevaux.org>
# Copyright (C) 2016 Axel Beckert
# License: DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# See LICENSE or http://www.wtfpl.net/txt/copying/ for the
# full license text.
# Boilerplate
use strict;
use warnings;
use 5.010;
our $VERSION = 0.01;
# Configuration
my $number_of_entries = 7;
my $timeout = 3;
my $url_template = 'http://zitscomics.com/comics/%B-%d-%Y/#comic';
my $user_agent = 'Mozilla/5.0';
local $ENV{TZ} = 'AST4ADT';
local $ENV{LC_TIME} = 'en_US';
# Libraries
use Carp;
use POSIX::strftime::Compiler qw(strftime);
use Date::Calc qw(Add_Delta_Days Date_to_Time Time_to_Date Mktime);
use Getopt::Std;
use LWP::UserAgent;
# Commandline parsing
my %options = ();
getopts('nd:', \%options);
if ($options{d}) {
$number_of_entries = $options{d};
}
# Initialisation
my @ymdhms = Time_to_Date(time);
my $now = strftime('%FT%T%z', localtime);
my $ua = LWP::UserAgent->new;
$ua->timeout($timeout);
$ua->env_proxy;
$ua->agent($user_agent
# zits-gen-rss/$VERSION ".$ua->_agent
#." https://github.com/xtaran/zits-gen-rss"
);
# Output RSS header
print <<"EOT";
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<!-- GENERATED BY Axel\'s Zits RSS Generator $VERSION -->
<channel>
<title>Zits</title>
<atom:link href="http://zitscomics.com/feed/" rel="self" type="application/rss+xml" />
<link>http://zitscomics.com/#comic</link>
<description>By Jerry Scott and Jim Borgman</description>
<lastBuildDate>Tue, 14 Jun 2016 00:00:00 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>https://github.com/xtaran/zits-gen-rss</generator>
EOT
# Actual functionality
for (my $i = 0; $i < $number_of_entries; $i++) {
&output_item(Add_Delta_Days(@ymdhms[0..2], -$i));
}
# Output RSS footer
print <<'EOT';
</channel>
</rss>
EOT
sub output_item {
my @ymd = @_;
my @localtime = localtime(Date_to_Time(@ymd,@ymdhms[3..5]));
my $page_url = lc(strftime($url_template, @localtime));
$page_url =~ s/-0/-/g;
my $pubdate = strftime('%a, %d %b %Y', @localtime);
my $img_url;
my $title = 'Zits';
unless($options{n}) {
my $response = $ua->get($page_url);
if ($response->is_success) {
my $content = $response->decoded_content;
if ($content =~ m{value="Zits" /><img src="(.*?)" /></form>}) {
$img_url = $1;
}
}
else {
carp "$page_url gave ".$response->status_line;
}
}
print <<"EOT";
<item>
<title>$title - $pubdate</title>
<link>$page_url</link>
<guid>$page_url</guid>
<pubDate>$pubdate 00:00:00 +00:00</pubDate>
<description><img src="$img_url" border="0" alt="$title" /></description>
</item>
EOT
return;
}