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');
23 function str_to_gmdate($date)
25 $timestamp = strtotime($date);
26 return epoch_to_gmdate($timestamp);
29 function epoch_to_gmdate($timestamp)
31 return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
34 function get_url_info($url)
36 $ch = curl_init($url);
37 curl_setopt_array($ch, array(
38 CURLOPT_HEADER => TRUE, // get the header
39 CURLOPT_NOBODY => TRUE, // don't donwload body
40 CURLOPT_FOLLOWLOCATION => TRUE, // the magic sauce
41 CURLOPT_RETURNTRANSFER => TRUE,
42 CURLOPT_SSL_VERIFYHOST => FALSE, // suppress certain SSL errors
43 CURLOPT_SSL_VERIFYPEER => FALSE,
46 $url_info = curl_getinfo($ch);;
52 function generate_enclosure($url) {
53 $url_info = get_url_info($url);
55 $dom = new DomDocument();
56 $enc = $dom->createElement('enclosure');
57 $enc->setAttribute( 'url', $url_info['url']);
58 $enc->setAttribute( 'length', $url_info['download_content_length']);
59 $enc->setAttribute( 'type', $url_info['content_type']);
61 $dom->appendChild($enc);
63 return $dom->saveXML($enc);
69 private $USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0";
71 public function __construct($stylesheet) {
72 $stylesheet_contents = $this->get_contents($stylesheet);
74 $xslDoc = new DOMDocument();
75 $xslDoc->loadXML($stylesheet_contents);
77 $this->xsltProcessor = new XSLTProcessor();
78 $this->xsltProcessor->registerPHPFunctions();
79 $this->xsltProcessor->importStylesheet($xslDoc);
82 private function get_contents($uri) {
83 # https://www.wjsams.com/c/docs/Wiki/Php-HowToSetUserAgentOnFileGetContents
87 'header' => join(array(
88 "Accept-language: en\r\n",
89 "User-Agent: {$this->USER_AGENT}\r\n"
94 $context = stream_context_create($opts);
95 $contents = file_get_contents($uri, false, $context);
99 public function tweep($uri) {
100 $html = $this->get_contents($uri);
102 $xmlDoc = new DOMDocument();
103 $xmlDoc->loadHTML($html);
105 $output = $this->xsltProcessor->transformToXML($xmlDoc);
107 if (FALSE === $output) {
108 trigger_error('XSL transformation failed.', E_USER_ERROR);
115 function usage($argv)
117 if (php_sapi_name() != 'cli')
118 $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>");
120 $usage = "{$argv[0]} <src_url>\n";
122 return "usage: $usage";
125 if (isset($_GET['src_url'])) {
126 $src_url = $_GET['src_url'];
127 } else if (isset($argv[1])) {
128 if ($argv[1] == "-h" || $argv[1] == "--help") {
137 $url = parse_url($src_url);
138 if (FALSE === $url || empty($url["host"]))
139 die("Invalid url: $src_url\n");
141 $stylesheet = __DIR__ . "/rss_converter_" . $url["host"] . ".xsl";
142 if (FALSE === file_exists($stylesheet))
143 die("Conversion to RSS not supported: {$url["host"]}\n");
145 $tweeper = new Tweeper($stylesheet);
146 echo $tweeper->tweep($src_url);