[instagram] use '/topsearch/' to fetch user information (#8978)
This commit is contained in:
@@ -879,6 +879,17 @@ class InstagramRestAPI():
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
raise exception.NotFoundError("user")
|
raise exception.NotFoundError("user")
|
||||||
|
|
||||||
|
@cache(maxage=36500*86400, keyarg=1)
|
||||||
|
def user_by_search(self, screen_name):
|
||||||
|
url = "https://www.instagram.com/web/search/topsearch/"
|
||||||
|
params = {"query": screen_name}
|
||||||
|
|
||||||
|
name = screen_name.lower()
|
||||||
|
for result in self._call(url, params=params)["users"]:
|
||||||
|
user = result["user"]
|
||||||
|
if user["username"].lower() == name:
|
||||||
|
return user
|
||||||
|
|
||||||
@cache(maxage=36500*86400, keyarg=1)
|
@cache(maxage=36500*86400, keyarg=1)
|
||||||
def user_by_id(self, user_id):
|
def user_by_id(self, user_id):
|
||||||
endpoint = f"/v1/users/{user_id}/info/"
|
endpoint = f"/v1/users/{user_id}/info/"
|
||||||
@@ -890,16 +901,22 @@ class InstagramRestAPI():
|
|||||||
self.extractor._user = self.user_by_id(screen_name[3:])
|
self.extractor._user = self.user_by_id(screen_name[3:])
|
||||||
return screen_name[3:]
|
return screen_name[3:]
|
||||||
|
|
||||||
user = self.user_by_name(screen_name)
|
user = self.user_by_search(screen_name)
|
||||||
if user is None:
|
if user is None:
|
||||||
self.user_by_name.invalidate(screen_name)
|
self.user_by_search.invalidate(screen_name)
|
||||||
raise exception.AuthorizationError(
|
self.log.warning("Failed to find profile '%s' via search. "
|
||||||
"Login required to access this profile")
|
"Trying 'web_profile_info' fallback", screen_name)
|
||||||
if check_private and user["is_private"] and \
|
user = self.user_by_name(screen_name)
|
||||||
not user["followed_by_viewer"]:
|
if user is None:
|
||||||
name = user["username"]
|
self.user_by_name.invalidate(screen_name)
|
||||||
s = "" if name.endswith("s") else "s"
|
raise exception.AuthorizationError(
|
||||||
self.extractor.log.warning("%s'%s posts are private", name, s)
|
"Login required to access this profile")
|
||||||
|
if check_private and user["is_private"] and \
|
||||||
|
not user.get("followed_by_viewer", True):
|
||||||
|
name = user["username"]
|
||||||
|
s = "" if name.endswith("s") else "s"
|
||||||
|
self.extractor.log.warning("%s'%s posts are private", name, s)
|
||||||
|
|
||||||
self.extractor._assign_user(user)
|
self.extractor._assign_user(user)
|
||||||
return user["id"]
|
return user["id"]
|
||||||
|
|
||||||
@@ -946,7 +963,10 @@ class InstagramRestAPI():
|
|||||||
def _call(self, endpoint, **kwargs):
|
def _call(self, endpoint, **kwargs):
|
||||||
extr = self.extractor
|
extr = self.extractor
|
||||||
|
|
||||||
url = "https://www.instagram.com/api" + endpoint
|
if endpoint[0] == "/":
|
||||||
|
url = "https://www.instagram.com/api" + endpoint
|
||||||
|
else:
|
||||||
|
url = endpoint
|
||||||
kwargs["headers"] = {
|
kwargs["headers"] = {
|
||||||
"Accept" : "*/*",
|
"Accept" : "*/*",
|
||||||
"X-CSRFToken" : extr.csrf_token,
|
"X-CSRFToken" : extr.csrf_token,
|
||||||
|
|||||||
Reference in New Issue
Block a user