From 651b590cfa9b4c549178c9fb93ad5a4be176f736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Koch-Kramer?= Date: Thu, 28 Jul 2016 18:15:36 +0200 Subject: [PATCH] Implementation of get_username_by_id() --- README.md | 9 ++++++++- instaloader.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0822d17..982f37a 100644 --- a/README.md +++ b/README.md @@ -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']) +``` diff --git a/instaloader.py b/instaloader.py index 6250f41..7c5d2e2 100755 --- a/instaloader.py +++ b/instaloader.py @@ -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')