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.

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