def parse(self, username, password): driver = Chrome() self.sign_in(username, password, driver) input_data = self.get_input_data() highlights = { item['title']: item['id'] for item in input_data['highlights'] } story_hashtags = { item['hash_tag']: item['id'] for item in input_data['story_hashtags'] } igtv_hashtags = { item['hash_tag']: item['id'] for item in input_data['igtv_hashtags'] } output_data = dict() for user in input_data['users']: driver.get(user['instagram']) request = driver.wait_for_request('include_suggested_users') output_data['stories'] = self.get_stories( user['id'], driver, request, highlights, story_hashtags, ) output_data['igtvs'] = self.get_igtv_data( driver, user, igtv_hashtags, ) self.send_to_app(output_data)
def get_stories(user: AnyStr, driver: Chrome, request: AnyStr, highlights: Dict, hashtags: Dict) -> List: """ :param user: instagram user :param driver: browser :param request: driver request :param highlights: dict of highlights names :param hashtags: dict of hashtags names :return: stories data list """ current_user_data = requests.get(request.url, headers=request.headers).text edges = json.loads( current_user_data)['data']['user']['edge_highlight_reels']['edges'] items = [] for edge in edges: node = edge['node'] title = node['title'] if title not in highlights: continue driver.get( 'https://www.instagram.com/stories/highlights/{}'.format( node['id'])) sleep(randint(2, 4)) request = driver.wait_for_request('show_story_viewer_list') response_data = json.loads( requests.get(request.url, headers=request.headers).text) for item in response_data['data']['reels_media'][0]['items']: video_resources = item.get('video_resources') tappable_objects = item['tappable_objects'] if tappable_objects and tappable_objects[0][ '__typename'] == 'GraphTappableHashtag': hashtag = tappable_objects[0]['name'] if hashtag not in hashtags or not video_resources: continue items.append({ 'hash_tag': hashtags[hashtag], 'highlight': highlights[title], 'user': user, 'url': video_resources[-1]['src'], }) return items