Advertisement
Guest User

Untitled

a guest
Mar 20th, 2016
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. % cat index.php <master
  2. <?php
  3. header("Content-type: text/xml");
  4.  
  5. echo '<'.'?xml version="1.0" encoding="UTF-8"?>';
  6.  
  7. $username = $_REQUEST["user"];
  8.  
  9. #if (!$username) $username = "wildstarops";
  10.  
  11. function rewrite_urls($line) {
  12. $result = preg_replace("/(([a-z]+):\/\/[^ ]+)/i",
  13. "<a href=\"$1\">$1</a>", $line);
  14.  
  15. return $result;
  16. }
  17.  
  18. function log_error($username, $error) {
  19. $msg = "[twitter2rss] [$username]: $error";
  20.  
  21. syslog(LOG_NOTICE, $msg);
  22. }
  23.  
  24. ?>
  25. <rss version="2.0" xmlns:torrent="http://xmlns.ezrss.it/0.1/">
  26. <channel>
  27. <title>@<?php echo $username ?></title>
  28. <link>https://twitter.com/<?php echo $username ?></link>
  29. <description>twitter2rss</description>
  30.  
  31. <?php
  32. if ($username) {
  33. $fetch_url = "https://twitter.com/$username";
  34. #$fetch_url = "wildstarops.html";
  35.  
  36. $cache_file = "cache/" . sha1($fetch_url);
  37.  
  38. if (file_exists($cache_file) && filesize($cache_file) > 0 &&
  39. filemtime($cache_file) > time() - 600) {
  40.  
  41. $data = file_get_contents($cache_file);
  42.  
  43. } else {
  44. $opts = array(
  45. 'http'=>array(
  46. 'method' => "GET",
  47. 'header' => "Accept-language: en\r\n"
  48. ));
  49.  
  50. $context = stream_context_create($opts);
  51.  
  52. @$data = file_get_contents($fetch_url, false, $context);
  53.  
  54. if ($data) {
  55. file_put_contents($cache_file, $data);
  56. }
  57. }
  58.  
  59. if ($data) {
  60. $doc = new DOMDocument();
  61.  
  62. if (@$doc->loadHTML($data)) {
  63. $xpath = new DOMXpath($doc);
  64.  
  65. $elems = $xpath->query("//div[@class='content']");
  66. $found = 0;
  67.  
  68. foreach ($elems as $elem) {
  69. ++$found;
  70.  
  71. $username = $xpath->query("*/*/*[contains(@class,'username')]", $elem)->item(0);
  72. $fullname = $xpath->query("*/*/*[contains(@class,'fullname')]", $elem)->item(0);
  73. $content = $xpath->query("*[contains(@class,'tweet-text')]", $elem)->item(0);
  74. $permalink = $xpath->query("*/*/a[contains(@class,'js-permalink')]", $elem)->item(0);
  75. $timestamp = $xpath->query("*[contains(@class,'_timestamp')]", $permalink)->item(0);
  76. $img = $xpath->query("*/*/a[contains(@class,'is-preview')]", $elem)->item(0);
  77.  
  78. if ($permalink && $content && $username && $fullname && $timestamp) {
  79.  
  80. $text = $content->textContent;
  81. $text_urls = rewrite_urls($text);
  82.  
  83. if ($img && $img->hasAttribute('data-url')) {
  84. $text_urls .= "<p>" . "<img src=\"".$img->getAttribute('data-url')."\">";
  85.  
  86. }
  87.  
  88. $url = "https://twitter.com" . $permalink->getAttribute('href');
  89. $author = $fullname->textContent . " " . $username->textContent;
  90. $ts_formatted = date('r', $timestamp->getAttribute('data-time'));
  91.  
  92. print "<item>";
  93. print "<guid>urn:tag:" . sha1($url) . "</guid>";
  94. print "<author>$author</author>";
  95. print "<pubDate>$ts_formatted</pubDate>";
  96. print "<title>".htmlspecialchars($text)."</title>";
  97. print "<link>" . htmlspecialchars($url) . "</link>";
  98. print "<description><![CDATA[$text_urls]]></description>";
  99. print "</item>";
  100.  
  101.  
  102. } else {
  103. log_error($username, 'Error parsing tweet contents.');
  104. }
  105.  
  106. }
  107.  
  108. if ($found == 0) {
  109. log_error($username, 'No content elements found.');
  110. }
  111. }
  112. }
  113. } else {
  114. print "<!-- username not specified -->";
  115.  
  116. }
  117. ?>
  118. </channel>
  119. </rss>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement