get the JSON data from the graphql URL output the amount of segments we can watch
(browser, elem, action_type, logger, simulate=False)
| 22 | |
| 23 | |
| 24 | def get_story_data(browser, elem, action_type, logger, simulate=False): |
| 25 | """ |
| 26 | get the JSON data from the graphql URL |
| 27 | output the amount of segments we can watch |
| 28 | """ |
| 29 | |
| 30 | # if things change in the future, modify here: |
| 31 | query_hash = "cda12de4f7fd3719c0569ce03589f4c4" |
| 32 | elem_id = "" |
| 33 | |
| 34 | if action_type == "user": |
| 35 | try: |
| 36 | reel_id = browser.execute_script( |
| 37 | "return window.__additionalData[Object.keys(window.__additionalData)[0]].data.graphql.user.id" |
| 38 | ) |
| 39 | # correct formating for elem_id |
| 40 | elem_id = '"' + reel_id + '"' |
| 41 | # and elem needs to be nothing |
| 42 | elem = "" |
| 43 | except WebDriverException: |
| 44 | try: |
| 45 | reel_id = browser.execute_script( |
| 46 | "return window._sharedData." |
| 47 | "entry_data.ProfilePage[0]." |
| 48 | "graphql.user.id" |
| 49 | ) |
| 50 | # correct formatting for elem_id |
| 51 | elem_id = '"' + reel_id + '"' |
| 52 | # and elem needs to be nothing |
| 53 | elem = "" |
| 54 | except WebDriverException: |
| 55 | logger.error( |
| 56 | "--> Sorry, this page isn't available!\t~either " |
| 57 | + "link is broken or page is removed\n" |
| 58 | ) |
| 59 | return {"status": "not_ok", "reels_cnt": 0} |
| 60 | else: |
| 61 | reel_id = "tag:{}".format(elem) |
| 62 | |
| 63 | graphql_query_url = ( |
| 64 | "https://www.instagram.com/graphql/query/?query_hash={}" |
| 65 | '&variables={{"reel_ids":[{}],"tag_names":["{}"],"location_ids":[],' |
| 66 | '"highlight_reel_ids":[],"precomposed_overlay":false,"show_story_viewer_list":true,' |
| 67 | '"story_viewer_fetch_count":50,"story_viewer_cursor":"",' |
| 68 | '"stories_video_dash_manifest":false}}'.format(query_hash, elem_id, elem) |
| 69 | ) |
| 70 | |
| 71 | cookies = browser.get_cookies() |
| 72 | session = requests.Session() |
| 73 | csrftoken = "" |
| 74 | |
| 75 | # prepare the cookies for the requests session |
| 76 | for cookie in cookies: |
| 77 | all_args = { |
| 78 | "name": cookie["name"], |
| 79 | "value": cookie["value"], |
| 80 | "domain": cookie["domain"], |
| 81 | "secure": cookie["secure"], |
no test coverage detected