PageRenderTime 64ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/r2t.php

http://github.com/chregu/rss2twi.php
PHP | 273 lines | 236 code | 28 blank | 9 comment | 32 complexity | 406184d59f69d09906e19d8d710b093a MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. class r2t {
  3. public $debug = true;
  4. protected $feeds = array();
  5. /**
  6. *
  7. */
  8. function __construct() {
  9. $this->init();
  10. }
  11. protected function init() {
  12. include_once ("sfYaml/sfYaml.class.php");
  13. define('R2T_TEMP_DIR', R2T_PROJECT_DIR . "/tmp/");
  14. if (!file_Exists(R2T_TEMP_DIR)) {
  15. if (!mkdir(R2T_TEMP_DIR)) {
  16. die("Could not create " . R2T_TEMP_DIR);
  17. }
  18. }
  19. $yaml = file_get_contents(R2T_PROJECT_DIR . '/conf/defaults.yml');
  20. $yaml .= file_get_contents(R2T_PROJECT_DIR . '/conf/feeds.yml');
  21. $f = sfYAML::Load($yaml);
  22. if ($f['feeds']) {
  23. $this->feeds = $f['feeds'];
  24. }
  25. $this->defaults = $f['defaults'];
  26. }
  27. public function process() {
  28. foreach ($this->feeds as $feedname => $options) {
  29. $options = $this->mergeOptionsWithDefaults($options);
  30. $newentries = $this->getNewEntries($feedname, $options['url']);
  31. $cnt = 1;
  32. foreach ($newentries as $guid => $entry) {
  33. try {
  34. $options = $this->twit($entry, $options);
  35. } catch (Exception $e) {
  36. $entries = sfYAML::Load(R2T_TEMP_DIR . "/$feedname");
  37. print "Couldn't post " . $entry['title'] . " " . $entry['link'] . " due to " . $e->getMessage() . "\n";
  38. unset ($entries[$guid]);
  39. unset ($newentries[$guid]);
  40. file_put_contents(R2T_TEMP_DIR . "/$feedname", sfYaml::dump($entries));
  41. chmod(R2T_TEMP_DIR . "/$feedname",0666);
  42. continue;
  43. }
  44. $cnt++;
  45. if ($cnt > $options['maxposts']) {
  46. break;
  47. }
  48. }
  49. }
  50. return $newentries;
  51. }
  52. protected function mergeOptionsWithDefaults($options) {
  53. foreach ($this->defaults as $name => $value) {
  54. if (!isset($options[$name])) {
  55. $options[$name] = $value;
  56. }
  57. }
  58. return $options;
  59. }
  60. protected function twit($entry, $options) {
  61. if (isset($options['shortener']) && $options['shortener'] && strlen($entry['link']) > $options['maxurllength']) {
  62. if (!isset($options['shortenerObject'])) {
  63. $this->debug("create " . $options['shortener'] . " class");
  64. include_once ("r2t/shortener/" . $options['shortener'] . ".php");
  65. $classname = "r2t_shortener_" . $options['shortener'];
  66. $options['shortenerObject'] = new $classname();
  67. }
  68. $this->debug("shorten " . $entry['link'] . " to ");
  69. $res = $options['shortenerObject']->shorten($entry['link'],$entry['title']);
  70. if (is_array($res)) {
  71. $entry['link'] = $res['url'];
  72. $entry['title'] = $res['text'];
  73. } else {
  74. $entry['link'] = $res;
  75. }
  76. $this->debug(" " . $entry['link']);
  77. }
  78. // check if something was posted with that link already
  79. include_once("HTTP/Request.php");
  80. if (isset($options['twitter']['user'])) {
  81. $req = new HTTP_Request('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $options['twitter']['user'] );
  82. if ($req->sendRequest()) {
  83. $tweets = json_decode($req->getResponseBody());
  84. foreach($tweets as $tw) {
  85. if (isset($tw->text)) {
  86. if (strpos($tw->text,$entry['link']) !== false) {
  87. $this->debug(" already tweeted link " . $entry['link']);
  88. return $options;
  89. }
  90. } else {
  91. continue;
  92. }
  93. }
  94. }
  95. }
  96. $msg = $entry['title'];
  97. if ($options['addAuthor'] ) {
  98. $msg .= " - " . preg_replace("/[^\s]+@[^\s]+/","",$entry['author']);
  99. }
  100. $msg .= " " . $entry['link'];
  101. $msg = preg_replace("/[\s]{2,}/"," ",$msg);
  102. if (isset($options['prefix'])) {
  103. $msg = $options['prefix'] . " " . $msg;
  104. }
  105. $msg = trim($msg);
  106. $this->debug("twit " . $msg);
  107. /* oauth */
  108. if ($options['twitter']['token'] && class_exists("OAuth")) {
  109. $req_url = 'http://twitter.com/oauth/request_token';
  110. $acc_url = 'http://twitter.com/oauth/access_token';
  111. $authurl = 'http://twitter.com/oauth/authorize';
  112. $api_url = 'http://twitter.com/statuses/update.json';
  113. $conskey = 'DyhAb4DLlFmc5Wn29QvL9g';
  114. $conssec = 'wgaBiC9YJx38sqBLklUqpkWB1Cq1ztAemp5lkfwQ';
  115. $oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
  116. $oauth->debug = 1;
  117. $oauth->setToken($options['twitter']['token'], $options['twitter']['secret']);
  118. $api_args = array("status" => $msg, "empty_param" => NULL);
  119. if (isset($entry['lat'])) {
  120. $api_args['lat'] = $entry['lat'];
  121. $api_args['long'] = $entry['long'];
  122. }
  123. $oauth->fetch($api_url, $api_args, OAUTH_HTTP_METHOD_POST, array("User-Agent" => "pecl/oauth"));
  124. /* end oauth */
  125. } else {
  126. include_once 'Services/Twitter.php';
  127. $service = new Services_Twitter($options['twitter']['user'], $options['twitter']['pass']);
  128. $service->statuses->update($msg);
  129. }
  130. $this->debug("prowlApiKey: " . (isset($options['prowlApiKey']) ? $options['prowlApiKey'] : 'unset'));
  131. if (!empty($options['prowlApiKey'])) {
  132. include_once('ProwlPHP/ProwlPHP.php');
  133. $prowl = new Prowl($options['prowlApiKey']);
  134. $prowl->push(array(
  135. 'application'=>'rss2twi.php',
  136. 'event'=>'New Post',
  137. 'description'=> $msg,
  138. 'priority'=>0,
  139. //'apikey'=>'APIKEY' // Not required if already set during object construction.
  140. //'providerkey'=>"PROVIDERKEY'
  141. ),true);
  142. }
  143. return $options;
  144. }
  145. protected function getNewEntries($feedname, $url) {
  146. $oldentries = $this->getOldEntries($feedname);
  147. $onlineentries = $this->getOnlineEntries($feedname,$url);
  148. if (count($onlineentries) > 0) {
  149. //keep some old entries, so that they don't get repostet if the show up later
  150. $z = 0;
  151. $max = count($onlineentries);
  152. foreach($oldentries as $k => $v) {
  153. if(!isset($onlineentries[$k])) {
  154. $onlineentries[$k] = $v;
  155. $z++;
  156. if ($z > $max) {
  157. break;
  158. }
  159. }
  160. }
  161. file_put_contents(R2T_TEMP_DIR . "/$feedname", sfYaml::dump($onlineentries));
  162. chmod(R2T_TEMP_DIR . "/$feedname",0666);
  163. }
  164. $newentries = $onlineentries;
  165. foreach ($onlineentries as $guid => $a) {
  166. if (isset($oldentries[$guid])) {
  167. unset($newentries[$guid]);
  168. } else {
  169. $this->debug(" New Entry: " . $a['link'] . " " . $a['title']);
  170. }
  171. }
  172. return $newentries;
  173. }
  174. protected function getOldEntries($feed) {
  175. $file = R2T_TEMP_DIR . "/$feed";
  176. $oldentries = array();
  177. if (file_exists($file)) {
  178. $oldentries = sfYAML::Load($file);
  179. }
  180. return $oldentries;
  181. }
  182. protected function getOnlineEntries($feedname,$url) {
  183. $feed = $this->readFeed($feedname,$url);
  184. $this->debug("Loop through entries");
  185. $entries = array();
  186. foreach ($feed as $entry) {
  187. //$this->debug(" " . $entry->link . ": " . $entry->guid . " " . $entry->title);
  188. if (isset($entry->guid)) {
  189. $entry->guid = $entry->link;
  190. }
  191. $e = array(
  192. "link" => $entry->link,
  193. "title" => $entry->title,
  194. "author" => $entry->author
  195. );
  196. if(!$entry->guid) {
  197. $entry->guid = $entry->link;
  198. }
  199. if ($entry->lat) {
  200. $e['lat'] = $entry->lat;
  201. $e['long'] = $entry->long;
  202. }
  203. $entries[$entry->guid] = $e;
  204. }
  205. return $entries;
  206. }
  207. protected function readFeed($feedname,$url) {
  208. require_once ("XML/Feed/Parser.php");
  209. $this->debug("readFeed for $url");
  210. $body = $this->httpRequest($feedname,$url);
  211. if ($body) {
  212. $this->debug("parse Feed");
  213. return new XML_Feed_Parser($body);
  214. } else {
  215. $this->debug("Feed for $url was empty");
  216. return array();
  217. }
  218. }
  219. protected function httpRequest($feedname,$url) {
  220. require_once ("HTTP/Request.php");
  221. $this->debug("httpRequest for $url");
  222. $req = new HTTP_Request($url);
  223. if (!PEAR::isError($req->sendRequest())) {
  224. return $req->getResponseBody();
  225. }
  226. return null;
  227. }
  228. protected function debug($msg) {
  229. if ($this->debug) {
  230. if (is_string($msg)) {
  231. print $msg . "\n";
  232. } else {
  233. var_dump($msg);
  234. }
  235. }
  236. }
  237. }