Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def related_users(self, endpoint, user):
- req = InstagramAuthRequest()
- session = requests.Session()
- url = 'https://www.instagram.com/%s/' % user.username
- # response = req.authorized_request('get', url=url)
- response = session.get(url=url)
- csrf_token = req.get_csrf_token_from_content(response.content)
- headers = {
- 'Referer': url,
- 'X-CSRFToken': csrf_token,
- 'Cookie': self.cookies.replace('CSRF_TOKEN', csrf_token),
- }
- user_id = user.id
- limit = 1000
- method = 'first'
- args = limit
- next_page = True
- while next_page:
- graphql = '''
- ig_user(%(user_id)s) {
- %(endpoint)s.%(method)s(%(args)s) {
- count, page_info { end_cursor, has_next_page },
- nodes { id, is_verified, followed_by_viewer, requested_by_viewer, full_name, profile_pic_url, username }
- }
- }''' % locals()
- # response = req.authorized_request('post', url=self.url, data={'q': graphql}, headers=headers)
- response = session.post(url=self.url, data={'q': graphql}, headers=headers)
- json_response = json.loads(response.content)
- if json_response['status'] == 'fail' \
- and json_response['message'] == 'Sorry, too many requests. Please try again later.':
- time.sleep(1)
- continue
- method = 'after'
- try:
- args = '%s, %s' % (json_response[endpoint]['page_info']['end_cursor'], limit)
- next_page = json_response[endpoint]['page_info']['has_next_page']
- # print json_response[endpoint]['count'], len(json_response[endpoint]['nodes']), next_page
- yield json_response[endpoint]['nodes']
- except KeyError:
- raise Exception('Unexpected response: "%s" of graphql request: "%s"' % (json_response, graphql))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement