3 * tweeper - a Twitter to RSS web scraper
5 * Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 date_default_timezone_set('UTC');
25 private $USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0";
27 public function __construct($stylesheet) {
28 $stylesheet_contents = $this->get_contents($stylesheet);
30 $xslDoc = new DOMDocument();
31 $xslDoc->loadXML($stylesheet_contents);
33 $this->xsltProcessor = new XSLTProcessor();
34 $this->xsltProcessor->importStylesheet($xslDoc);
37 private function get_contents($uri) {
38 # https://www.wjsams.com/c/docs/Wiki/Php-HowToSetUserAgentOnFileGetContents
42 'header' => join(array(
43 "Accept-language: en\r\n",
44 "User-Agent: {$this->USER_AGENT}\r\n"
49 $context = stream_context_create($opts);
50 $contents = file_get_contents($uri, false, $context);
54 public function tweep($uri) {
55 $html = $this->get_contents($uri);
57 $xmlDoc = new DOMDocument();
58 $xmlDoc->loadHTML($html);
60 $output = $this->xsltProcessor->transformToXML($xmlDoc);
62 if (FALSE === $output) {
63 trigger_error('XSL transformation failed.', E_USER_ERROR);
70 if (isset($_GET['screen_name'])) {
71 $screen_name = $_GET['screen_name'];
72 } else if (isset($argv[1])) {
73 $screen_name = $argv[1];
75 if (isset($_SERVER['SCRIPT_NAME']))
76 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?screen_name=<screen_name>");
78 $usage = "{$argv[0]} <screen_name>\n";
83 $tweeper = new Tweeper('twitter_user_timeline2rss.xsl');
85 $src_uri = 'https://twitter.com/' . $screen_name;
86 echo $tweeper->tweep($src_uri);