3 * tweeper - a Twitter to RSS web scraper
5 * Copyright (C) 2013-2015 Antonio Ospite <ao2@ao2.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 static $USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0";
27 public function __construct($generate_enclosure = FALSE) {
28 $this->generate_enclosure = $generate_enclosure;
31 public static function epoch_to_gmdate($timestamp)
33 return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
36 public static function str_to_gmdate($date)
38 $timestamp = strtotime($date);
39 return Tweeper::epoch_to_gmdate($timestamp);
42 private static function get_contents($url)
44 $ch = curl_init($url);
45 curl_setopt_array($ch, array(
46 CURLOPT_HEADER => FALSE,
47 CURLOPT_FOLLOWLOCATION => TRUE, // follow http redirects to get the real URL
48 CURLOPT_RETURNTRANSFER => TRUE,
49 CURLOPT_SSL_VERIFYHOST => FALSE,
50 CURLOPT_SSL_VERIFYPEER => FALSE,
51 CURLOPT_HTTPHEADER => array('Accept-language: en'),
52 CURLOPT_USERAGENT => Tweeper::$USER_AGENT,
54 $contents = curl_exec($ch);
60 private static function get_info($url)
62 $ch = curl_init($url);
63 curl_setopt_array($ch, array(
64 CURLOPT_HEADER => TRUE,
65 CURLOPT_NOBODY => TRUE,
66 CURLOPT_FOLLOWLOCATION => TRUE, // follow http redirects to get the real URL
67 CURLOPT_RETURNTRANSFER => TRUE,
68 CURLOPT_SSL_VERIFYHOST => FALSE,
69 CURLOPT_SSL_VERIFYPEER => FALSE,
70 CURLOPT_USERAGENT => Tweeper::$USER_AGENT,
73 $url_info = curl_getinfo($ch);
79 public static function generate_enclosure($url)
81 $supported_content_types = array(
99 $url_info = Tweeper::get_info($url);
101 $supported = in_array($url_info['content_type'], $supported_content_types);
103 error_log("Unsupported enclosure content type \"" . $url_info['content_type'] . "\" for URL: " . $url_info['url']);
107 $dom = new DomDocument();
108 $enc = $dom->createElement('enclosure');
109 $enc->setAttribute('url', $url_info['url']);
110 $enc->setAttribute('length', $url_info['download_content_length']);
111 $enc->setAttribute('type', $url_info['content_type']);
113 $dom->appendChild($enc);
115 return $dom->saveXML($enc);
118 /* Mimic the message from libxml.c::php_libxml_ctx_error_level() */
119 private function log_xml_error($error) {
122 switch ($error->level) {
123 case LIBXML_ERR_WARNING:
124 $output .= "Warning $error->code: ";
126 case LIBXML_ERR_ERROR:
127 $output .= "Error $error->code: ";
129 case LIBXML_ERR_FATAL:
130 $output .= "Fatal Error $error->code: ";
134 $output .= trim($error->message);
137 $output .= " in $error->file";
139 $output .= " in Entity,";
142 $output .=" line $error->line";
147 private function load_stylesheet($host) {
148 $stylesheet = "file://" . __DIR__ . "/rss_converter_" . $host . ".xsl";
149 if (FALSE === file_exists($stylesheet)) {
150 trigger_error("Conversion to RSS not supported: $host", E_USER_ERROR);
154 $stylesheet_contents = $this->get_contents($stylesheet);
156 $xslDoc = new DOMDocument();
157 $xslDoc->loadXML($stylesheet_contents);
159 $xsltProcessor = new XSLTProcessor();
160 $xsltProcessor->registerPHPFunctions();
161 $xsltProcessor->setParameter('', 'generateEnclosure', $this->generate_enclosure);
162 $xsltProcessor->importStylesheet($xslDoc);
164 return $xsltProcessor;
167 public function tweep($src_url) {
168 $url = parse_url($src_url);
169 if (FALSE === $url || empty($url["host"])) {
170 trigger_error("Invalid url: $src_url", E_USER_ERROR);
174 $xsltProcessor = $this->load_stylesheet($url["host"]);
175 if (NULL === $xsltProcessor) {
179 $html = Tweeper::get_contents($src_url);
181 $xmlDoc = new DOMDocument();
183 // Handle warnings and errors when loading invalid HTML.
184 $xml_errors_value = libxml_use_internal_errors(true);
185 $xmlDoc->loadHTML($html);
186 foreach (libxml_get_errors() as $xml_error) {
187 $this->log_xml_error($xml_error);
189 libxml_clear_errors();
190 libxml_use_internal_errors($xml_errors_value);
192 $output = $xsltProcessor->transformToXML($xmlDoc);
194 if (FALSE === $output) {
195 trigger_error('XSL transformation failed.', E_USER_ERROR);
202 function usage($argv)
204 if (php_sapi_name() != 'cli')
205 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>");
207 $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n";
209 return "usage: $usage";
212 function parse_options_cli($argv, $argc)
215 'generate_enclosure' => FALSE
221 $cli_options = getopt("eh", array("help"));
222 foreach ($cli_options as $opt => $val) {
225 $options['generate_enclosure'] = TRUE;
232 fwrite(STDERR, usage($argv));
237 $options['src_url'] = $argv[count($cli_options) + 1];
242 function parse_options_query_string()
245 'generate_enclosure' => FALSE
248 if (isset($_GET['src_url']))
249 $options['src_url'] = $_GET['src_url'];
251 if (isset($_GET['generate_enclosure']))
252 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
258 if (php_sapi_name() != 'cli') {
259 $options = parse_options_query_string();
260 $ERROR_STREAM = fopen('php://output', 'w');
262 $options = parse_options_cli($argv, $argc);
263 $ERROR_STREAM = fopen('php://stderr', 'w');
266 if (!isset($options['src_url'])) {
267 fwrite($ERROR_STREAM, usage($argv));
271 $tweeper = new Tweeper($options['generate_enclosure']);
272 echo $tweeper->tweep($options['src_url']);