MCPcopy
hub / github.com/InstaPy/InstaPy / find_user_id

Function find_user_id

instapy/util.py:1714–1767  ·  view source on GitHub ↗

Find the user ID from the loaded page

(browser, track, username, logger)

Source from the content-addressed store, hash-verified

1712
1713
1714def find_user_id(browser, track, username, logger):
1715 """Find the user ID from the loaded page"""
1716
1717 query = None
1718 meta_XP = None
1719
1720 logger.info(
1721 "Attempting to find user ID: Track: {}, Username {}".format(track, username)
1722 )
1723 if track in ["dialog", "profile"]:
1724 query = "return window.__additionalData[Object.keys(window.__additionalData)[0]].data.graphql.user.id"
1725
1726 elif track == "post":
1727 query = "return window._sharedData.entry_data.ProfilePage[0].graphql.user.id"
1728 meta_XP = read_xpath(find_user_id.__name__, "meta_XP")
1729
1730 failure_message = "Failed to get the user ID of '{}' from {} page!".format(
1731 username, track
1732 )
1733
1734 try:
1735 user_id = browser.execute_script(query)
1736
1737 except WebDriverException:
1738 try:
1739 browser.execute_script("location.reload()")
1740 update_activity(browser, state=None)
1741
1742 user_id = browser.execute_script(
1743 "return window._sharedData.entry_data.ProfilePage[0].graphql.user.id"
1744 )
1745
1746 except WebDriverException:
1747 if track == "post":
1748 try:
1749 user_id = browser.find_element(By.XPATH, meta_XP).get_attribute(
1750 "content"
1751 )
1752 if user_id:
1753 user_id = format_number(user_id)
1754
1755 else:
1756 logger.error("{}\t~empty string".format(failure_message))
1757 user_id = None
1758
1759 except NoSuchElementException:
1760 logger.error(failure_message)
1761 user_id = None
1762
1763 else:
1764 logger.error(failure_message)
1765 user_id = None
1766
1767 return user_id
1768
1769
1770@contextmanager

Callers 1

get_user_idFunction · 0.85

Calls 4

read_xpathFunction · 0.85
update_activityFunction · 0.85
format_numberFunction · 0.85
execute_scriptMethod · 0.80

Tested by

no test coverage detected