Make get_url_info() and generate_enclosure() static methods
authorAntonio Ospite <ospite@studenti.unina.it>
Sun, 11 Aug 2013 19:23:42 +0000 (21:23 +0200)
committerAntonio Ospite <ospite@studenti.unina.it>
Sun, 11 Aug 2013 23:21:44 +0000 (01:21 +0200)
Also rename get_url_info() to get_info() to match the naming scheme of
get_contents().

rss_converter_twitter.com.xsl
tweeper.php

index 96ecf04..b67946e 100644 (file)
@@ -32,7 +32,7 @@
     </xsl:variable>
 
     <xsl:template match="a[@data-expanded-url]">
-        <xsl:value-of disable-output-escaping="yes" select="php:function('generate_enclosure', string(./@data-expanded-url))"/>
+        <xsl:value-of disable-output-escaping="yes" select="php:function('Tweeper::generate_enclosure', string(./@data-expanded-url))"/>
     </xsl:template>
 
     <xsl:variable name="screen-name" select="//div[@class='profile-card-inner']/@data-screen-name"/>
index 74dc14f..ef55022 100644 (file)
 
 date_default_timezone_set('UTC');
 
-define('USER_AGENT', 'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0');
-
-function get_url_info($url)
-{
-  $ch = curl_init($url);
-  curl_setopt_array($ch, array(
-      CURLOPT_HEADER => TRUE,
-      CURLOPT_NOBODY => TRUE,
-      CURLOPT_FOLLOWLOCATION => TRUE,  // follow http redirects to get the real URL
-      CURLOPT_RETURNTRANSFER => TRUE,
-      CURLOPT_SSL_VERIFYHOST => FALSE,
-      CURLOPT_SSL_VERIFYPEER => FALSE,
-      CURLOPT_USERAGENT => USER_AGENT,
-  ));
-  curl_exec($ch);
-  $url_info = curl_getinfo($ch);;
-  curl_close($ch);
-
-  return $url_info;
-}
-
-function generate_enclosure($url)
-{
-  $supported_content_types = array(
-    "application/ogg",
-    "audio/aac",
-    "audio/mp4",
-    "audio/mpeg",
-    "audio/vorbis",
-    "audio/wav",
-    "audio/webm",
-    "audio/x-midi",
-    "image/gif",
-    "image/jpeg",
-    "video/avi",
-    "video/mp4",
-    "video/mpeg",
-    "video/ogg",
-  );
-
-  $url_info = get_url_info($url);
-
-  $supported = in_array($url_info['content_type'], $supported_content_types);
-  if (!$supported) {
-    error_log("Unsupported enclosure content type \"" . $url_info['content_type'] . "\" for URL: " . $url_info['url']);
-    return '';
-  }
-
-  $dom = new DomDocument();
-  $enc = $dom->createElement('enclosure');
-  $enc->setAttribute('url', $url_info['url']);
-  $enc->setAttribute('length', $url_info['download_content_length']);
-  $enc->setAttribute('type', $url_info['content_type']);
-
-  $dom->appendChild($enc);
-
-  return $dom->saveXML($enc);
-}
-
-
 class Tweeper {
 
+  private static $USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20130405 Firefox/22.0";
+
   public function __construct($stylesheet, $generate_enclosure = FALSE) {
     $stylesheet_contents = $this->get_contents($stylesheet);
 
@@ -112,7 +54,7 @@ class Tweeper {
         'method' => "GET",
         'header' => join(array(
           "Accept-language: en\r\n",
-          "User-Agent: {" + USER_AGENT + "}\r\n"
+          "User-Agent: {" + Tweeper::$USER_AGENT + "}\r\n"
         ))
       )
     );
@@ -122,6 +64,63 @@ class Tweeper {
     return $contents;
   }
 
+  private static function get_info($url)
+  {
+    $ch = curl_init($url);
+    curl_setopt_array($ch, array(
+        CURLOPT_HEADER => TRUE,
+        CURLOPT_NOBODY => TRUE,
+        CURLOPT_FOLLOWLOCATION => TRUE,  // follow http redirects to get the real URL
+        CURLOPT_RETURNTRANSFER => TRUE,
+        CURLOPT_SSL_VERIFYHOST => FALSE,
+        CURLOPT_SSL_VERIFYPEER => FALSE,
+        CURLOPT_USERAGENT => Tweeper::$USER_AGENT,
+    ));
+    curl_exec($ch);
+    $url_info = curl_getinfo($ch);;
+    curl_close($ch);
+
+    return $url_info;
+  }
+
+  public static function generate_enclosure($url)
+  {
+    $supported_content_types = array(
+      "application/ogg",
+      "audio/aac",
+      "audio/mp4",
+      "audio/mpeg",
+      "audio/vorbis",
+      "audio/wav",
+      "audio/webm",
+      "audio/x-midi",
+      "image/gif",
+      "image/jpeg",
+      "video/avi",
+      "video/mp4",
+      "video/mpeg",
+      "video/ogg",
+    );
+
+    $url_info = Tweeper::get_info($url);
+
+    $supported = in_array($url_info['content_type'], $supported_content_types);
+    if (!$supported) {
+      error_log("Unsupported enclosure content type \"" . $url_info['content_type'] . "\" for URL: " . $url_info['url']);
+      return '';
+    }
+
+    $dom = new DomDocument();
+    $enc = $dom->createElement('enclosure');
+    $enc->setAttribute('url', $url_info['url']);
+    $enc->setAttribute('length', $url_info['download_content_length']);
+    $enc->setAttribute('type', $url_info['content_type']);
+
+    $dom->appendChild($enc);
+
+    return $dom->saveXML($enc);
+  }
+
   public function tweep($uri) {
     $html = Tweeper::get_contents($uri);