You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
8.1 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use DateTime, DateTimeZone;
  4. class Twitter extends Format {
  5. public static function matches_host($url) {
  6. $host = parse_url($url, PHP_URL_HOST);
  7. return in_array($host, ['mobile.twitter.com','twitter.com','www.twitter.com','twtr.io']);
  8. }
  9. public static function matches($url) {
  10. if(preg_match('/https?:\/\/(?:mobile\.twitter\.com|twitter\.com|twtr\.io)\/(?:[a-z0-9_\/!#]+statuse?s?\/([0-9]+)|([a-zA-Z0-9_]+))/i', $url, $match))
  11. return $match;
  12. else
  13. return false;
  14. }
  15. public static function fetch($url, $creds) {
  16. if(!($match = self::matches($url))) {
  17. return false;
  18. }
  19. $tweet_id = $match[1];
  20. $host = parse_url($url, PHP_URL_HOST);
  21. if($host == 'twtr.io') {
  22. $tweet_id = self::b60to10($tweet_id);
  23. }
  24. $twitter = new \Twitter($creds['twitter_api_key'], $creds['twitter_api_secret'], $creds['twitter_access_token'], $creds['twitter_access_token_secret']);
  25. try {
  26. $tweet = $twitter->request('statuses/show/'.$tweet_id, 'GET', ['tweet_mode'=>'extended']);
  27. } catch(\TwitterException $e) {
  28. return [
  29. 'error' => 'twitter_error',
  30. 'error_description' => $e->getMessage()
  31. ];
  32. }
  33. return [
  34. 'url' => $url,
  35. 'body' => $tweet,
  36. 'code' => 200,
  37. ];
  38. }
  39. public static function parse($json, $url) {
  40. if(is_string($json))
  41. $tweet = json_decode($json);
  42. else
  43. $tweet = $json;
  44. if(!$tweet) {
  45. return self::_unknown();
  46. }
  47. $entry = array(
  48. 'type' => 'entry',
  49. 'url' => $url,
  50. 'author' => [
  51. 'type' => 'card',
  52. 'name' => null,
  53. 'nickname' => null,
  54. 'photo' => null,
  55. 'url' => null
  56. ]
  57. );
  58. $refs = [];
  59. // Only use the "display" segment of the text
  60. $text = mb_substr($tweet->full_text,
  61. $tweet->display_text_range[0],
  62. $tweet->display_text_range[1]-$tweet->display_text_range[0],
  63. 'UTF-8');
  64. if(property_exists($tweet, 'retweeted_status')) {
  65. // No content for retweets
  66. $reposted = $tweet->retweeted_status;
  67. $repostOf = 'https://twitter.com/' . $reposted->user->screen_name . '/status/' . $reposted->id_str;
  68. $entry['repost-of'] = $repostOf;
  69. $repostedEntry = self::parse($reposted, $repostOf);
  70. if(isset($repostedEntry['data']['refs'])) {
  71. foreach($repostedEntry['data']['refs'] as $k=>$v) {
  72. $refs[$k] = $v;
  73. }
  74. }
  75. $refs[$repostOf] = $repostedEntry['data'];
  76. } else {
  77. // Twitter escapes & as &amp; in the text
  78. $text = html_entity_decode($text);
  79. $text = self::expandTweetURLs($text, $tweet);
  80. $entry['content'] = ['text' => $text];
  81. }
  82. // Published date
  83. $published = new DateTime($tweet->created_at);
  84. if(property_exists($tweet->user, 'utc_offset')) {
  85. $tz = new DateTimeZone(sprintf('%+d', $tweet->user->utc_offset / 3600));
  86. $published->setTimeZone($tz);
  87. }
  88. $entry['published'] = $published->format('c');
  89. // Hashtags
  90. if(property_exists($tweet, 'entities') && property_exists($tweet->entities, 'hashtags')) {
  91. if(count($tweet->entities->hashtags)) {
  92. $entry['category'] = [];
  93. foreach($tweet->entities->hashtags as $hashtag) {
  94. $entry['category'][] = $hashtag->text;
  95. }
  96. }
  97. }
  98. // Don't include the RT'd photo or video in the main object.
  99. // They get included in the reposted object instead.
  100. if(!property_exists($tweet, 'retweeted_status')) {
  101. // Photos and Videos
  102. if(property_exists($tweet, 'extended_entities') && property_exists($tweet->extended_entities, 'media')) {
  103. foreach($tweet->extended_entities->media as $media) {
  104. if($media->type == 'photo') {
  105. if(!array_key_exists('photo', $entry))
  106. $entry['photo'] = [];
  107. $entry['photo'][] = $media->media_url_https;
  108. } elseif($media->type == 'video') {
  109. if(!array_key_exists('video', $entry))
  110. $entry['video'] = [];
  111. // Find the highest bitrate video that is mp4
  112. $videos = $media->video_info->variants;
  113. $videos = array_filter($videos, function($v) {
  114. return property_exists($v, 'bitrate') && $v->content_type == 'video/mp4';
  115. });
  116. if(count($videos)) {
  117. usort($videos, function($a,$b) {
  118. return $a->bitrate < $b->bitrate;
  119. });
  120. $entry['video'][] = $videos[0]->url;
  121. }
  122. }
  123. }
  124. }
  125. // Place
  126. if(property_exists($tweet, 'place') && $tweet->place) {
  127. $place = $tweet->place;
  128. if($place->place_type == 'city') {
  129. $entry['location'] = $place->url;
  130. $refs[$place->url] = [
  131. 'type' => 'adr',
  132. 'name' => $place->full_name,
  133. 'locality' => $place->name,
  134. 'country-name' => $place->country,
  135. ];
  136. }
  137. }
  138. }
  139. // Quoted Status
  140. if(property_exists($tweet, 'quoted_status')) {
  141. $quoteOf = 'https://twitter.com/' . $tweet->quoted_status->user->screen_name . '/status/' . $tweet->quoted_status_id_str;
  142. $quotedEntry = self::parse($tweet->quoted_status, $quoteOf);
  143. if(isset($quotedEntry['data']['refs'])) {
  144. foreach($quotedEntry['data']['refs'] as $k=>$v) {
  145. $refs[$k] = $v;
  146. }
  147. }
  148. $refs[$quoteOf] = $quotedEntry['data'];
  149. $entry['quotation-of'] = $quoteOf;
  150. }
  151. if($author = self::_buildHCardFromTwitterProfile($tweet->user)) {
  152. $entry['author'] = $author;
  153. }
  154. if(count($refs)) {
  155. $entry['refs'] = $refs;
  156. }
  157. return [
  158. 'data' => $entry,
  159. 'original' => $tweet,
  160. ];
  161. }
  162. private static function _buildHCardFromTwitterProfile($profile) {
  163. if(!$profile) return false;
  164. $author = [
  165. 'type' => 'card'
  166. ];
  167. $author['nickname'] = $profile->screen_name;
  168. $author['location'] = $profile->location;
  169. $author['bio'] = self::expandTwitterObjectURLs($profile->description, $profile, 'description');
  170. if($profile->name)
  171. $author['name'] = $profile->name;
  172. else
  173. $author['name'] = $profile->screen_name;
  174. if($profile->url) {
  175. if($profile->entities->url->urls[0]->expanded_url)
  176. $author['url'] = $profile->entities->url->urls[0]->expanded_url;
  177. else
  178. $author['url'] = $profile->entities->url->urls[0]->url;
  179. }
  180. else {
  181. $author['url'] = 'https://twitter.com/' . $profile->screen_name;
  182. }
  183. $author['photo'] = $profile->profile_image_url_https;
  184. return $author;
  185. }
  186. private static function expandTweetURLs($text, $object) {
  187. if(property_exists($object, 'entities') && property_exists($object->entities, 'urls')) {
  188. foreach($object->entities->urls as $url) {
  189. $text = str_replace($url->url, $url->expanded_url, $text);
  190. }
  191. }
  192. return $text;
  193. }
  194. private static function expandTwitterObjectURLs($text, $object, $key) {
  195. if(property_exists($object, 'entities')
  196. && property_exists($object->entities, $key)
  197. && property_exists($object->entities->{$key}, 'urls')) {
  198. foreach($object->entities->{$key}->urls as $url) {
  199. $text = str_replace($url->url, $url->expanded_url, $text);
  200. }
  201. }
  202. return $text;
  203. }
  204. /**
  205. * Converts base 60 to base 10, with error checking
  206. * http://tantek.pbworks.com/NewBase60
  207. * @param string $s
  208. * @return int
  209. */
  210. function b60to10($s)
  211. {
  212. $n = 0;
  213. for($i = 0; $i < strlen($s); $i++) // iterate from first to last char of $s
  214. {
  215. $c = ord($s[$i]); // put current ASCII of char into $c
  216. if ($c>=48 && $c<=57) { $c=bcsub($c,48); }
  217. else if ($c>=65 && $c<=72) { $c=bcsub($c,55); }
  218. else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
  219. else if ($c>=74 && $c<=78) { $c=bcsub($c,56); }
  220. else if ($c==79) { $c=0; } // error correct typo capital O to 0
  221. else if ($c>=80 && $c<=90) { $c=bcsub($c,57); }
  222. else if ($c==95) { $c=34; } // underscore
  223. else if ($c>=97 && $c<=107) { $c=bcsub($c,62); }
  224. else if ($c>=109 && $c<=122) { $c=bcsub($c,63); }
  225. else { $c = 0; } // treat all other noise as 0
  226. $n = bcadd(bcmul(60, $n), $c);
  227. }
  228. return $n;
  229. }
  230. }