diff options
author | emkael <emkael@tlen.pl> | 2016-12-27 14:37:42 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-12-27 14:37:42 +0100 |
commit | 4d80ef582eae06edd785db3d0e4bdf94d83cf537 (patch) | |
tree | 353339f6e6c56fdc0b0ce2497e301b26df1baa39 | |
parent | 0f90768414cfd4c779f8ac51573fcfa0695a13ff (diff) |
* initial commit
-rw-r--r-- | _log/.gitignore | 1 | ||||
-rw-r--r-- | _stats/.gitignore | 1 | ||||
-rw-r--r-- | http/.gitattributes | 1 | ||||
-rw-r--r-- | http/.htaccess | 2 | ||||
l--------- | http/_stats | 1 | ||||
-rw-r--r-- | http/index.php | 108 | ||||
-rw-r--r-- | http/templates/.htaccess | 1 | ||||
-rw-r--r-- | http/templates/atom.tpl | 21 | ||||
-rw-r--r-- | http/templates/rss.tpl | 19 |
9 files changed, 155 insertions, 0 deletions
diff --git a/_log/.gitignore b/_log/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/_log/.gitignore @@ -0,0 +1 @@ +* diff --git a/_stats/.gitignore b/_stats/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/_stats/.gitignore @@ -0,0 +1 @@ +* diff --git a/http/.gitattributes b/http/.gitattributes new file mode 100644 index 0000000..7daec9b --- /dev/null +++ b/http/.gitattributes @@ -0,0 +1 @@ +config.json filter=git-crypt diff=git-crypt diff --git a/http/.htaccess b/http/.htaccess new file mode 100644 index 0000000..9f57019 --- /dev/null +++ b/http/.htaccess @@ -0,0 +1,2 @@ +RewriteEngine On +RewriteRule (.*)/(.*).xml index.php?user=$1&format=$2 diff --git a/http/_stats b/http/_stats new file mode 120000 index 0000000..20da1a2 --- /dev/null +++ b/http/_stats @@ -0,0 +1 @@ +../_stats/
\ No newline at end of file diff --git a/http/index.php b/http/index.php new file mode 100644 index 0000000..cf2f636 --- /dev/null +++ b/http/index.php @@ -0,0 +1,108 @@ +<?php + +$config = json_decode(file_get_contents('config.json'), TRUE); + +require_once('codebird-php/src/codebird.php'); + +\Codebird\Codebird::setConsumerKey($config['key'], $config['secret']); +\Codebird\Codebird::setBearerToken($config['token']); + +$cb = \Codebird\Codebird::getInstance(); + +$user = $_GET['user']; +$spamFilter = FALSE; + +$filterMatch = []; +if (preg_match('/^(.*)\/spamfilter$/', $user, $filterMatch)) { + $user = $filterMatch[1]; + $spamFilter = TRUE; +} + +if ($user) { + + $cacheFile = 'cache/'.$user.'.json'; + $cacheTime = file_exists($cacheFile) ? filemtime($cacheFile) : 0; + + $content = ''; + if ($cacheTime > strtotime('-15 minutes')) { + $content = json_decode(file_get_contents($cacheFile)); + } + else { + $content = $cb->statuses_userTimeline([ + 'screen_name' => $user, + 'count' => 200, + 'exclude_replies' => TRUE + ], TRUE); + if (isset($content->rate)) { + unset($content->rate); + } + file_put_contents($cacheFile, json_encode($content)); + $cacheTime = time(); + } + + if ($content->httpstatus !== 200) { + header('HTTP/1.1 '.$content->httpstatus); + if (isset($content->error)) { + print $content->error; + die(); + } + if (isset($content->errors)) { + foreach ($content->errors as $error) { + print $error->message.' ('.$error->code.')<br />'; + } + die(); + } + } + + unset($content->httpstatus); + + if ($spamFilter) { + $filteredContent = []; + foreach ($content as $c) { + $urls = array_map( + function($url) { + return $url->expanded_url; + }, + $c->entities->urls + ); + if (!$urls) { + $filteredContent[] = $c; + } else { + sort($urls); + $urlHash = md5(implode('|', $urls)); + $filteredContent[$urlHash] = $c; + } + } + usort($filteredContent, function($c1, $c2) { return strcmp($c1->id_str, $c2->id_str); }); + array_reverse($filteredContent); + $content = $filteredContent; + } + + require_once('smarty3/Smarty.class.php'); + + $smarty = new Smarty(); + $smarty->setCacheDir('cache/smarty'); + $smarty->setCompileDir('cache/smarty/compile'); + $smarty->setTemplateDir('templates'); + + $smarty->assign('cacheTime', $cacheTime); + $smarty->assign('user', $user); + $smarty->assign('content', $content); + + $format = $_GET['format']; + + switch ($format) { + case 'rss': + header('Content-Type: application/rss+xml'); + $smarty->display('rss.tpl'); + break; + case 'atom': + default: + header('Content-Type: application/atom+xml'); + $smarty->display('atom.tpl'); + break; + } + +} + +?> diff --git a/http/templates/.htaccess b/http/templates/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/http/templates/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/http/templates/atom.tpl b/http/templates/atom.tpl new file mode 100644 index 0000000..e7e28f5 --- /dev/null +++ b/http/templates/atom.tpl @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<feed xmlns="http://www.w3.org/2005/Atom"> + <title>{$user}'s timeline</title> + <link href="https://twitter.com/{$user}" /> + <id>http://emkael.info/tulz/twitterRSS/{$user}</id> + <updated>{$cacheTime|date_format:"c"}</updated> + {foreach from=$content item=item} + <entry> + <title>{$item->text|escape}</title> + <link href="https://twitter.com/{$user}/status/{$item->id_str}" /> + <id>http://emkael.info/tulz/twitterRSS/{$item->user->id_str}/{$item->id_str}</id> + <updated>{$item->created_at|date_format:"c"}</updated> + <summary><![CDATA[ {$item->text|escape} ]]></summary> + {if isset($item->user)} + <author> + <name>{$item->user->screen_name}</name> + </author> + {/if} + </entry> + {/foreach} +</feed> diff --git a/http/templates/rss.tpl b/http/templates/rss.tpl new file mode 100644 index 0000000..f95a462 --- /dev/null +++ b/http/templates/rss.tpl @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<rss version="2.0"> + <channel> + <title>{$user}'s timeline</title> + <link>https://twitter.com/{$user}</link> + <lastBuildDate>{$cacheTime|date_format:"D, d M Y H:i:s O"}</lastBuildDate> + {foreach from=$content item=item} + <item> + <title>{$item->text|escape}</title> + <link>https://twitter.com/{$user}/status/{$item->id_str}</link> + {if isset($item->user)} + <guid>http://emkael.info/tulz/twitterRSS/{$item->user->id_str}/{$item->id_str}</guid> + {/if} + <description><![CDATA[ {$item->text|escape} ]]></description> + <pubDate>{$item->created_at|date_format:"D, d M Y H:i:s O"}</pubDate> + </item> + {/foreach} + </channel> +</rss> |