Skip to content

Commit c17f33a

Browse files
authored
Merge pull request #346 from Joeffison/examples
Fix get user's followers example
2 parents ff27548 + da80ed6 commit c17f33a

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

examples/evaluation/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Use text editor to edit the script and type in valid Instagram username/password
5+
6+
from InstagramAPI import InstagramAPI
7+
from examples.evaluation.evaluation_log import EvaluationLog
8+
from examples.user_followers import getTotalFollowers
9+
10+
11+
def evaluate_method(function, parameters, function_name=None):
12+
evaluation_log = EvaluationLog()
13+
evaluation_log.start_log(function_name)
14+
response = function(*parameters)
15+
evaluation_log.end_log(function_name)
16+
17+
print('response size:', len(response))
18+
print('number of unique users:', len(set([user['username'] for user in response])))
19+
print()
20+
21+
22+
if __name__ == "__main__":
23+
api = InstagramAPI("username", "password")
24+
api.login()
25+
26+
# For a user with over 22k followers, use: user_id = '1461295173'
27+
user_id = api.username_id
28+
29+
evaluate_method(api.getTotalFollowers, [user_id], 'api.getTotalFollowers')
30+
evaluate_method(getTotalFollowers, [api, user_id], 'getTotalFollowers')

examples/evaluation/evaluation_log.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from time import time, process_time, strftime, localtime
2+
from datetime import timedelta
3+
4+
5+
def time_to_str(elapsed=None):
6+
if elapsed is None:
7+
return strftime("%Y-%m-%d %H:%M:%S", localtime())
8+
else:
9+
return str(timedelta(seconds=elapsed))
10+
11+
12+
class EvaluationLog():
13+
14+
def start_log(self, s="Start Program"):
15+
self.start = time()
16+
self.cpu_start = process_time()
17+
self.log(s)
18+
19+
def end_log(self, s="End Program"):
20+
self.end = time()
21+
self.cpu_end = process_time()
22+
elapsed_time = self.end - self.start
23+
cpu_time = self.cpu_end - self.cpu_start
24+
self.log(s, time_to_str(elapsed_time), time_to_str(cpu_time))
25+
26+
@staticmethod
27+
def log(s, elapsed_time=None, cpu_time=None):
28+
line = "=" * 40
29+
print(line)
30+
print(time_to_str(), '-', s)
31+
32+
if elapsed_time:
33+
print("Elapsed time:", elapsed_time)
34+
if cpu_time:
35+
print("CPU time:", cpu_time)
36+
37+
print(line)

examples/user_followers.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,38 @@
55

66
from InstagramAPI import InstagramAPI
77

8-
api = InstagramAPI("login", "password")
9-
api.login() # login
10-
api.tagFeed("cat") # get media list by tag #cat
11-
media_id = api.LastJson # last response JSON
12-
api.like(media_id["ranked_items"][0]["pk"]) # like first media
13-
api.getUserFollowers(media_id["ranked_items"][0]["user"]["pk"]) # get first media owner followers
14-
print(api.LastJson)
8+
9+
def getTotalFollowers(api, user_id):
10+
"""
11+
Returns the list of followers of the user.
12+
It should be equivalent of calling api.getTotalFollowers from InstagramAPI
13+
"""
14+
15+
followers = []
16+
next_max_id = True
17+
while next_max_id:
18+
# first iteration hack
19+
if next_max_id is True:
20+
next_max_id = ''
21+
22+
_ = api.getUserFollowers(user_id, maxid=next_max_id)
23+
followers.extend(api.LastJson.get('users', []))
24+
next_max_id = api.LastJson.get('next_max_id', '')
25+
return followers
26+
27+
28+
if __name__ == "__main__":
29+
api = InstagramAPI("username", "password")
30+
api.login()
31+
32+
# user_id = '1461295173'
33+
user_id = api.username_id
34+
35+
# List of all followers
36+
followers = getTotalFollowers(api, user_id)
37+
print('Number of followers:', len(followers))
38+
39+
# Alternatively, use the code below
40+
# (check evaluation.evaluate_user_followers for further details).
41+
followers = api.getTotalFollowers(user_id)
42+
print('Number of followers:', len(followers))

0 commit comments

Comments
 (0)