-
Notifications
You must be signed in to change notification settings - Fork 7
/
r2t.php
273 lines (237 loc) · 9.53 KB
/
r2t.php
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
class r2t {
public $debug = true;
protected $feeds = array();
/**
*
*/
function __construct() {
$this->init();
}
protected function init() {
include_once ("sfYaml/sfYaml.class.php");
define('R2T_TEMP_DIR', R2T_PROJECT_DIR . "/tmp/");
if (!file_Exists(R2T_TEMP_DIR)) {
if (!mkdir(R2T_TEMP_DIR)) {
die("Could not create " . R2T_TEMP_DIR);
}
}
$yaml = file_get_contents(R2T_PROJECT_DIR . '/conf/defaults.yml');
$yaml .= file_get_contents(R2T_PROJECT_DIR . '/conf/feeds.yml');
$f = sfYAML::Load($yaml);
if ($f['feeds']) {
$this->feeds = $f['feeds'];
}
$this->defaults = $f['defaults'];
}
public function process() {
foreach ($this->feeds as $feedname => $options) {
$options = $this->mergeOptionsWithDefaults($options);
$newentries = $this->getNewEntries($feedname, $options['url']);
$cnt = 1;
foreach ($newentries as $guid => $entry) {
try {
$options = $this->twit($entry, $options);
} catch (Exception $e) {
$entries = sfYAML::Load(R2T_TEMP_DIR . "/$feedname");
print "Couldn't post " . $entry['title'] . " " . $entry['link'] . " due to " . $e->getMessage() . "\n";
unset ($entries[$guid]);
unset ($newentries[$guid]);
file_put_contents(R2T_TEMP_DIR . "/$feedname", sfYaml::dump($entries));
chmod(R2T_TEMP_DIR . "/$feedname",0666);
continue;
}
$cnt++;
if ($cnt > $options['maxposts']) {
break;
}
}
}
return $newentries;
}
protected function mergeOptionsWithDefaults($options) {
foreach ($this->defaults as $name => $value) {
if (!isset($options[$name])) {
$options[$name] = $value;
}
}
return $options;
}
protected function twit($entry, $options) {
if (isset($options['shortener']) && $options['shortener'] && strlen($entry['link']) > $options['maxurllength']) {
if (!isset($options['shortenerObject'])) {
$this->debug("create " . $options['shortener'] . " class");
include_once ("r2t/shortener/" . $options['shortener'] . ".php");
$classname = "r2t_shortener_" . $options['shortener'];
$options['shortenerObject'] = new $classname();
}
$this->debug("shorten " . $entry['link'] . " to ");
$res = $options['shortenerObject']->shorten($entry['link'],$entry['title']);
if (is_array($res)) {
$entry['link'] = $res['url'];
$entry['title'] = $res['text'];
} else {
$entry['link'] = $res;
}
$this->debug(" " . $entry['link']);
}
// check if something was posted with that link already
include_once("HTTP/Request.php");
if (isset($options['twitter']['user'])) {
$req = new HTTP_Request('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $options['twitter']['user'] );
if ($req->sendRequest()) {
$tweets = json_decode($req->getResponseBody());
foreach($tweets as $tw) {
if (isset($tw->text)) {
if (strpos($tw->text,$entry['link']) !== false) {
$this->debug(" already tweeted link " . $entry['link']);
return $options;
}
} else {
continue;
}
}
}
}
$msg = $entry['title'];
if ($options['addAuthor'] ) {
$msg .= " - " . preg_replace("/[^\s]+@[^\s]+/","",$entry['author']);
}
$msg .= " " . $entry['link'];
$msg = preg_replace("/[\s]{2,}/"," ",$msg);
if (isset($options['prefix'])) {
$msg = $options['prefix'] . " " . $msg;
}
$msg = trim($msg);
$this->debug("twit " . $msg);
/* oauth */
if ($options['twitter']['token'] && class_exists("OAuth")) {
$req_url = 'http://twitter.com/oauth/request_token';
$acc_url = 'http://twitter.com/oauth/access_token';
$authurl = 'http://twitter.com/oauth/authorize';
$api_url = 'http://twitter.com/statuses/update.json';
$conskey = 'DyhAb4DLlFmc5Wn29QvL9g';
$conssec = 'wgaBiC9YJx38sqBLklUqpkWB1Cq1ztAemp5lkfwQ';
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth->debug = 1;
$oauth->setToken($options['twitter']['token'], $options['twitter']['secret']);
$api_args = array("status" => $msg, "empty_param" => NULL);
if (isset($entry['lat'])) {
$api_args['lat'] = $entry['lat'];
$api_args['long'] = $entry['long'];
}
$oauth->fetch($api_url, $api_args, OAUTH_HTTP_METHOD_POST, array("User-Agent" => "pecl/oauth"));
/* end oauth */
} else {
include_once 'Services/Twitter.php';
$service = new Services_Twitter($options['twitter']['user'], $options['twitter']['pass']);
$service->statuses->update($msg);
}
$this->debug("prowlApiKey: " . (isset($options['prowlApiKey']) ? $options['prowlApiKey'] : 'unset'));
if (!empty($options['prowlApiKey'])) {
include_once('ProwlPHP/ProwlPHP.php');
$prowl = new Prowl($options['prowlApiKey']);
$prowl->push(array(
'application'=>'rss2twi.php',
'event'=>'New Post',
'description'=> $msg,
'priority'=>0,
//'apikey'=>'APIKEY' // Not required if already set during object construction.
//'providerkey'=>"PROVIDERKEY'
),true);
}
return $options;
}
protected function getNewEntries($feedname, $url) {
$oldentries = $this->getOldEntries($feedname);
$onlineentries = $this->getOnlineEntries($feedname,$url);
if (count($onlineentries) > 0) {
//keep some old entries, so that they don't get repostet if the show up later
$z = 0;
$max = count($onlineentries);
foreach($oldentries as $k => $v) {
if(!isset($onlineentries[$k])) {
$onlineentries[$k] = $v;
$z++;
if ($z > $max) {
break;
}
}
}
file_put_contents(R2T_TEMP_DIR . "/$feedname", sfYaml::dump($onlineentries));
chmod(R2T_TEMP_DIR . "/$feedname",0666);
}
$newentries = $onlineentries;
foreach ($onlineentries as $guid => $a) {
if (isset($oldentries[$guid])) {
unset($newentries[$guid]);
} else {
$this->debug(" New Entry: " . $a['link'] . " " . $a['title']);
}
}
return $newentries;
}
protected function getOldEntries($feed) {
$file = R2T_TEMP_DIR . "/$feed";
$oldentries = array();
if (file_exists($file)) {
$oldentries = sfYAML::Load($file);
}
return $oldentries;
}
protected function getOnlineEntries($feedname,$url) {
$feed = $this->readFeed($feedname,$url);
$this->debug("Loop through entries");
$entries = array();
foreach ($feed as $entry) {
//$this->debug(" " . $entry->link . ": " . $entry->guid . " " . $entry->title);
if (isset($entry->guid)) {
$entry->guid = $entry->link;
}
$e = array(
"link" => $entry->link,
"title" => $entry->title,
"author" => $entry->author
);
if(!$entry->guid) {
$entry->guid = $entry->link;
}
if ($entry->lat) {
$e['lat'] = $entry->lat;
$e['long'] = $entry->long;
}
$entries[$entry->guid] = $e;
}
return $entries;
}
protected function readFeed($feedname,$url) {
require_once ("XML/Feed/Parser.php");
$this->debug("readFeed for $url");
$body = $this->httpRequest($feedname,$url);
if ($body) {
$this->debug("parse Feed");
return new XML_Feed_Parser($body);
} else {
$this->debug("Feed for $url was empty");
return array();
}
}
protected function httpRequest($feedname,$url) {
require_once ("HTTP/Request.php");
$this->debug("httpRequest for $url");
$req = new HTTP_Request($url);
if (!PEAR::isError($req->sendRequest())) {
return $req->getResponseBody();
}
return null;
}
protected function debug($msg) {
if ($this->debug) {
if (is_string($msg)) {
print $msg . "\n";
} else {
var_dump($msg);
}
}
}
}