c2c5a04378f71a91ffc800c9464ce6bf1c352b54
[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 class Tweeper {
24
25   private static $USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0";
26
27   public function __construct($stylesheet, $generate_enclosure = FALSE) {
28     $stylesheet_contents = $this->get_contents($stylesheet);
29
30     $xslDoc = new DOMDocument();
31     $xslDoc->loadXML($stylesheet_contents);
32
33     $this->xsltProcessor = new XSLTProcessor();
34     $this->xsltProcessor->registerPHPFunctions();
35     $this->xsltProcessor->setParameter('', 'generateEnclosure', $generate_enclosure);
36     $this->xsltProcessor->importStylesheet($xslDoc);
37   }
38
39   public static function epoch_to_gmdate($timestamp)
40   {
41     return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
42   }
43
44   public static function str_to_gmdate($date)
45   {
46     $timestamp = strtotime($date);
47     return Tweeper::epoch_to_gmdate($timestamp);
48   }
49
50   private static function get_contents($url)
51   {
52     $ch = curl_init($url);
53     curl_setopt_array($ch, array(
54       CURLOPT_HEADER => FALSE,
55       CURLOPT_RETURNTRANSFER => TRUE,
56       CURLOPT_SSL_VERIFYHOST => FALSE,
57       CURLOPT_SSL_VERIFYPEER => FALSE,
58       CURLOPT_HTTPHEADER => array('Accept-language: en'),
59       CURLOPT_USERAGENT => Tweeper::$USER_AGENT,
60     ));
61     $contents = curl_exec($ch);
62     curl_close($ch);
63
64     return $contents;
65   }
66
67   private static function get_info($url)
68   {
69     $ch = curl_init($url);
70     curl_setopt_array($ch, array(
71       CURLOPT_HEADER => TRUE,
72       CURLOPT_NOBODY => TRUE,
73       CURLOPT_FOLLOWLOCATION => TRUE,  // follow http redirects to get the real URL
74       CURLOPT_RETURNTRANSFER => TRUE,
75       CURLOPT_SSL_VERIFYHOST => FALSE,
76       CURLOPT_SSL_VERIFYPEER => FALSE,
77       CURLOPT_USERAGENT => Tweeper::$USER_AGENT,
78     ));
79     curl_exec($ch);
80     $url_info = curl_getinfo($ch);
81     curl_close($ch);
82
83     return $url_info;
84   }
85
86   public static function generate_enclosure($url)
87   {
88     $supported_content_types = array(
89       "application/ogg",
90       "audio/aac",
91       "audio/mp4",
92       "audio/mpeg",
93       "audio/vorbis",
94       "audio/wav",
95       "audio/webm",
96       "audio/x-midi",
97       "image/gif",
98       "image/jpeg",
99       "video/avi",
100       "video/mp4",
101       "video/mpeg",
102       "video/ogg",
103     );
104
105     $url_info = Tweeper::get_info($url);
106
107     $supported = in_array($url_info['content_type'], $supported_content_types);
108     if (!$supported) {
109       error_log("Unsupported enclosure content type \"" . $url_info['content_type'] . "\" for URL: " . $url_info['url']);
110       return '';
111     }
112
113     $dom = new DomDocument();
114     $enc = $dom->createElement('enclosure');
115     $enc->setAttribute('url', $url_info['url']);
116     $enc->setAttribute('length', $url_info['download_content_length']);
117     $enc->setAttribute('type', $url_info['content_type']);
118
119     $dom->appendChild($enc);
120
121     return $dom->saveXML($enc);
122   }
123
124   public function tweep($uri) {
125     $html = Tweeper::get_contents($uri);
126
127     $xmlDoc = new DOMDocument();
128     $xmlDoc->loadHTML($html);
129
130     $output = $this->xsltProcessor->transformToXML($xmlDoc);
131
132     if (FALSE === $output) {
133       trigger_error('XSL transformation failed.', E_USER_ERROR);
134       return NULL;
135     }
136     return $output;
137   }
138 }
139
140 function usage($argv)
141 {
142   if (php_sapi_name() != 'cli')
143     $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>");
144   else
145     $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n";
146
147   return "usage: $usage";
148 }
149
150 function parse_options_cli($argv, $argc)
151 {
152   $options = array(
153     'generate_enclosure' => FALSE
154   );
155
156   if ($argc < 2)
157     return $options;
158
159   $cli_options = getopt("eh", array("help"));
160   foreach ($cli_options as $opt => $val) {
161     switch ($opt) {
162     case 'e':
163       $options['generate_enclosure'] = TRUE;
164       break;
165     case 'h':
166     case 'help':
167       echo usage($argv);
168       exit(0);
169     default:
170       die(usage($argv));
171     }
172   }
173
174   $options['src_url'] = $argv[count($cli_options) + 1];
175
176   return $options;
177 }
178
179 function parse_options_query_string()
180 {
181   $options = array(
182     'generate_enclosure' => FALSE
183   );
184
185   if (isset($_GET['src_url']))
186     $options['src_url'] = $_GET['src_url'];
187
188   if (isset($_GET['generate_enclosure']))
189     $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
190
191   return $options;
192 }
193
194
195 if (php_sapi_name() != 'cli')
196   $options = parse_options_query_string();
197 else
198   $options = parse_options_cli($argv, $argc);
199
200 if (!isset($options['src_url']))
201   die(usage($argv));
202
203 $url = parse_url($options['src_url']);
204 if (FALSE === $url || empty($url["host"]))
205   die("Invalid url: ${options['src_url']}\n");
206
207 $stylesheet = "file://" . __DIR__ . "/rss_converter_" . $url["host"] . ".xsl";
208 if (FALSE === file_exists($stylesheet))
209   die("Conversion to RSS not supported: {$url["host"]}\n");
210
211 $tweeper = new Tweeper($stylesheet, $options['generate_enclosure']);
212 echo $tweeper->tweep($options['src_url']);