-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrssdent.php
More file actions
executable file
·194 lines (149 loc) · 4.87 KB
/
rssdent.php
File metadata and controls
executable file
·194 lines (149 loc) · 4.87 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
//
// rssdent.php
//
// h@x0red By Zach Copley (zach.status.net)
// Give this script an RSS feed to suck on, and it will spit the latest
// items in the feed out as dents on your indenti.ca/StatusNet account.
// Uses ur1.ca to shorten urls. Requires PHP 5 and SimplePie 1.1.1.
//
require_once('simplepie.inc');
$NICKNAME = 'jed';
$PASSWORD = 'lizard';
$FEEDURL = 'http://wendolonia.com/blog/feed/';
$STATUSNET_BASE = 'http://statusnetdev.net/zach';
$CURRENT_NOTICE_URL = "$STATUSNET_BASE/api/users/show/$NICKNAME.json";
$STATUSNET_UPDATE_URL = "$STATUSNET_BASE/api/statuses/update.xml";
// Add some hashtags to items
$HASHTAGS = array(
'(mars|martian)' => '#mars',
'ghost' => '#ghosts',
'chupa' => '#chupacabra',
'bigfoot' => '#bigfoot',
'chemtrail' => '#chemtrails',
'robot' => '#robot'
);
date_default_timezone_set('UTC'); # Make sure this is same TZ as your feed!
$current_notice_date = strtotime(get_current_notice_date($CURRENT_NOTICE_URL));
$feed = new SimplePie();
$feed->set_feed_url($FEEDURL);
$feed->enable_cache(false);
$feed->enable_order_by_date(true);
$feed->init();
$notices = array();
foreach ($feed->get_items() as $item) {
$notice = process_item($item);
if (empty($notice)) {
echo "Couldn't send: " . $item->get_title() . "\n";
} else {
array_unshift($notices, $notice);
}
}
if (!empty($notices)) {
array_map("send_notice", $notices);
} else {
print "No new items to send.\n";
}
exit();
function setup_curl() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "rssdent");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
return $ch;
}
function get_current_notice_date($url) {
$ch = setup_curl();
curl_setopt($ch, CURLOPT_URL, $url);
$buffer = curl_exec($ch);
if (!$buffer) {
printf("cURL error: %s\n", curl_error($ch));
curl_close($ch);
exit(1);
}
curl_close($ch);
$notice = json_decode($buffer);
if (!$notice->status->text) {
print "no notices yet.\n";
return;
}
print 'Current notice: ' . $notice->status->text . ' (' . $notice->status->created_at . ')' . "\n";
return $notice->status->created_at;
}
function process_item($item) {
global $current_notice_date, $HASHTAGS;
$new_notice_date = strtotime($item->get_date());
if ($new_notice_date > $current_notice_date) {
$title = truncate($item->get_title());
$link = ur1shorten($item->get_link());
if (empty($link)) {
echo "Couldn't get ur1.ca shorlink for " . $item->get_link() . "\n";
return null;
}
$notice = "$title $link";
$noticelen = strlen($notice);
# Tack on some tags, if there's room
foreach ($HASHTAGS as $keyword => $tag) {
if (eregi($keyword, $notice)) {
if (($noticelen + strlen($tag) + 1) <= 140 ) {
$notice = $notice . " $tag";
}
}
}
return $notice;
}
return null;
}
function send_notice($notice) {
global $NICKNAME, $PASSWORD, $STATUSNET_UPDATE_URL;
$ch = setup_curl();
curl_setopt($ch, CURLOPT_URL, $STATUSNET_UPDATE_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('status' => $notice, 'source' => 'rssdent'));
curl_setopt($ch, CURLOPT_USERPWD, "$NICKNAME:$PASSWORD");
$buffer = curl_exec($ch);
if (empty($buffer)) {
printf("send_notice - trouble posting to '%s', cURL error: %s\n",
$STATUSNET_UPDATE_URL,
curl_error($ch)
);
curl_close($ch);
return null;
}
curl_close($ch);
print "Dent: $notice\n";
sleep(1); # Just to be polite
return;
}
function ur1shorten($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "ur1shorten");
curl_setopt($ch, CURLOPT_URL,"http://ur1.ca");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "longurl=$url");
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$html = curl_exec($ch);
if (!$html) {
printf("url1shorten - cURL error: %s\n", curl_error($ch));
curl_close($ch);
return null;
}
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body/p[@class='success']/a");
return empty($hrefs) ? null : $hrefs->item(0)->getAttribute('href');
}
function truncate($str) {
if (strlen($str) > 100) {
// truncate at 100 chars -- Hey, we have to leave some room for the link
$str = substr($str, 0, 100) . '...';
}
return trim(htmlspecialchars_decode($str));
}
?>