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.

273 lines
7.5 KiB

7 years ago
  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use DOMDocument, DOMXPath;
  4. use DateTime, DateTimeZone;
  5. class Instagram extends Format {
  6. public static function matches_host($url) {
  7. $host = parse_url($url, PHP_URL_HOST);
  8. return in_array($host, ['www.instagram.com','instagram.com']);
  9. }
  10. public static function matches($url) {
  11. return self::matches_host($url);
  12. }
  13. public static function parse($http, $html, $url) {
  14. $photoData = self::_extractPhotoDataFromPhotoPage($html);
  15. if(!$photoData)
  16. return self::_unknown();
  17. // Start building the h-entry
  18. $entry = array(
  19. 'type' => 'entry',
  20. 'url' => $url,
  21. 'author' => [
  22. 'type' => 'card',
  23. 'name' => null,
  24. 'photo' => null,
  25. 'url' => null
  26. ]
  27. );
  28. $profiles = [];
  29. // Fetch profile info for this user
  30. $username = $photoData['owner']['username'];
  31. $profile = self::_getInstagramProfile($username, $http);
  32. if($profile) {
  33. $entry['author'] = self::_buildHCardFromInstagramProfile($profile);
  34. $profiles[] = $profile;
  35. }
  36. // Content and hashtags
  37. $caption = false;
  38. if(isset($photoData['caption'])) {
  39. $caption = $photoData['caption'];
  40. } elseif(isset($photoData['edge_media_to_caption']['edges'][0]['node']['text'])) {
  41. $caption = $photoData['edge_media_to_caption']['edges'][0]['node']['text'];
  42. }
  43. if($caption) {
  44. if(preg_match_all('/#([a-z0-9_-]+)/i', $caption, $matches)) {
  45. $entry['category'] = [];
  46. foreach($matches[1] as $match) {
  47. $entry['category'][] = $match;
  48. }
  49. }
  50. $entry['content'] = [
  51. 'text' => $caption
  52. ];
  53. }
  54. // Include the photo/video media URLs
  55. // (Always return arrays)
  56. if(array_key_exists('display_src', $photoData))
  57. $entry['photo'] = [$photoData['display_src']];
  58. elseif(array_key_exists('display_url', $photoData))
  59. $entry['photo'] = [$photoData['display_url']];
  60. if(array_key_exists('is_video', $photoData) && $photoData['is_video']) {
  61. $entry['video'] = [$photoData['video_url']];
  62. }
  63. $refs = [];
  64. // Find person tags and fetch user profiles
  65. // old json
  66. if(isset($photoData['usertags']['nodes'])) {
  67. if(!isset($entry['category'])) $entry['category'] = [];
  68. foreach($photoData['usertags']['nodes'] as $tag) {
  69. $profile = self::_getInstagramProfile($tag['user']['username'], $http);
  70. if($profile) {
  71. $card = self::_buildHCardFromInstagramProfile($profile);
  72. $entry['category'][] = $card['url'];
  73. $refs[$card['url']] = $card;
  74. $profiles[] = $profile;
  75. }
  76. }
  77. }
  78. // new json as of approximately 2017-04-19
  79. if(isset($photoData['edge_media_to_tagged_user']['edges'])) {
  80. if(!isset($entry['category'])) $entry['category'] = [];
  81. foreach($photoData['edge_media_to_tagged_user']['edges'] as $edge) {
  82. $profile = self::_getInstagramProfile($edge['node']['user']['username'], $http);
  83. if($profile) {
  84. $card = self::_buildHCardFromInstagramProfile($profile);
  85. $entry['category'][] = $card['url'];
  86. $refs[$card['url']] = $card;
  87. $profiles[] = $profile;
  88. }
  89. }
  90. }
  91. // Published date
  92. if(array_key_exists('taken_at_timestamp', $photoData))
  93. $published = DateTime::createFromFormat('U', $photoData['taken_at_timestamp']);
  94. elseif(array_key_exists('date', $photoData))
  95. $published = DateTime::createFromFormat('U', $photoData['date']);
  96. // Include venue data
  97. $locations = [];
  98. if($photoData['location']) {
  99. $location = self::_getInstagramLocation($photoData['location']['id'], $http);
  100. if($location) {
  101. $entry['location'] = [$location['url']];
  102. $refs[$location['url']] = $location;
  103. $locations[] = $location;
  104. // Look up timezone
  105. if($location['latitude']) {
  106. $tz = \p3k\Timezone::timezone_for_location($location['latitude'], $location['longitude']);
  107. if($tz) {
  108. $published->setTimeZone(new DateTimeZone($tz));
  109. }
  110. }
  111. }
  112. }
  113. $entry['published'] = $published->format('c');
  114. if(count($refs)) {
  115. $entry['refs'] = $refs;
  116. }
  117. return [
  118. 'data' => $entry,
  119. 'original' => json_encode([
  120. 'photo' => $photoData,
  121. 'profiles' => $profiles,
  122. 'locations' => $locations
  123. ])
  124. ];
  125. }
  126. private static function _buildHCardFromInstagramProfile($profile) {
  127. if(!$profile) return false;
  128. $author = [
  129. 'type' => 'card'
  130. ];
  131. if($profile['full_name'])
  132. $author['name'] = $profile['full_name'];
  133. else
  134. $author['name'] = $profile['username'];
  135. if(isset($profile['external_url']) && $profile['external_url'])
  136. $author['url'] = $profile['external_url'];
  137. else
  138. $author['url'] = 'https://www.instagram.com/' . $profile['username'];
  139. if(isset($profile['profile_pic_url_hd']))
  140. $author['photo'] = $profile['profile_pic_url_hd'];
  141. else
  142. $author['photo'] = $profile['profile_pic_url'];
  143. return $author;
  144. }
  145. private static function _getInstagramProfile($username, $http) {
  146. $response = $http->get('https://www.instagram.com/'.$username.'/?__a=1');
  147. if(!$response['error']) {
  148. $profile = @json_decode($response['body'], true);
  149. if($profile && array_key_exists('user', $profile)) {
  150. $user = $profile['user'];
  151. return $user;
  152. }
  153. }
  154. return null;
  155. }
  156. private static function _getInstagramLocation($id, $http) {
  157. $igURL = 'https://www.instagram.com/explore/locations/'.$id.'/';
  158. $response = $http->get($igURL);
  159. if($response['body']) {
  160. $data = self::_extractVenueDataFromVenuePage($response['body']);
  161. if($data) {
  162. return [
  163. 'type' => 'card',
  164. 'name' => $data['name'],
  165. 'url' => $igURL,
  166. 'latitude' => $data['lat'],
  167. 'longitude' => $data['lng'],
  168. ];
  169. }
  170. }
  171. return null;
  172. }
  173. private static function _extractPhotoDataFromPhotoPage($html) {
  174. $data = self::_extractIGData($html);
  175. if($data && is_array($data) && array_key_exists('entry_data', $data)) {
  176. if(is_array($data['entry_data']) && array_key_exists('PostPage', $data['entry_data'])) {
  177. $post = $data['entry_data']['PostPage'];
  178. if(isset($post[0]['graphql']['shortcode_media'])) {
  179. return $post[0]['graphql']['shortcode_media'];
  180. } elseif(isset($post[0]['graphql']['media'])) {
  181. return $post[0]['graphql']['media'];
  182. } elseif(isset($post[0]['media'])) {
  183. return $post[0]['media'];
  184. }
  185. }
  186. }
  187. return null;
  188. }
  189. private static function _extractVenueDataFromVenuePage($html) {
  190. $data = self::_extractIGData($html);
  191. if($data && is_array($data) && array_key_exists('entry_data', $data)) {
  192. if(isset($data['entry_data']['LocationsPage'])) {
  193. $data = $data['entry_data']['LocationsPage'];
  194. if(isset($data[0]['location'])) {
  195. $location = $data[0]['location'];
  196. # we don't need these and they're huge, so drop them now
  197. unset($location['media']);
  198. unset($location['top_posts']);
  199. return $location;
  200. }
  201. }
  202. }
  203. return null;
  204. }
  205. private static function _extractIGData($html) {
  206. $doc = new DOMDocument();
  207. @$doc->loadHTML($html);
  208. if(!$doc) {
  209. return null;
  210. }
  211. $xpath = new DOMXPath($doc);
  212. $data = null;
  213. foreach($xpath->query('//script') as $script) {
  214. if(preg_match('/window\._sharedData = ({.+});/', $script->textContent, $match)) {
  215. $data = json_decode($match[1], true);
  216. }
  217. }
  218. return $data;
  219. }
  220. }