forked from boltnet/rss-feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss-feed.pl
executable file
·130 lines (113 loc) · 3.48 KB
/
rss-feed.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
123
124
125
126
127
128
129
130
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use XML::RSS;
use LWP::Simple;
use HTTP::Date;
use Getopt::Long;
use URI::Escape;
use POSIX qw/strftime/;
use LWP;
#$|++;
sub usage {
die("usage: rss-bolt.pl -t token [-f feed_url] [-s state_file] [-a account_id]
-t|--token token - specify the authentication token you want to use
-f|--feed feed_url - URL to RSS feed you want to bolt from (defaults to Google News)
-s|--state state_file - path to a file where we keep RSS state (defaults to the domain)
-a|--account account_id - bolt into a specific account, not the default for the token
More information on the BO.LT API: https://dev.bo.lt/
You can get your token from: https://bo.lt/app/settings#api-app-form\n");
}
my ($help, $stamp, $verbose);
my $state = "";
my $feedurl = "http://news.google.com/news?ned=us&topic=h&output=rss";
my $token = "";
my $account = "";
GetOptions(
"feed|f=s" => \$feedurl,
"state|s=s" => \$state,
"token|t=s" => \$token,
"verbose|v" => \$verbose,
"account|a=s" => \$account,
"help|h" => \$help
);
if ($token !~ /.+/) {
print "ERROR: Need to specify an access token (from https://bo.lt/app/settings#api-app-form)\n";
usage();
}
if ($help) {
usage();
}
if ($state !~ /.+/) {
$state = $feedurl;
$state =~ s/[^A-Za-z0-9\_\-\.]+//g;
$state = "/tmp/" . $state;
}
sub bolt {
my $url = $_[0];
my $comment = $_[1];
my $token = $_[2];
my $verbose = $_[3];
my $account = $_[4];
my $path = $url . "_" . strftime('%Y-%m-%d_%H_%M_%S',localtime);
$path =~ s/https?:\/\///;
$path =~ s/^[_\/]//;
$path =~ s/\/\//\//g;
$path =~ s/\/$//;
$path =~ s/[^A-Za-z0-9\/\-\.\_]/-/g;
my $encoded_url = uri_escape($url);
my $encoded_path = uri_escape($path);
my $bolt_request = "https://api.bo.lt/bolt/create.plain?async=FALSE&url=" . $encoded_url . "&access_token=" . $token . "&path=" . $encoded_path;
if ($comment =~ /.+/) {
my $encoded_comment = uri_escape($comment);
$bolt_request .= "&comment=" . $encoded_comment;
}
if ($account =~ /.+/) {
$bolt_request .= "&account_id=" . $account;
}
my $bolt_user_agent = LWP::UserAgent->new;
my $bolt_output = $bolt_user_agent->get($bolt_request);
die "ERROR: call failed to " . $bolt_request . " with " . $bolt_output->status_line unless $bolt_output->is_success;
if ($verbose) {
print $bolt_output->content;
} else {
my $output = $bolt_output->content;
$output =~ s/.+?bolt.short_url\s+([^\s]+).+/$1/s;
print "$output\n";
}
}
sub getNewLinksFromFeed {
my $stamp = 0;
my $feed_url = $_[0];
my $state_file = $_[1];
my $token = $_[2];
my $verbose = $_[3];
if (-e $state_file) {
local *FILE;
open FILE, "<$state_file";
$stamp = <FILE>;
close FILE;
}
my $xml = get($feed_url);
return unless defined ($xml);
my $rss_parser = new XML::RSS;
$rss_parser->parse($xml);
my $channel = $rss_parser->{channel};
foreach my $item (reverse(@{$rss_parser->{items}})) {
$item->{'pubDate'} =~ s/[A-Z][A-Z][A-Z]+\+(\d\d):(\d\d)/+$1$2/; # work around some timestamps not being recognized by str2time
if (str2time($$item{pubDate}) > $stamp) {
$stamp = str2time($$item{pubDate});
my $title = $item->{'title'};
my $comment = $item->{'title'};
my $url = $item->{'link'};
if ($url =~ /.+/ and $comment =~ /.+/) {
bolt($url, $comment, $token, $verbose, $account);
}
}
}
open FILE, ">$state_file";
print FILE $stamp;
close FILE;
}
getNewLinksFromFeed($feedurl, $state, $token, $verbose);