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 private function html_to_xml($html) {
168 $xmlDoc = new DOMDocument();
170 // Handle warnings and errors when loading invalid HTML.
171 $xml_errors_value = libxml_use_internal_errors(true);
172 $xmlDoc->loadHTML($html);
173 foreach (libxml_get_errors() as $xml_error) {
174 $this->log_xml_error($xml_error);
176 libxml_clear_errors();
177 libxml_use_internal_errors($xml_errors_value);
182 public function tweep($src_url) {
183 $url = parse_url($src_url);
184 if (FALSE === $url || empty($url["host"])) {
185 trigger_error("Invalid url: $src_url", E_USER_ERROR);
189 $xsltProcessor = $this->load_stylesheet($url["host"]);
190 if (NULL === $xsltProcessor) {
194 $html = $this->get_contents($src_url);
195 if (FALSE === $html) {
199 $xmlDoc = $this->html_to_xml($html);
200 if (NULL === $xmlDoc) {
204 $output = $xsltProcessor->transformToXML($xmlDoc);
206 if (FALSE === $output) {
207 trigger_error('XSL transformation failed.', E_USER_ERROR);
214 function usage($argv)
216 if (php_sapi_name() != 'cli')
217 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>");
219 $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n";
221 return "usage: $usage";
224 function parse_options_cli($argv, $argc)
227 'generate_enclosure' => FALSE
233 $cli_options = getopt("eh", array("help"));
234 foreach ($cli_options as $opt => $val) {
237 $options['generate_enclosure'] = TRUE;
244 fwrite(STDERR, usage($argv));
249 $options['src_url'] = $argv[count($cli_options) + 1];
254 function parse_options_query_string()
257 'generate_enclosure' => FALSE
260 if (isset($_GET['src_url']))
261 $options['src_url'] = $_GET['src_url'];
263 if (isset($_GET['generate_enclosure']))
264 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
270 if (php_sapi_name() != 'cli') {
271 $options = parse_options_query_string();
272 $ERROR_STREAM = fopen('php://output', 'w');
274 $options = parse_options_cli($argv, $argc);
275 $ERROR_STREAM = fopen('php://stderr', 'w');
278 if (!isset($options['src_url'])) {
279 fwrite($ERROR_STREAM, usage($argv));
283 $tweeper = new Tweeper($options['generate_enclosure']);
284 echo $tweeper->tweep($options['src_url']);