4050100f8258b4a98e5898afd8da9160f53eb883
[tweeper.git] / tweeper.php
1 <?php
2 /*
3  * tweeper - a Twitter to RSS web scraper
4  * 
5  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
6  * 
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.
11  * 
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.
16  * 
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/>.
19  */
20
21 date_default_timezone_set('UTC');
22
23 function formatDate($timestamp)
24 {
25   return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
26 }
27
28 class Tweeper {
29
30   private $USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0";
31
32   public function __construct($stylesheet) {
33     $stylesheet_contents = $this->get_contents($stylesheet);
34
35     $xslDoc = new DOMDocument();
36     $xslDoc->loadXML($stylesheet_contents);
37
38     $this->xsltProcessor = new XSLTProcessor();
39     $this->xsltProcessor->registerPHPFunctions();
40     $this->xsltProcessor->importStylesheet($xslDoc);
41   }
42
43   private function get_contents($uri) {
44     # https://www.wjsams.com/c/docs/Wiki/Php-HowToSetUserAgentOnFileGetContents
45     $opts = array(
46       'http' => array(
47         'method' => "GET",
48         'header' => join(array(
49           "Accept-language: en\r\n",
50           "User-Agent: {$this->USER_AGENT}\r\n"
51         ))
52       )
53     );
54
55     $context = stream_context_create($opts);
56     $contents = file_get_contents($uri, false, $context);
57     return $contents;
58   }
59
60   public function tweep($uri) {
61     $html = $this->get_contents($uri);
62
63     $xmlDoc = new DOMDocument();
64     $xmlDoc->loadHTML($html);
65
66     $output = $this->xsltProcessor->transformToXML($xmlDoc);
67
68     if (FALSE === $output) {
69       trigger_error('XSL transformation failed.', E_USER_ERROR);
70       return NULL;
71     }
72     return $output;
73   }
74 }
75
76 function usage($argv)
77 {
78   if (php_sapi_name() != 'cli')
79     $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>");
80   else
81     $usage = "{$argv[0]} <src_url>\n";
82
83   return "usage: $usage";
84 }
85
86 if (isset($_GET['src_url'])) {
87   $src_url = $_GET['src_url'];
88 } else if (isset($argv[1])) {
89   $src_url = $argv[1];
90 } else {
91   die(usage($argv));
92 }
93
94 $url = parse_url($src_url);
95 if (FALSE === $url || empty($url["host"]))
96   die("Invalid url\n");
97
98 $stylesheet = "rss_converter_" . $url["host"] . ".xsl";
99 if (FALSE === file_exists($stylesheet))
100   die("Unsupported host\n");
101
102 $tweeper = new Tweeper($stylesheet);
103 echo $tweeper->tweep($src_url);