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.

337 lines
9.6 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, $opts=[]) {
  14. if(preg_match('#instagram.com/([^/]+)/$#', $url)) {
  15. if(isset($opts['expect']) && $opts['expect'] == 'feed')
  16. return self::parseFeed($http, $html, $url);
  17. else
  18. return self::parseProfile($http, $html, $url);
  19. } else {
  20. return self::parsePhoto($http, $html, $url);
  21. }
  22. }
  23. private static function parseProfile($http, $html, $url) {
  24. $profileData = self::_parseProfileFromHTML($html);
  25. if(!$profileData)
  26. return self::_unknown();
  27. $card = self::_buildHCardFromInstagramProfile($profileData);
  28. return [
  29. 'data' => $card
  30. ];
  31. }
  32. private static function parseFeed($http, $html, $url) {
  33. $profileData = self::_parseProfileFromHTML($html);
  34. if(!$profileData)
  35. return self::_unknown();
  36. $photos = $profileData['edge_owner_to_timeline_media']['edges'];
  37. $items = [];
  38. foreach($photos as $photoData) {
  39. $item = self::parsePhotoFromData($http, $photoData['node'],
  40. 'https://www.instagram.com/p/'.$photoData['node']['shortcode'].'/', $profileData);
  41. // Note: Not all the photo info is available in the initial JSON.
  42. // Things like video mp4 URLs and person tags and locations are missing.
  43. // Consumers of the feed will need to fetch the photo permalink in order to get all missing information.
  44. // if($photoData['is_video'])
  45. // $item['data']['video'] = true;
  46. $items[] = $item['data'];
  47. }
  48. return [
  49. 'data' => [
  50. 'type' => 'feed',
  51. 'items' => $items,
  52. ]
  53. ];
  54. }
  55. private static function parsePhoto($http, $html, $url, $profile=false) {
  56. $photoData = self::_extractPhotoDataFromPhotoPage($html);
  57. return self::parsePhotoFromData($http, $photoData, $url, $profile);
  58. }
  59. private static function parsePhotoFromData($http, $photoData, $url, $profile=false) {
  60. if(!$photoData)
  61. return self::_unknown();
  62. // Start building the h-entry
  63. $entry = array(
  64. 'type' => 'entry',
  65. 'url' => $url,
  66. 'author' => [
  67. 'type' => 'card',
  68. 'name' => null,
  69. 'photo' => null,
  70. 'url' => null
  71. ]
  72. );
  73. $profiles = [];
  74. if(!$profile) {
  75. // Fetch profile info for this user
  76. $username = $photoData['owner']['username'];
  77. $profile = self::_getInstagramProfile($username, $http);
  78. if($profile) {
  79. $entry['author'] = self::_buildHCardFromInstagramProfile($profile);
  80. $profiles[] = $profile;
  81. }
  82. } else {
  83. $entry['author'] = self::_buildHCardFromInstagramProfile($profile);
  84. $profiles[] = $profile;
  85. }
  86. // Content and hashtags
  87. $caption = false;
  88. if(isset($photoData['caption'])) {
  89. $caption = $photoData['caption'];
  90. } elseif(isset($photoData['edge_media_to_caption']['edges'][0]['node']['text'])) {
  91. $caption = $photoData['edge_media_to_caption']['edges'][0]['node']['text'];
  92. }
  93. if($caption) {
  94. if(preg_match_all('/#([a-z0-9_-]+)/i', $caption, $matches)) {
  95. $entry['category'] = [];
  96. foreach($matches[1] as $match) {
  97. $entry['category'][] = $match;
  98. }
  99. }
  100. $entry['content'] = [
  101. 'text' => $caption
  102. ];
  103. }
  104. $refs = [];
  105. // Include the photo/video media URLs
  106. // (Always return arrays, even for single images)
  107. if(array_key_exists('edge_sidecar_to_children', $photoData)) {
  108. // Multi-post
  109. // For now, we will only pull photos from multi-posts, and skip videos.
  110. $entry['photo'] = [];
  111. foreach($photoData['edge_sidecar_to_children']['edges'] as $edge) {
  112. $entry['photo'][] = $edge['node']['display_url'];
  113. // Don't need to pull person-tags from here because the main parent object already has them.
  114. }
  115. } else {
  116. // Single photo or video
  117. if(array_key_exists('display_src', $photoData))
  118. $entry['photo'] = [$photoData['display_src']];
  119. elseif(array_key_exists('display_url', $photoData))
  120. $entry['photo'] = [$photoData['display_url']];
  121. if(isset($photoData['is_video']) && $photoData['is_video'] && isset($photoData['video_url'])) {
  122. $entry['video'] = [$photoData['video_url']];
  123. }
  124. }
  125. // Find person tags and fetch user profiles
  126. if(isset($photoData['edge_media_to_tagged_user']['edges'])) {
  127. if(!isset($entry['category'])) $entry['category'] = [];
  128. foreach($photoData['edge_media_to_tagged_user']['edges'] as $edge) {
  129. $profile = self::_getInstagramProfile($edge['node']['user']['username'], $http);
  130. if($profile) {
  131. $card = self::_buildHCardFromInstagramProfile($profile);
  132. $entry['category'][] = $card['url'];
  133. $refs[$card['url']] = $card;
  134. $profiles[] = $profile;
  135. }
  136. }
  137. }
  138. // Published date
  139. if(isset($photoData['taken_at_timestamp']))
  140. $published = DateTime::createFromFormat('U', $photoData['taken_at_timestamp']);
  141. elseif(isset($photoData['date']))
  142. $published = DateTime::createFromFormat('U', $photoData['date']);
  143. // Include venue data
  144. $locations = [];
  145. if(isset($photoData['location'])) {
  146. $location = self::_getInstagramLocation($photoData['location']['id'], $http);
  147. if($location) {
  148. $entry['location'] = [$location['url']];
  149. $refs[$location['url']] = $location;
  150. $locations[] = $location;
  151. // Look up timezone
  152. if($location['latitude']) {
  153. $tz = \p3k\Timezone::timezone_for_location($location['latitude'], $location['longitude']);
  154. if($tz) {
  155. $published->setTimeZone(new DateTimeZone($tz));
  156. }
  157. }
  158. }
  159. }
  160. $entry['published'] = $published->format('c');
  161. if(count($refs)) {
  162. $entry['refs'] = $refs;
  163. }
  164. return [
  165. 'data' => $entry,
  166. 'original' => json_encode([
  167. 'photo' => $photoData,
  168. 'profiles' => $profiles,
  169. 'locations' => $locations
  170. ])
  171. ];
  172. }
  173. private static function _buildHCardFromInstagramProfile($profile) {
  174. if(!$profile) return false;
  175. $author = [
  176. 'type' => 'card'
  177. ];
  178. if($profile['full_name'])
  179. $author['name'] = $profile['full_name'];
  180. else
  181. $author['name'] = $profile['username'];
  182. if(isset($profile['external_url']) && $profile['external_url'])
  183. $author['url'] = $profile['external_url'];
  184. else
  185. $author['url'] = 'https://www.instagram.com/' . $profile['username'];
  186. if(isset($profile['profile_pic_url_hd']))
  187. $author['photo'] = $profile['profile_pic_url_hd'];
  188. else
  189. $author['photo'] = $profile['profile_pic_url'];
  190. if(isset($profile['biography']))
  191. $author['note'] = $profile['biography'];
  192. return $author;
  193. }
  194. private static function _getInstagramProfile($username, $http) {
  195. $response = $http->get('https://www.instagram.com/'.$username.'/');
  196. if(!$response['error'])
  197. return self::_parseProfileFromHTML($response['body']);
  198. return null;
  199. }
  200. private static function _parseProfileFromHTML($html) {
  201. $data = self::_extractIGData($html);
  202. if(isset($data['entry_data']['ProfilePage'][0])) {
  203. $profile = $data['entry_data']['ProfilePage'][0];
  204. if($profile && isset($profile['graphql']['user'])) {
  205. $user = $profile['graphql']['user'];
  206. return $user;
  207. }
  208. }
  209. return null;
  210. }
  211. private static function _getInstagramLocation($id, $http) {
  212. $igURL = 'https://www.instagram.com/explore/locations/'.$id.'/';
  213. $response = $http->get($igURL);
  214. if($response['body']) {
  215. $data = self::_extractVenueDataFromVenuePage($response['body']);
  216. if($data) {
  217. return [
  218. 'type' => 'card',
  219. 'name' => $data['name'],
  220. 'url' => $igURL,
  221. 'latitude' => $data['lat'],
  222. 'longitude' => $data['lng'],
  223. ];
  224. }
  225. }
  226. return null;
  227. }
  228. private static function _extractPhotoDataFromPhotoPage($html) {
  229. $data = self::_extractIGData($html);
  230. if($data && is_array($data) && array_key_exists('entry_data', $data)) {
  231. if(is_array($data['entry_data']) && array_key_exists('PostPage', $data['entry_data'])) {
  232. $post = $data['entry_data']['PostPage'];
  233. if(isset($post[0]['graphql']['shortcode_media'])) {
  234. return $post[0]['graphql']['shortcode_media'];
  235. } elseif(isset($post[0]['graphql']['media'])) {
  236. return $post[0]['graphql']['media'];
  237. } elseif(isset($post[0]['media'])) {
  238. return $post[0]['media'];
  239. }
  240. }
  241. }
  242. return null;
  243. }
  244. private static function _extractVenueDataFromVenuePage($html) {
  245. $data = self::_extractIGData($html);
  246. if($data && isset($data['entry_data']['LocationsPage'])) {
  247. $data = $data['entry_data']['LocationsPage'];
  248. if(isset($data[0]['graphql']['location'])) {
  249. $location = $data[0]['graphql']['location'];
  250. # we don't need these and they're huge, so drop them now
  251. unset($location['media']);
  252. unset($location['top_posts']);
  253. return $location;
  254. }
  255. }
  256. return null;
  257. }
  258. private static function _extractIGData($html) {
  259. $doc = new DOMDocument();
  260. @$doc->loadHTML($html);
  261. if(!$doc) {
  262. return null;
  263. }
  264. $xpath = new DOMXPath($doc);
  265. $data = null;
  266. foreach($xpath->query('//script') as $script) {
  267. if(preg_match('/window\._sharedData = ({.+});/', $script->textContent, $match)) {
  268. $data = json_decode($match[1], true);
  269. }
  270. }
  271. return $data;
  272. }
  273. }