X-Git-Url: https://git.ao2.it/tweeper.git/blobdiff_plain/2efcaf768f68d35872c0d06136279e673128c46f..af103c976dd4992d79e9d9a71837aecff30d6e9c:/src/Tweeper.php?ds=inline diff --git a/src/Tweeper.php b/src/Tweeper.php index 7ecbf2f..5b9f235 100644 --- a/src/Tweeper.php +++ b/src/Tweeper.php @@ -36,7 +36,7 @@ date_default_timezone_set('UTC'); */ class Tweeper { - private static $userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0"; + private static $userAgent = "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J)"; private static $maxConnectionTimeout = 5; private static $maxConnectionRetries = 5; @@ -53,11 +53,14 @@ class Tweeper { * Enables showing multimedia content (images, videos) directly in the * item description (enabled by default). Only some stylesheets supports * this functionality (twitter, instagram, dilbert). + * @param bool $verbose_output + * Enables showing non-fatal errors like XML parsing errors. */ - public function __construct($generate_enclosure = FALSE, $show_usernames = TRUE, $show_multimedia = TRUE) { + public function __construct($generate_enclosure = FALSE, $show_usernames = TRUE, $show_multimedia = TRUE, $verbose_output = TRUE) { $this->generate_enclosure = $generate_enclosure; $this->show_usernames = $show_usernames; $this->show_multimedia = $show_multimedia; + $this->verbose_output = $verbose_output; } /** @@ -84,6 +87,46 @@ class Tweeper { } /** + * Convert Twitter mobile date to the date format expected in a RSS document. + */ + public static function twitterToRssDate($date) { + // Twitter uses relative timestamps in minutes for recent tweets. + if (preg_match('/^(\d+)m$/', $date, $matches)) { + $timestamp = strtotime("+" . $matches[1] . " min", time()); + if (FALSE === $timestamp) { + $timestamp = 0; + } + } + else { + /* + * In case the time is specified put it after the date, + * to make it recognized by strptime(). + */ + if (preg_match('/(.*) - (.*)/', $date, $matches)) { + $date = $matches[2] . " " . $matches[1]; + } + + $timestamp = strtotime($date); + if (FALSE === $timestamp) { + $timestamp = 0; + } + + /* + * The twitter mobile UI usually only specifies the month and the day, so + * strtotime($date) may interpret the date as being in the future. + * + * If the date is in the future it is probably in the same day but in the + * previous year. + */ + if ($timestamp > time()) { + $timestamp = strtotime('-1 years', $timestamp); + } + } + + return Tweeper::epochToRssDate($timestamp); + } + + /** * Convert string to UpperCamelCase. */ public static function toUpperCamelCase($str, $delim = ' ') { @@ -123,8 +166,6 @@ class Tweeper { CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_COOKIEFILE => "", CURLOPT_RETURNTRANSFER => TRUE, - CURLOPT_SSL_VERIFYHOST => FALSE, - CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_HTTPHEADER => array('Accept-language: en'), CURLOPT_USERAGENT => Tweeper::$userAgent, )); @@ -146,8 +187,6 @@ class Tweeper { // Follow http redirects to get the real URL. CURLOPT_FOLLOWLOCATION => TRUE, CURLOPT_RETURNTRANSFER => TRUE, - CURLOPT_SSL_VERIFYHOST => FALSE, - CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_USERAGENT => Tweeper::$userAgent, )); @@ -343,8 +382,10 @@ class Tweeper { $xmlDoc->loadHTML($html); } - foreach (libxml_get_errors() as $xml_error) { - Tweeper::logXmlError($xml_error); + if ($this->verbose_output) { + foreach (libxml_get_errors() as $xml_error) { + Tweeper::logXmlError($xml_error); + } } libxml_clear_errors(); libxml_use_internal_errors($xml_errors_value);