Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % cat index.php <master
- <?php
- header("Content-type: text/xml");
- echo '<'.'?xml version="1.0" encoding="UTF-8"?>';
- $username = $_REQUEST["user"];
- #if (!$username) $username = "wildstarops";
- function rewrite_urls($line) {
- $result = preg_replace("/(([a-z]+):\/\/[^ ]+)/i",
- "<a href=\"$1\">$1</a>", $line);
- return $result;
- }
- function log_error($username, $error) {
- $msg = "[twitter2rss] [$username]: $error";
- syslog(LOG_NOTICE, $msg);
- }
- ?>
- <rss version="2.0" xmlns:torrent="http://xmlns.ezrss.it/0.1/">
- <channel>
- <title>@<?php echo $username ?></title>
- <link>https://twitter.com/<?php echo $username ?></link>
- <description>twitter2rss</description>
- <?php
- if ($username) {
- $fetch_url = "https://twitter.com/$username";
- #$fetch_url = "wildstarops.html";
- $cache_file = "cache/" . sha1($fetch_url);
- if (file_exists($cache_file) && filesize($cache_file) > 0 &&
- filemtime($cache_file) > time() - 600) {
- $data = file_get_contents($cache_file);
- } else {
- $opts = array(
- 'http'=>array(
- 'method' => "GET",
- 'header' => "Accept-language: en\r\n"
- ));
- $context = stream_context_create($opts);
- @$data = file_get_contents($fetch_url, false, $context);
- if ($data) {
- file_put_contents($cache_file, $data);
- }
- }
- if ($data) {
- $doc = new DOMDocument();
- if (@$doc->loadHTML($data)) {
- $xpath = new DOMXpath($doc);
- $elems = $xpath->query("//div[@class='content']");
- $found = 0;
- foreach ($elems as $elem) {
- ++$found;
- $username = $xpath->query("*/*/*[contains(@class,'username')]", $elem)->item(0);
- $fullname = $xpath->query("*/*/*[contains(@class,'fullname')]", $elem)->item(0);
- $content = $xpath->query("*[contains(@class,'tweet-text')]", $elem)->item(0);
- $permalink = $xpath->query("*/*/a[contains(@class,'js-permalink')]", $elem)->item(0);
- $timestamp = $xpath->query("*[contains(@class,'_timestamp')]", $permalink)->item(0);
- $img = $xpath->query("*/*/a[contains(@class,'is-preview')]", $elem)->item(0);
- if ($permalink && $content && $username && $fullname && $timestamp) {
- $text = $content->textContent;
- $text_urls = rewrite_urls($text);
- if ($img && $img->hasAttribute('data-url')) {
- $text_urls .= "<p>" . "<img src=\"".$img->getAttribute('data-url')."\">";
- }
- $url = "https://twitter.com" . $permalink->getAttribute('href');
- $author = $fullname->textContent . " " . $username->textContent;
- $ts_formatted = date('r', $timestamp->getAttribute('data-time'));
- print "<item>";
- print "<guid>urn:tag:" . sha1($url) . "</guid>";
- print "<author>$author</author>";
- print "<pubDate>$ts_formatted</pubDate>";
- print "<title>".htmlspecialchars($text)."</title>";
- print "<link>" . htmlspecialchars($url) . "</link>";
- print "<description><![CDATA[$text_urls]]></description>";
- print "</item>";
- } else {
- log_error($username, 'Error parsing tweet contents.');
- }
- }
- if ($found == 0) {
- log_error($username, 'No content elements found.');
- }
- }
- }
- } else {
- print "<!-- username not specified -->";
- }
- ?>
- </channel>
- </rss>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement