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