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.

319 lines
9.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  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. if(property_exists($tweet, 'retweeted_status')) {
  60. // No content for retweets
  61. $reposted = $tweet->retweeted_status;
  62. $repostOf = 'https://twitter.com/' . $reposted->user->screen_name . '/status/' . $reposted->id_str;
  63. $entry['repost-of'] = $repostOf;
  64. $repostedEntry = self::parse($reposted, $repostOf);
  65. if(isset($repostedEntry['data']['refs'])) {
  66. foreach($repostedEntry['data']['refs'] as $k=>$v) {
  67. $refs[$k] = $v;
  68. }
  69. }
  70. $refs[$repostOf] = $repostedEntry['data'];
  71. } else {
  72. $entry['content'] = self::expandTweetContent($tweet);
  73. }
  74. // Published date
  75. $published = new DateTime($tweet->created_at);
  76. if(property_exists($tweet->user, 'utc_offset')) {
  77. $tz = new DateTimeZone(sprintf('%+d', $tweet->user->utc_offset / 3600));
  78. $published->setTimeZone($tz);
  79. }
  80. $entry['published'] = $published->format('c');
  81. // Hashtags
  82. if(property_exists($tweet, 'entities') && property_exists($tweet->entities, 'hashtags')) {
  83. if(count($tweet->entities->hashtags)) {
  84. $entry['category'] = [];
  85. foreach($tweet->entities->hashtags as $hashtag) {
  86. $entry['category'][] = $hashtag->text;
  87. }
  88. }
  89. }
  90. // Don't include the RT'd photo or video in the main object.
  91. // They get included in the reposted object instead.
  92. if(!property_exists($tweet, 'retweeted_status')) {
  93. // Photos and Videos
  94. if(property_exists($tweet, 'extended_entities') && property_exists($tweet->extended_entities, 'media')) {
  95. foreach($tweet->extended_entities->media as $media) {
  96. if($media->type == 'photo') {
  97. if(!array_key_exists('photo', $entry))
  98. $entry['photo'] = [];
  99. $entry['photo'][] = $media->media_url_https;
  100. } elseif($media->type == 'video') {
  101. if(!array_key_exists('video', $entry))
  102. $entry['video'] = [];
  103. // Find the highest bitrate video that is mp4
  104. $videos = $media->video_info->variants;
  105. $videos = array_filter($videos, function($v) {
  106. return property_exists($v, 'bitrate') && $v->content_type == 'video/mp4';
  107. });
  108. if(count($videos)) {
  109. usort($videos, function($a,$b) {
  110. return $a->bitrate < $b->bitrate;
  111. });
  112. $entry['video'][] = $videos[0]->url;
  113. }
  114. }
  115. }
  116. }
  117. // Place
  118. if(property_exists($tweet, 'place') && $tweet->place) {
  119. $place = $tweet->place;
  120. if($place->place_type == 'city') {
  121. $entry['location'] = $place->url;
  122. $refs[$place->url] = [
  123. 'type' => 'adr',
  124. 'name' => $place->full_name,
  125. 'locality' => $place->name,
  126. 'country-name' => $place->country,
  127. ];
  128. }
  129. }
  130. }
  131. // Quoted Status
  132. if(property_exists($tweet, 'quoted_status')) {
  133. $quoteOf = 'https://twitter.com/' . $tweet->quoted_status->user->screen_name . '/status/' . $tweet->quoted_status_id_str;
  134. $quotedEntry = self::parse($tweet->quoted_status, $quoteOf);
  135. if(isset($quotedEntry['data']['refs'])) {
  136. foreach($quotedEntry['data']['refs'] as $k=>$v) {
  137. $refs[$k] = $v;
  138. }
  139. }
  140. $refs[$quoteOf] = $quotedEntry['data'];
  141. $entry['quotation-of'] = $quoteOf;
  142. }
  143. if($author = self::_buildHCardFromTwitterProfile($tweet->user)) {
  144. $entry['author'] = $author;
  145. }
  146. if(count($refs)) {
  147. $entry['refs'] = $refs;
  148. }
  149. return [
  150. 'data' => $entry,
  151. 'original' => $tweet,
  152. ];
  153. }
  154. private static function _buildHCardFromTwitterProfile($profile) {
  155. if(!$profile) return false;
  156. $author = [
  157. 'type' => 'card'
  158. ];
  159. $author['nickname'] = $profile->screen_name;
  160. $author['location'] = $profile->location;
  161. $author['bio'] = self::expandTwitterObjectURLs($profile->description, $profile, 'description');
  162. if($profile->name)
  163. $author['name'] = $profile->name;
  164. else
  165. $author['name'] = $profile->screen_name;
  166. if($profile->url) {
  167. if(property_exists($profile, 'entities')) {
  168. if($profile->entities->url->urls[0]->expanded_url)
  169. $author['url'] = $profile->entities->url->urls[0]->expanded_url;
  170. else
  171. $author['url'] = $profile->entities->url->urls[0]->url;
  172. } else {
  173. $author['url'] = $profile->url;
  174. }
  175. }
  176. else {
  177. $author['url'] = 'https://twitter.com/' . $profile->screen_name;
  178. }
  179. $author['photo'] = $profile->profile_image_url_https;
  180. return $author;
  181. }
  182. private static function expandTweetContent($tweet) {
  183. $entities = new \StdClass;
  184. if(property_exists($tweet, 'truncated') && $tweet->truncated) {
  185. if(property_exists($tweet, 'extended_tweet')) {
  186. $text = $tweet->extended_tweet->full_text;
  187. if(property_exists($tweet->extended_tweet, 'entities')) {
  188. $entities = $tweet->extended_tweet->entities;
  189. }
  190. } else {
  191. $text = $tweet->text;
  192. if(property_exists($tweet, 'entities')) {
  193. $entities = $tweet->entities;
  194. }
  195. }
  196. } else {
  197. // Only use the "display" segment of the text
  198. if(property_exists($tweet, 'full_text')) {
  199. // Only use the "display" segment of the text
  200. $text = mb_substr($tweet->full_text,
  201. $tweet->display_text_range[0],
  202. $tweet->display_text_range[1]-$tweet->display_text_range[0],
  203. 'UTF-8');
  204. } else {
  205. $text = $tweet->text;
  206. }
  207. if(property_exists($tweet, 'entities')) {
  208. $entities = $tweet->entities;
  209. }
  210. }
  211. // Twitter escapes & as &amp; in the text
  212. $text = html_entity_decode($text);
  213. $html = $text;
  214. if(property_exists($entities, 'user_mentions')) {
  215. foreach($entities->user_mentions as $user) {
  216. $html = str_replace('@'.$user->screen_name, '<a href="https://twitter.com/'.$user->screen_name.'">@'.$user->screen_name.'</a>', $html);
  217. }
  218. }
  219. if(property_exists($entities, 'urls')) {
  220. foreach($entities->urls as $url) {
  221. $text = str_replace($url->url, $url->expanded_url, $text);
  222. $html = str_replace($url->url, '<a href="'.$url->expanded_url.'">'.$url->expanded_url.'</a>', $html);
  223. }
  224. }
  225. $content = [
  226. 'text' => $text,
  227. ];
  228. if($html != $text)
  229. $content['html'] = $html;
  230. return $content;
  231. }
  232. private static function expandTwitterObjectURLs($text, $object, $key) {
  233. if(property_exists($object, 'entities')
  234. && property_exists($object->entities, $key)
  235. && property_exists($object->entities->{$key}, 'urls')) {
  236. foreach($object->entities->{$key}->urls as $url) {
  237. $text = str_replace($url->url, $url->expanded_url, $text);
  238. }
  239. }
  240. return $text;
  241. }
  242. /**
  243. * Converts base 60 to base 10, with error checking
  244. * http://tantek.pbworks.com/NewBase60
  245. * @param string $s
  246. * @return int
  247. */
  248. function b60to10($s)
  249. {
  250. $n = 0;
  251. for($i = 0; $i < strlen($s); $i++) // iterate from first to last char of $s
  252. {
  253. $c = ord($s[$i]); // put current ASCII of char into $c
  254. if ($c>=48 && $c<=57) { $c=bcsub($c,48); }
  255. else if ($c>=65 && $c<=72) { $c=bcsub($c,55); }
  256. else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
  257. else if ($c>=74 && $c<=78) { $c=bcsub($c,56); }
  258. else if ($c==79) { $c=0; } // error correct typo capital O to 0
  259. else if ($c>=80 && $c<=90) { $c=bcsub($c,57); }
  260. else if ($c==95) { $c=34; } // underscore
  261. else if ($c>=97 && $c<=107) { $c=bcsub($c,62); }
  262. else if ($c>=109 && $c<=122) { $c=bcsub($c,63); }
  263. else { $c = 0; } // treat all other noise as 0
  264. $n = bcadd(bcmul(60, $n), $c);
  265. }
  266. return $n;
  267. }
  268. }