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.

247 lines
7.4 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, false];
  23. }
  24. }
  25. if(!$tweet)
  26. return [false, 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. // Don't include the RT'd photo or video in the main object.
  79. // They get included in the reposted object instead.
  80. if(!property_exists($tweet, 'retweeted_status')) {
  81. // Photos and Videos
  82. if(property_exists($tweet, 'extended_entities') && property_exists($tweet->extended_entities, 'media')) {
  83. foreach($tweet->extended_entities->media as $media) {
  84. if($media->type == 'photo') {
  85. if(!array_key_exists('photo', $entry))
  86. $entry['photo'] = [];
  87. $entry['photo'][] = $media->media_url_https;
  88. } elseif($media->type == 'video') {
  89. if(!array_key_exists('video', $entry))
  90. $entry['video'] = [];
  91. // Find the highest bitrate video that is mp4
  92. $videos = $media->video_info->variants;
  93. $videos = array_filter($videos, function($v) {
  94. return property_exists($v, 'bitrate') && $v->content_type == 'video/mp4';
  95. });
  96. if(count($videos)) {
  97. usort($videos, function($a,$b) {
  98. return $a->bitrate < $b->bitrate;
  99. });
  100. $entry['video'][] = $videos[0]->url;
  101. }
  102. }
  103. }
  104. }
  105. // Place
  106. if(property_exists($tweet, 'place') && $tweet->place) {
  107. $place = $tweet->place;
  108. if($place->place_type == 'city') {
  109. $entry['location'] = $place->url;
  110. $refs[$place->url] = [
  111. 'type' => 'adr',
  112. 'name' => $place->full_name,
  113. 'locality' => $place->name,
  114. 'country-name' => $place->country,
  115. ];
  116. }
  117. }
  118. }
  119. // Quoted Status
  120. if(property_exists($tweet, 'quoted_status')) {
  121. $quoteOf = 'https://twitter.com/' . $tweet->quoted_status->user->screen_name . '/status/' . $tweet->quoted_status_id_str;
  122. list($quoted) = self::parse($quoteOf, $tweet->quoted_status_id_str, null, $tweet->quoted_status);
  123. if(isset($quoted['refs'])) {
  124. foreach($quoted['refs'] as $k=>$v) {
  125. $refs[$k] = $v;
  126. }
  127. }
  128. $refs[$quoteOf] = $quoted['data'];
  129. }
  130. if($author = self::_buildHCardFromTwitterProfile($tweet->user)) {
  131. $entry['author'] = $author;
  132. }
  133. $response = [
  134. 'data' => $entry
  135. ];
  136. if(count($refs)) {
  137. $response['refs'] = $refs;
  138. }
  139. return [$response, $tweet];
  140. }
  141. private static function _buildHCardFromTwitterProfile($profile) {
  142. if(!$profile) return false;
  143. $author = [
  144. 'type' => 'card'
  145. ];
  146. $author['nickname'] = $profile->screen_name;
  147. $author['location'] = $profile->location;
  148. $author['bio'] = self::expandTwitterObjectURLs($profile->description, $profile, 'description');
  149. if($profile->name)
  150. $author['name'] = $profile->name;
  151. else
  152. $author['name'] = $profile->screen_name;
  153. if($profile->url) {
  154. if($profile->entities->url->urls[0]->expanded_url)
  155. $author['url'] = $profile->entities->url->urls[0]->expanded_url;
  156. else
  157. $author['url'] = $profile->entities->url->urls[0]->url;
  158. }
  159. else {
  160. $author['url'] = 'https://twitter.com/' . $profile->screen_name;
  161. }
  162. $author['photo'] = $profile->profile_image_url_https;
  163. return $author;
  164. }
  165. private static function expandTweetURLs($text, $object) {
  166. if(property_exists($object, 'entities') && property_exists($object->entities, 'urls')) {
  167. foreach($object->entities->urls as $url) {
  168. $text = str_replace($url->url, $url->expanded_url, $text);
  169. }
  170. }
  171. return $text;
  172. }
  173. private static function expandTwitterObjectURLs($text, $object, $key) {
  174. if(property_exists($object, 'entities')
  175. && property_exists($object->entities, $key)
  176. && property_exists($object->entities->{$key}, 'urls')) {
  177. foreach($object->entities->{$key}->urls as $url) {
  178. $text = str_replace($url->url, $url->expanded_url, $text);
  179. }
  180. }
  181. return $text;
  182. }
  183. /**
  184. * Converts base 60 to base 10, with error checking
  185. * http://tantek.pbworks.com/NewBase60
  186. * @param string $s
  187. * @return int
  188. */
  189. function b60to10($s)
  190. {
  191. $n = 0;
  192. for($i = 0; $i < strlen($s); $i++) // iterate from first to last char of $s
  193. {
  194. $c = ord($s[$i]); // put current ASCII of char into $c
  195. if ($c>=48 && $c<=57) { $c=bcsub($c,48); }
  196. else if ($c>=65 && $c<=72) { $c=bcsub($c,55); }
  197. else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
  198. else if ($c>=74 && $c<=78) { $c=bcsub($c,56); }
  199. else if ($c==79) { $c=0; } // error correct typo capital O to 0
  200. else if ($c>=80 && $c<=90) { $c=bcsub($c,57); }
  201. else if ($c==95) { $c=34; } // underscore
  202. else if ($c>=97 && $c<=107) { $c=bcsub($c,62); }
  203. else if ($c>=109 && $c<=122) { $c=bcsub($c,63); }
  204. else { $c = 0; } // treat all other noise as 0
  205. $n = bcadd(bcmul(60, $n), $c);
  206. }
  207. return $n;
  208. }
  209. }