From 8e7df9a925ce30ebdd2bf6441c9dd639dd0ffddd Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Sun, 11 Aug 2013 21:23:42 +0200 Subject: [PATCH] Make get_url_info() and generate_enclosure() static methods Also rename get_url_info() to get_info() to match the naming scheme of get_contents(). --- rss_converter_twitter.com.xsl | 2 +- tweeper.php | 121 +++++++++++++++++++++--------------------- 2 files changed, 61 insertions(+), 62 deletions(-) diff --git a/rss_converter_twitter.com.xsl b/rss_converter_twitter.com.xsl index 96ecf04..b67946e 100644 --- a/rss_converter_twitter.com.xsl +++ b/rss_converter_twitter.com.xsl @@ -32,7 +32,7 @@ - + diff --git a/tweeper.php b/tweeper.php index 74dc14f..ef55022 100644 --- a/tweeper.php +++ b/tweeper.php @@ -20,68 +20,10 @@ 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); -- 2.1.4