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 define('USER_AGENT', 'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0');
 
  25 function str_to_gmdate($date)
 
  27   $timestamp = strtotime($date);
 
  28   return epoch_to_gmdate($timestamp);
 
  31 function epoch_to_gmdate($timestamp)
 
  33   return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
 
  36 function get_url_info($url)
 
  38   $ch = curl_init($url);
 
  39   curl_setopt_array($ch, array(
 
  40       CURLOPT_HEADER => TRUE,
 
  41       CURLOPT_NOBODY => TRUE,
 
  42       CURLOPT_FOLLOWLOCATION => TRUE,  // follow http redirects to get the real URL
 
  43       CURLOPT_RETURNTRANSFER => TRUE,
 
  44       CURLOPT_SSL_VERIFYHOST => FALSE,
 
  45       CURLOPT_SSL_VERIFYPEER => FALSE,
 
  46       CURLOPT_USERAGENT => USER_AGENT,
 
  49   $url_info = curl_getinfo($ch);;
 
  55 function generate_enclosure($url)
 
  57   $url_info = get_url_info($url);
 
  59   switch($url_info['content_type']) {
 
  67     case "application/ogg":
 
  74       $dom = new DomDocument();
 
  75       $enc = $dom->createElement('enclosure');
 
  76       $enc->setAttribute( 'url', $url_info['url']);
 
  77       $enc->setAttribute( 'length', $url_info['download_content_length']);
 
  78       $enc->setAttribute( 'type', $url_info['content_type']);
 
  80       $dom->appendChild($enc);
 
  82       return $dom->saveXML($enc);
 
  91   public function __construct($stylesheet, $generate_enclosure = FALSE) {
 
  92     $stylesheet_contents = $this->get_contents($stylesheet);
 
  94     $xslDoc = new DOMDocument();
 
  95     $xslDoc->loadXML($stylesheet_contents);
 
  97     $this->xsltProcessor = new XSLTProcessor();
 
  98     $this->xsltProcessor->registerPHPFunctions();
 
  99     $this->xsltProcessor->setParameter('', 'generateEnclosure', $generate_enclosure);
 
 100     $this->xsltProcessor->importStylesheet($xslDoc);
 
 103   private function get_contents($uri) {
 
 104     # https://www.wjsams.com/c/docs/Wiki/Php-HowToSetUserAgentOnFileGetContents
 
 108         'header' => join(array(
 
 109           "Accept-language: en\r\n",
 
 110           "User-Agent: {" + USER_AGENT + "}\r\n"
 
 115     $context = stream_context_create($opts);
 
 116     $contents = file_get_contents($uri, false, $context);
 
 120   public function tweep($uri) {
 
 121     $html = $this->get_contents($uri);
 
 123     $xmlDoc = new DOMDocument();
 
 124     $xmlDoc->loadHTML($html);
 
 126     $output = $this->xsltProcessor->transformToXML($xmlDoc);
 
 128     if (FALSE === $output) {
 
 129       trigger_error('XSL transformation failed.', E_USER_ERROR);
 
 136 function usage($argv)
 
 138   if (php_sapi_name() != 'cli')
 
 139     $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>");
 
 141     $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n";
 
 143   return "usage: $usage";
 
 146 function parse_options_cli($argv, $argc)
 
 149     'generate_enclosure' => FALSE
 
 155   $cli_options = getopt("eh", array("help"));
 
 156   foreach ($cli_options as $opt => $val) {
 
 159       $options['generate_enclosure'] = TRUE;
 
 170   $options['src_url'] = $argv[count($cli_options) + 1];
 
 175 function parse_options_query_string()
 
 178     'generate_enclosure' => FALSE
 
 181   if (isset($_GET['src_url']))
 
 182     $options['src_url'] = $_GET['src_url'];
 
 184   if (isset($_GET['generate_enclosure']))
 
 185     $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1;
 
 191 if (php_sapi_name() != 'cli')
 
 192   $options = parse_options_query_string();
 
 194   $options = parse_options_cli($argv, $argc);
 
 196 if (!isset($options['src_url']))
 
 199 $url = parse_url($options['src_url']);
 
 200 if (FALSE === $url || empty($url["host"]))
 
 201   die("Invalid url: ${options['src_url']}\n");
 
 203 $stylesheet = __DIR__ . "/rss_converter_" . $url["host"] . ".xsl";
 
 204 if (FALSE === file_exists($stylesheet))
 
 205   die("Conversion to RSS not supported: {$url["host"]}\n");
 
 207 $tweeper = new Tweeper($stylesheet, $options['generate_enclosure']);
 
 208 echo $tweeper->tweep($options['src_url']);