From 921d5262eaa2a54a5659b0f360bfd82219dc595f Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 19 Apr 2018 10:23:00 -0700 Subject: [PATCH] also parse instagram profile URLs --- lib/XRay/Formats/Instagram.php | 41 +++++++++++++++++++++++++++------- tests/InstagramTest.php | 16 +++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lib/XRay/Formats/Instagram.php b/lib/XRay/Formats/Instagram.php index eccc667..e543a08 100644 --- a/lib/XRay/Formats/Instagram.php +++ b/lib/XRay/Formats/Instagram.php @@ -16,6 +16,26 @@ class Instagram extends Format { } public static function parse($http, $html, $url) { + if(preg_match('#instagram.com/([^/]+)/$#', $url)) { + return self::parseProfile($http, $html, $url); + } else { + return self::parsePhoto($http, $html, $url); + } + } + + private static function parseProfile($http, $html, $url) { + $profileData = self::_parseProfileFromHTML($html); + if(!$profileData) + return self::_unknown(); + + $card = self::_buildHCardFromInstagramProfile($profileData); + + return [ + 'data' => $card + ]; + } + + private static function parsePhoto($http, $html, $url) { $photoData = self::_extractPhotoDataFromPhotoPage($html); @@ -195,14 +215,19 @@ class Instagram extends Format { private static function _getInstagramProfile($username, $http) { $response = $http->get('https://www.instagram.com/'.$username.'/'); - if(!$response['error']) { - $data = self::_extractIGData($response['body']); - if(isset($data['entry_data']['ProfilePage'][0])) { - $profile = $data['entry_data']['ProfilePage'][0]; - if($profile && isset($profile['graphql']['user'])) { - $user = $profile['graphql']['user']; - return $user; - } + if(!$response['error']) + return self::_parseProfileFromHTML($response['body']); + + return null; + } + + private static function _parseProfileFromHTML($html) { + $data = self::_extractIGData($html); + if(isset($data['entry_data']['ProfilePage'][0])) { + $profile = $data['entry_data']['ProfilePage'][0]; + if($profile && isset($profile['graphql']['user'])) { + $user = $profile['graphql']['user']; + return $user; } } return null; diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index 1a0d157..16cd215 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -165,4 +165,20 @@ class InstagramTest extends PHPUnit_Framework_TestCase { $this->assertEquals(2, count($data['data']['category'])); } + public function testInstagramProfile() { + $url = 'https://www.instagram.com/aaronpk/'; + $response = $this->parse(['url' => $url]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body, true); + + $this->assertSame([ + 'type' => 'card', + 'name' => 'Aaron Parecki', + 'url' => 'https://aaronparecki.com/', + 'photo' => 'https://instagram.fsea1-1.fna.fbcdn.net/vp/0dc6166cbd4ec6782453d36cd07fec06/5B67568E/t51.2885-19/s320x320/14240576_268350536897085_1129715662_a.jpg' + ], $data['data']); + } + }