Gets the followers & following counts of a given user
(browser, username, logger)
| 1173 | |
| 1174 | |
| 1175 | def get_relationship_counts(browser, username, logger): |
| 1176 | """Gets the followers & following counts of a given user""" |
| 1177 | |
| 1178 | user_link = "https://www.instagram.com/{}/".format(username) |
| 1179 | |
| 1180 | # check URL of the webpage, if it already is user's profile page, |
| 1181 | # then do not navigate to it again |
| 1182 | web_address_navigator(browser, user_link) |
| 1183 | |
| 1184 | try: |
| 1185 | followers_count = browser.execute_script( |
| 1186 | "return window._sharedData.entry_data." |
| 1187 | "ProfilePage[0].graphql.user.edge_followed_by.count" |
| 1188 | ) |
| 1189 | |
| 1190 | except WebDriverException: |
| 1191 | try: |
| 1192 | followers_count = format_number( |
| 1193 | browser.find_element( |
| 1194 | By.XPATH, |
| 1195 | str( |
| 1196 | read_xpath(get_relationship_counts.__name__, "followers_count") |
| 1197 | ), |
| 1198 | ).text |
| 1199 | ) |
| 1200 | except NoSuchElementException: |
| 1201 | try: |
| 1202 | browser.execute_script("location.reload()") |
| 1203 | update_activity(browser, state=None) |
| 1204 | |
| 1205 | followers_count = browser.execute_script( |
| 1206 | "return window._sharedData.entry_data." |
| 1207 | "ProfilePage[0].graphql.user.edge_followed_by.count" |
| 1208 | ) |
| 1209 | |
| 1210 | except WebDriverException: |
| 1211 | try: |
| 1212 | topCount_elements = browser.find_elements( |
| 1213 | By.XPATH, |
| 1214 | read_xpath( |
| 1215 | get_relationship_counts.__name__, "topCount_elements" |
| 1216 | ), |
| 1217 | ) |
| 1218 | |
| 1219 | if topCount_elements: |
| 1220 | followers_count = format_number(topCount_elements[1].text) |
| 1221 | else: |
| 1222 | logger.info( |
| 1223 | "Failed to get followers count of '{}' ~empty " |
| 1224 | "list".format(username.encode("utf-8")) |
| 1225 | ) |
| 1226 | followers_count = None |
| 1227 | |
| 1228 | except NoSuchElementException: |
| 1229 | logger.error( |
| 1230 | "Error occurred during getting the followers count " |
| 1231 | "of '{}'\n".format(username.encode("utf-8")) |
| 1232 | ) |
no test coverage detected