3 * tweeper - a Twitter to RSS web scraper
5 * Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.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($stylesheet, $generate_enclosure = FALSE) {
28 $stylesheet_contents = $this->get_contents($stylesheet);
30 $xslDoc = new DOMDocument();
31 $xslDoc->loadXML($stylesheet_contents);
33 $this->xsltProcessor = new XSLTProcessor();
34 $this->xsltProcessor->registerPHPFunctions();
35 $this->xsltProcessor->setParameter('', 'generateEnclosure', $generate_enclosure);
36 $this->xsltProcessor->importStylesheet($xslDoc);
39 public static function epoch_to_gmdate($timestamp)
41 return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
44 public static function str_to_gmdate($date)
46 $timestamp = strtotime($date);
47 return Tweeper::epoch_to_gmdate($timestamp);
50 private static function get_contents($url)
52 $ch = curl_init($url);
53 curl_setopt_array($ch, array(
54 CURLOPT_HEADER => FALSE,
55 CURLOPT_FOLLOWLOCATION => TRUE, // follow http redirects to get the real URL
56 CURLOPT_RETURNTRANSFER => TRUE,
57 CURLOPT_SSL_VERIFYHOST => FALSE,
58 CURLOPT_SSL_VERIFYPEER => FALSE,
59 CURLOPT_HTTPHEADER => array('Accept-language: en'),
60 CURLOPT_USERAGENT => Tweeper::$USER_AGENT,
62 $contents = curl_exec($ch);
68 private static function get_info($url)
70 $ch = curl_init($url);
71 curl_setopt_array($ch, array(
72 CURLOPT_HEADER => TRUE,
73 CURLOPT_NOBODY => TRUE,
74 CURLOPT_FOLLOWLOCATION => TRUE, // follow http redirects to get the real URL
75 CURLOPT_RETURNTRANSFER => TRUE,
76 CURLOPT_SSL_VERIFYHOST => FALSE,
77 CURLOPT_SSL_VERIFYPEER => FALSE,
78 CURLOPT_USERAGENT => Tweeper::$USER_AGENT,
81 $url_info = curl_getinfo($ch);
87 public static function generate_enclosure($url)
89 $supported_content_types = array(
106 $url_info = Tweeper::get_info($url);
108 $supported = in_array($url_info['content_type'], $supported_content_types);
110 error_log("Unsupported enclosure content type \"" . $url_info['content_type'] . "\" for URL: " . $url_info['url']);
114 $dom = new DomDocument();
115 $enc = $dom->createElement('enclosure');
116 $enc->setAttribute('url', $url_info['url']);
117 $enc->setAttribute('length', $url_info['download_content_length']);
118 $enc->setAttribute('type', $url_info['content_type']);
120 $dom->appendChild($enc);
122 return $dom->saveXML($enc);
125 /* Mimic the message from libxml.c::php_libxml_ctx_error_level() */
126 private function log_xml_error($error) {
129 switch ($error->level) {
130 case LIBXML_ERR_WARNING:
131 $output .= "Warning $error->code: ";
133 case LIBXML_ERR_ERROR:
134 $output .= "Error $error->code: ";
136 case LIBXML_ERR_FATAL:
137 $output .= "Fatal Error $error->code: ";
141 $output .= trim($error->message);
144 $output .= " in $error->file";
146 $output .= " in Entity,";
149 $output .=" line $error->line";
154 public function tweep($uri) {
155 $html = Tweeper::get_contents($uri);
157 $xmlDoc = new DOMDocument();
159 // Handle warnings and errors when loading invalid HTML.
160 $xml_errors_value = libxml_use_internal_errors(true);
161 $xmlDoc->loadHTML($html);
162 foreach (libxml_get_errors() as $xml_error) {
163 $this->log_xml_error($xml_error);
165 libxml_clear_errors();
166 libxml_use_internal_errors($xml_errors_value);
168 $output = $this->xsltProcessor->transformToXML($xmlDoc);
170 if (FALSE === $output) {
171 trigger_error('XSL transformation failed.', E_USER_ERROR);
178 function usage($argv)
180 if (php_sapi_name() != 'cli')
181 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>");
183 $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n";
185 return "usage: $usage";
188 function parse_options_cli($argv, $argc)
191 'generate_enclosure' => FALSE
197 $cli_options = getopt("eh", array("help"));
198 foreach ($cli_options as $opt => $val) {
201 $options['generate_enclosure'] = TRUE;
208 fwrite(STDERR, usage($argv));
213 $options['src_url'] = $argv[count($cli_options) + 1];
218 function parse_options_query_string()
221 'generate_enclosure' => FALSE
224 if (isset($_GET['src_url']))
225 $options['src_url'] = $_GET['src_url'];
227 if (isset($_GET['generate_enclosure']))
228 $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
234 if (php_sapi_name() != 'cli') {
235 $options = parse_options_query_string();
236 $ERROR_STREAM = fopen('php://output', 'w');
238 $options = parse_options_cli($argv, $argc);
239 $ERROR_STREAM = fopen('php://stderr', 'w');
242 if (!isset($options['src_url'])) {
243 fwrite($ERROR_STREAM, usage($argv));
247 $url = parse_url($options['src_url']);
248 if (FALSE === $url || empty($url["host"])) {
249 fwrite($ERROR_STREAM, "Invalid url: ${options['src_url']}\n");
253 $stylesheet = "file://" . __DIR__ . "/rss_converter_" . $url["host"] . ".xsl";
254 if (FALSE === file_exists($stylesheet)) {
255 fwrite($ERROR_STREAM, "Conversion to RSS not supported: {$url["host"]}\n");
259 $tweeper = new Tweeper($stylesheet, $options['generate_enclosure']);
260 echo $tweeper->tweep($options['src_url']);