mirror of
https://github.com/instaloader/instaloader.git
synced 2024-11-04 09:22:29 +01:00
Implementation of get_username_by_id()
This commit is contained in:
parent
9d1af7adaf
commit
651b590cfa
@ -45,7 +45,7 @@ import instaloader
|
||||
session = instaloader.get_logged_in_session(USERNAME)
|
||||
|
||||
# get followees
|
||||
followees = instaloader.get_followees(PROFILES, session)
|
||||
followees = instaloader.get_followees(PROFILE, session)
|
||||
for f in followees:
|
||||
print("%i\t%s\t%s" % (f['follower_count'], f['username'], f['full_name']))
|
||||
```
|
||||
@ -58,3 +58,10 @@ for f in followees:
|
||||
except instaloader.NonfatalException:
|
||||
pass
|
||||
```
|
||||
|
||||
`get_followees()` also returns unique IDs for all loaded followees. These IDs stay unchanged even
|
||||
if a user changes his/her username. To get the current username of a profile, given this unique ID
|
||||
`get_username_by_id()` can be used. For example:
|
||||
```python
|
||||
instaloader.get_username_by_id(session, followees[0]['id'])
|
||||
```
|
||||
|
@ -53,6 +53,24 @@ def get_last_id(data):
|
||||
data = data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]
|
||||
return int(data[len(data)-1]["id"])
|
||||
|
||||
def get_username_by_id(session, profile_id):
|
||||
tempsession = copy_session(session)
|
||||
tempsession.headers.update({'Content-Type' : 'application/json'})
|
||||
resp = tempsession.post('https://www.instagram.com/query/', data='q=ig_user(' +
|
||||
str(profile_id) +')+%7B%0A++username%0A%7D%0A')
|
||||
if resp.status_code == 200:
|
||||
data = json.loads(resp.text)
|
||||
if 'username' in data:
|
||||
return json.loads(resp.text)['username']
|
||||
raise ProfileNotExistsException("no profile found, the user may have blocked " +
|
||||
"you (id: " + str(profile_id) + ")")
|
||||
else:
|
||||
if test_login(session):
|
||||
raise ConnectionException("username could not be determined due to ConnectionError" +
|
||||
" (id: "+ str(profile_id) +")")
|
||||
raise LoginRequiredException("Login required to determine username (id: " +
|
||||
str(profile_id) + ")")
|
||||
|
||||
def epoch_to_string(epoch):
|
||||
return datetime.datetime.fromtimestamp(epoch).strftime('%Y-%m-%d_%H-%M-%S')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user