Browse Source

enable passing a session cookie in instagram requests

pull/94/head
Aaron Parecki 5 years ago
parent
commit
673daa1842
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 43 additions and 1 deletions
  1. +2
    -1
      lib/XRay.php
  2. +5
    -0
      lib/XRay/Fetcher.php
  3. +36
    -0
      lib/XRay/Formats/Instagram.php

+ 2
- 1
lib/XRay.php View File

@ -5,7 +5,7 @@ class XRay {
public $http; public $http;
public function __construct() { public function __construct() {
$this->http = new HTTP();
$this->http = new HTTP('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 p3k/XRay');
} }
public function rels($url, $opts=[]) { public function rels($url, $opts=[]) {
@ -40,6 +40,7 @@ class XRay {
'url' => $url, 'url' => $url,
'code' => $code, 'code' => $code,
], $opts); ], $opts);
if(!isset($opts['include_original']) || !$opts['include_original']) if(!isset($opts['include_original']) || !$opts['include_original'])
unset($result['original']); unset($result['original']);
if(!isset($result['url'])) $result['url'] = $url; if(!isset($result['url'])) $result['url'] = $url;

+ 5
- 0
lib/XRay/Fetcher.php View File

@ -58,6 +58,11 @@ class Fetcher {
return Formats\Hackernews::fetch($this->http, $url, $opts); return Formats\Hackernews::fetch($this->http, $url, $opts);
} }
// Check if this is an Instagram URL and enable passing a session cookie
if(Formats\Instagram::matches($url)) {
return Formats\Instagram::fetch($this->http, $url, $opts);
}
// All other URLs are fetched normally // All other URLs are fetched normally
// Special-case appspot.com URLs to not follow redirects. // Special-case appspot.com URLs to not follow redirects.

+ 36
- 0
lib/XRay/Formats/Instagram.php View File

@ -15,6 +15,42 @@ class Instagram extends Format {
return self::matches_host($url); return self::matches_host($url);
} }
public static function fetch($http, $url, $opts=[]) {
if(!self::matches($url))
return false;
$headers = [];
if(isset($opts['instagram_session']) && $opts['instagram_session'])
$headers[] = 'Cookie: sessionid='.$opts['instagram_session'];
$result = $http->get($url, $headers);
// Check for errors such as getting redirected to the login page or getting rate limiited
/*
// TODO
if(false) {
return [
'error' => 'rate_limited',
'error_description' => 'Instagram has rate limited this client. Please try again later.',
'url' => $result['url'],
'code' => $result['code'],
];
}
if(false) {
return [
'error' => 'unauthorized',
'error_description' => 'Instagram redirected to the login page. Either this user is private, or the client has been rate limited.',
'url' => $result['url'],
'code' => $result['code'],
];
}
*/
return $result;
}
public static function parse($http, $http_response, $opts=[]) { public static function parse($http, $http_response, $opts=[]) {
$html = $http_response['body']; $html = $http_response['body'];
$url = $http_response['url']; $url = $http_response['url'];

Loading…
Cancel
Save