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 // The RSS specification says that the enclosure element url must be http.
100 // See http://sourceforge.net/p/feedvalidator/bugs/72/
101 $http_url = preg_replace("/^https/", "http", $url);
103 $url_info = Tweeper::get_info($http_url);
105 $supported = in_array($url_info['content_type'], $supported_content_types);
107 error_log("Unsupported enclosure content type \"" . $url_info['content_type'] . "\" for URL: " . $url_info['url']);
111 $dom = new DomDocument();
112 $enc = $dom->createElement('enclosure');
113 $enc->setAttribute('url', $url_info['url']);
114 $enc->setAttribute('length', $url_info['download_content_length']);
115 $enc->setAttribute('type', $url_info['content_type']);
117 $dom->appendChild($enc);
119 return $dom->saveXML($enc);
122 /* Mimic the message from libxml.c::php_libxml_ctx_error_level() */
123 private function log_xml_error($error) {
126 switch ($error->level) {
127 case LIBXML_ERR_WARNING:
128 $output .= "Warning $error->code: ";
130 case LIBXML_ERR_ERROR:
131 $output .= "Error $error->code: ";
133 case LIBXML_ERR_FATAL:
134 $output .= "Fatal Error $error->code: ";
138 $output .= trim($error->message);
141 $output .= " in $error->file";
143 $output .= " in Entity,";
146 $output .=" line $error->line";
151 private function load_stylesheet($host) {
152 $stylesheet = "file://" . __DIR__ . "/rss_converter_" . $host . ".xsl";
153 if (FALSE === file_exists($stylesheet)) {
154 trigger_error("Conversion to RSS not supported: $host", E_USER_ERROR);
158 $stylesheet_contents = $this->get_contents($stylesheet);
160 $xslDoc = new DOMDocument();
161 $xslDoc->loadXML($stylesheet_contents);
163 $xsltProcessor = new XSLTProcessor();
164 $xsltProcessor->registerPHPFunctions();
165 $xsltProcessor->setParameter('', 'generateEnclosure', $this->generate_enclosure);
166 $xsltProcessor->importStylesheet($xslDoc);
168 return $xsltProcessor;
171 private function html_to_xml($html) {
172 $xmlDoc = new DOMDocument();
174 // Handle warnings and errors when loading invalid HTML.
175 $xml_errors_value = libxml_use_internal_errors(true);
176 $xmlDoc->loadHTML($html);
177 foreach (libxml_get_errors() as $xml_error) {
178 $this->log_xml_error($xml_error);
180 libxml_clear_errors();
181 libxml_use_internal_errors($xml_errors_value);
186 public function tweep($src_url) {
187 $url = parse_url($src_url);
188 if (FALSE === $url || empty($url["host"])) {
189 trigger_error("Invalid url: $src_url", E_USER_ERROR);
193 $xsltProcessor = $this->load_stylesheet($url["host"]);
194 if (NULL === $xsltProcessor) {
198 $html = $this->get_contents($src_url);
199 if (FALSE === $html) {
203 $xmlDoc = $this->html_to_xml($html);
204 if (NULL === $xmlDoc) {
208 $output = $xsltProcessor->transformToXML($xmlDoc);
210 if (FALSE === $output) {
211 trigger_error('XSL transformation failed.', E_USER_ERROR);
218 function usage($argv)
220 if (php_sapi_name() != 'cli')
221 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>");
223 $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n";
225 return "usage: $usage";
228 function parse_options_cli($argv, $argc)
231 'generate_enclosure' => FALSE
237 $cli_options = getopt("eh", array("help"));
238 foreach ($cli_options as $opt => $val) {
241 $options['generate_enclosure'] = TRUE;
248 fwrite(STDERR, usage($argv));
253 $options['src_url'] = $argv[count($cli_options) + 1];
258 function parse_options_query_string()
261 'generate_enclosure' => FALSE
264 if (isset($_GET['src_url']))
265 $options['src_url'] = $_GET['src_url'];
267 if (isset($_GET['generate_enclosure']))
268 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
274 if (php_sapi_name() != 'cli') {
275 $options = parse_options_query_string();
276 $ERROR_STREAM = fopen('php://output', 'w');
278 $options = parse_options_cli($argv, $argc);
279 $ERROR_STREAM = fopen('php://stderr', 'w');
282 if (!isset($options['src_url'])) {
283 fwrite($ERROR_STREAM, usage($argv));
287 $tweeper = new Tweeper($options['generate_enclosure']);
288 echo $tweeper->tweep($options['src_url']);