1
0
mirror of https://github.com/instaloader/instaloader.git synced 2024-09-11 16:22:24 +02:00

Implementation of get_username_by_id()

This commit is contained in:
André Koch-Kramer 2016-07-28 18:15:36 +02:00
parent 9d1af7adaf
commit 651b590cfa
2 changed files with 26 additions and 1 deletions

View File

@ -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'])
```

View File

@ -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')