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.

231 lines
6.9 KiB

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