Skip to content

Commit

Permalink
add generating and using x-instagram-gis and new limits
Browse files Browse the repository at this point in the history
  • Loading branch information
kricha committed Apr 22, 2018
1 parent 8dbec82 commit 2435e6c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions insta_browser/meter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import _md5
import json
import re
from urllib.parse import quote
Expand Down Expand Up @@ -52,6 +53,10 @@ def __init__(self, username, callback=None):
'Cookie': 'ig_pr=2;',
}
self.client.headers.update(self.headers)
m = re.search(r'"rhx_gis":"([A-z0-9]+)"', self.client.get('https://instagram.com/').text)
if m is None:
raise Exception("STOP!")
self.rhx_gis = m.group(1)

def analyze_profile(self):
try:
Expand All @@ -77,10 +82,14 @@ def analyze_profile(self):
'top_posts_viewed': self.top_posts_viewed,
}, ensure_ascii=False)

def __update_instagram_gis(self, params):
instagram_gis = _md5.md5('{}:{}'.format(self.rhx_gis, params).encode("utf-8")).hexdigest()
self.client.headers.update({'x-instagram-gis': instagram_gis})

def __get_profile_first_posts(self):
url = self.__profile_fp_url.format(self.username)
self.__update_instagram_gis('/{}/'.format(self.username))
data = self.client.request('get', url).json()

user_data = data['graphql']
self.user['un'] = self.username
self.user['id'] = user_data['user']['id']
Expand Down Expand Up @@ -190,13 +199,14 @@ def __calculate_per_post_counters(self):
def __request_for_rest_loop(self):
var_json = {
'id': self.user['id'],
'first': 500 if self.user[COUNTERS_KEY][COUNT_KEY_POSTS] > 500 else self.user[COUNTERS_KEY][
COUNT_KEY_POSTS] - 12,
'first': 50 if self.user[COUNTERS_KEY][COUNT_KEY_POSTS] > 50 else self.user[COUNTERS_KEY][
COUNT_KEY_POSTS] - 12,
}
if self.__tmp_req_info['has_next_page']:
var_json.update({'after': self.__tmp_req_info['end_cursor']})
variable = json.dumps(var_json).replace(' ', '')
url = self.__profile_rp_url.format(quote(variable))
variables = json.dumps(var_json, separators=(',', ':'))
url = self.__profile_rp_url.format(quote(variables))
self.__update_instagram_gis(variables)
data = self.client.request('get', url).json()
self.__tmp_data = data['data']['user']['edge_owner_to_timeline_media']['edges']
self.__tmp_req_info = data['data']['user']['edge_owner_to_timeline_media']['page_info']
Expand Down
2 changes: 1 addition & 1 deletion insta_browser/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.8.5.2'
__version__ = '0.8.5.3'

0 comments on commit 2435e6c

Please sign in to comment.