Browse Source

also parse instagram profile URLs

pull/72/head v1.4.26
Aaron Parecki 6 years ago
parent
commit
921d5262ea
No known key found for this signature in database GPG Key ID: 276C2817346D6056
2 changed files with 49 additions and 8 deletions
  1. +33
    -8
      lib/XRay/Formats/Instagram.php
  2. +16
    -0
      tests/InstagramTest.php

+ 33
- 8
lib/XRay/Formats/Instagram.php View File

@ -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;

+ 16
- 0
tests/InstagramTest.php View File

@ -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']);
}
}

Loading…
Cancel
Save