Fixed and reimplemented get_username_by_id()
This commit is contained in:
parent
715582138b
commit
ca2829becc
@ -179,24 +179,33 @@ class Instaloader:
|
|||||||
return json.loads(match.group(0)[21:-2])
|
return json.loads(match.group(0)[21:-2])
|
||||||
|
|
||||||
def get_username_by_id(self, profile_id: int) -> str:
|
def get_username_by_id(self, profile_id: int) -> str:
|
||||||
"""To get the current username of a profile, given its unique ID, this function can be used.
|
"""To get the current username of a profile, given its unique ID, this function can be used."""
|
||||||
session is required to be a logged-in (i.e. non-anonymous) session."""
|
tmpsession = copy_session(self.session)
|
||||||
tempsession = copy_session(self.session)
|
tmpsession.headers.update(default_http_header())
|
||||||
tempsession.headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
|
del tmpsession.headers['Referer']
|
||||||
resp = tempsession.post('https://www.instagram.com/query/',
|
del tmpsession.headers['X-Instagram-AJAX']
|
||||||
data='q=ig_user(' + str(profile_id) + ')+%7B%0A++username%0A%7D%0A')
|
resp = tmpsession.get('https://www.instagram.com/graphql/query/',
|
||||||
|
params={'query_id': '17862015703145017',
|
||||||
|
'variables': '{"id":"' + str(profile_id) + '","first":1}'})
|
||||||
if resp.status_code == 200:
|
if resp.status_code == 200:
|
||||||
data = json.loads(resp.text)
|
data = json.loads(resp.text)["data"]["user"]
|
||||||
if 'username' in data:
|
if data:
|
||||||
return json.loads(resp.text)['username']
|
data = data["edge_owner_to_timeline_media"]
|
||||||
raise ProfileNotExistsException("No profile found, the user may have blocked " +
|
else:
|
||||||
"you (id: " + str(profile_id) + ").")
|
raise ProfileNotExistsException("No profile found, the user may have blocked you (ID: " +
|
||||||
|
str(profile_id) + ").")
|
||||||
|
if not data['edges']:
|
||||||
|
if data['count'] == 0:
|
||||||
|
raise ProfileHasNoPicsException("Profile with ID {0}: no pics found.".format(str(profile_id)))
|
||||||
|
else:
|
||||||
|
raise LoginRequiredException("Login required to determine username (ID: " + str(profile_id) + ").")
|
||||||
|
else:
|
||||||
|
shortcode = mediaid_to_shortcode(int(data['edges'][0]["node"]["id"]))
|
||||||
|
data = self.get_json("p/" + shortcode)
|
||||||
|
return data['entry_data']['PostPage'][0]['graphql']['shortcode_media']['owner']['username']
|
||||||
else:
|
else:
|
||||||
if self.test_login(self.session):
|
raise ProfileAccessDeniedException("Username could not be determined due to error {0} (ID: {1})."
|
||||||
raise ProfileAccessDeniedException("Username could not be determined due to error {0} (id: {1})."
|
.format(str(resp.status_code), str(profile_id)))
|
||||||
.format(str(resp.status_code), str(profile_id)))
|
|
||||||
raise LoginRequiredException("Login required to determine username (id: " +
|
|
||||||
str(profile_id) + ").")
|
|
||||||
|
|
||||||
def get_id_by_username(self, profile: str) -> int:
|
def get_id_by_username(self, profile: str) -> int:
|
||||||
"""Each Instagram profile has its own unique ID which stays unmodified even if a user changes
|
"""Each Instagram profile has its own unique ID which stays unmodified even if a user changes
|
||||||
@ -687,17 +696,14 @@ class Instaloader:
|
|||||||
profile_id = int(id_file.read())
|
profile_id = int(id_file.read())
|
||||||
if (not profile_exists) or \
|
if (not profile_exists) or \
|
||||||
(profile_id != int(json_data['entry_data']['ProfilePage'][0]['user']['id'])):
|
(profile_id != int(json_data['entry_data']['ProfilePage'][0]['user']['id'])):
|
||||||
if is_logged_in:
|
|
||||||
newname = self.get_username_by_id(profile_id)
|
|
||||||
self._log("Profile {0} has changed its name to {1}.".format(profile, newname))
|
|
||||||
os.rename(profile, newname)
|
|
||||||
return newname
|
|
||||||
if profile_exists:
|
if profile_exists:
|
||||||
raise ProfileNotExistsException("Profile {0} does not match the stored "
|
self._log("Profile {0} does not match the stored unique ID {1}.".format(profile, profile_id))
|
||||||
"unique ID {1}.".format(profile, profile_id))
|
else:
|
||||||
raise ProfileNotExistsException("Profile {0} does not exist. Please login to "
|
self._log("Trying to find profile {0} using its unique ID {1}.".format(profile, profile_id))
|
||||||
"update profile name. Unique ID: {1}."
|
newname = self.get_username_by_id(profile_id)
|
||||||
.format(profile, profile_id))
|
self._log("Profile {0} has changed its name to {1}.".format(profile, newname))
|
||||||
|
os.rename(profile, newname)
|
||||||
|
return newname
|
||||||
return profile
|
return profile
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
@ -827,8 +833,9 @@ class Instaloader:
|
|||||||
geotags, fast_update)
|
geotags, fast_update)
|
||||||
except ProfileNotExistsException as err:
|
except ProfileNotExistsException as err:
|
||||||
if username is not None:
|
if username is not None:
|
||||||
|
self._log(err)
|
||||||
self._log(
|
self._log(
|
||||||
"\"Profile not exists\" - Trying again anonymously, helps in case you are just blocked")
|
"Trying again anonymously, helps in case you are just blocked.")
|
||||||
anonymous_loader = Instaloader(self.sleep, self.quiet, self.shorter_output)
|
anonymous_loader = Instaloader(self.sleep, self.quiet, self.shorter_output)
|
||||||
anonymous_loader.download(target, profile_pic_only, download_videos,
|
anonymous_loader.download(target, profile_pic_only, download_videos,
|
||||||
geotags, fast_update)
|
geotags, fast_update)
|
||||||
|
Loading…
Reference in New Issue
Block a user