From 673daa1842c3c9a9c4b248574523563e4bcfb990 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 25 Sep 2019 15:21:26 -0700 Subject: [PATCH] enable passing a session cookie in instagram requests --- lib/XRay.php | 3 ++- lib/XRay/Fetcher.php | 5 +++++ lib/XRay/Formats/Instagram.php | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/XRay.php b/lib/XRay.php index 9758b24..771bcdd 100644 --- a/lib/XRay.php +++ b/lib/XRay.php @@ -5,7 +5,7 @@ class XRay { public $http; 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=[]) { @@ -40,6 +40,7 @@ class XRay { 'url' => $url, 'code' => $code, ], $opts); + if(!isset($opts['include_original']) || !$opts['include_original']) unset($result['original']); if(!isset($result['url'])) $result['url'] = $url; diff --git a/lib/XRay/Fetcher.php b/lib/XRay/Fetcher.php index 6f22822..c1931e2 100644 --- a/lib/XRay/Fetcher.php +++ b/lib/XRay/Fetcher.php @@ -58,6 +58,11 @@ class Fetcher { 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 // Special-case appspot.com URLs to not follow redirects. diff --git a/lib/XRay/Formats/Instagram.php b/lib/XRay/Formats/Instagram.php index 04dfb2d..a1b06bc 100644 --- a/lib/XRay/Formats/Instagram.php +++ b/lib/XRay/Formats/Instagram.php @@ -15,6 +15,42 @@ class Instagram extends Format { 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=[]) { $html = $http_response['body']; $url = $http_response['url'];