Fetches the number of links specified by amount and returns a list of links
(
browser, location, amount, logger, media=None, skip_top_posts=True
)
| 98 | |
| 99 | |
| 100 | def get_links_for_location( |
| 101 | browser, location, amount, logger, media=None, skip_top_posts=True |
| 102 | ): |
| 103 | """ |
| 104 | Fetches the number of links specified by amount and returns a list of links |
| 105 | """ |
| 106 | |
| 107 | if media is None: |
| 108 | # All known media types |
| 109 | media = MEDIA_ALL_TYPES |
| 110 | elif media == MEDIA_PHOTO: |
| 111 | # Include posts with multiple images in it |
| 112 | media = [MEDIA_PHOTO, MEDIA_CAROUSEL] |
| 113 | else: |
| 114 | # Make it an array to use it in the following part |
| 115 | media = [media] |
| 116 | |
| 117 | location_link = "https://www.instagram.com/explore/locations/{}".format(location) |
| 118 | web_address_navigator(browser, location_link) |
| 119 | |
| 120 | top_elements = browser.find_element( |
| 121 | By.XPATH, read_xpath(get_links_for_location.__name__, "top_elements") |
| 122 | ) |
| 123 | top_posts = top_elements.find_elements(By.TAG_NAME, "a") |
| 124 | sleep(1) |
| 125 | |
| 126 | if skip_top_posts: |
| 127 | main_elem = browser.find_element( |
| 128 | By.XPATH, read_xpath(get_links_for_location.__name__, "main_elem") |
| 129 | ) |
| 130 | else: |
| 131 | main_elem = browser.find_element(By.TAG_NAME, "main") |
| 132 | |
| 133 | link_elems = main_elem.find_elements(By.TAG_NAME, "a") |
| 134 | sleep(1) |
| 135 | |
| 136 | if not link_elems: # this location does not have `Top Posts` or it |
| 137 | # really is empty.. |
| 138 | main_elem = browser.find_element( |
| 139 | By.XPATH, get_links_for_location.__name__, "top_elements" |
| 140 | ) |
| 141 | top_posts = [] |
| 142 | sleep(2) |
| 143 | |
| 144 | try: |
| 145 | possible_posts = browser.execute_script( |
| 146 | "return window._sharedData.entry_data." |
| 147 | "LocationsPage[0].graphql.location.edge_location_to_media.count" |
| 148 | ) |
| 149 | |
| 150 | except WebDriverException: |
| 151 | logger.info( |
| 152 | "Failed to get the amount of possible posts in '{}' " |
| 153 | "location".format(location) |
| 154 | ) |
| 155 | possible_posts = None |
| 156 | |
| 157 | logger.info( |
no test coverage detected