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.

246 lines
7.4 KiB

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