Skip to content

Commit

Permalink
Fix get user's followers example
Browse files Browse the repository at this point in the history
  • Loading branch information
Joeffison committed Mar 22, 2018
1 parent ff27548 commit da80ed6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
Empty file added examples/evaluation/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions examples/evaluation/evaluate_user_followers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Use text editor to edit the script and type in valid Instagram username/password

from InstagramAPI import InstagramAPI
from examples.evaluation.evaluation_log import EvaluationLog
from examples.user_followers import getTotalFollowers


def evaluate_method(function, parameters, function_name=None):
evaluation_log = EvaluationLog()
evaluation_log.start_log(function_name)
response = function(*parameters)
evaluation_log.end_log(function_name)

print('response size:', len(response))
print('number of unique users:', len(set([user['username'] for user in response])))
print()


if __name__ == "__main__":
api = InstagramAPI("username", "password")
api.login()

# For a user with over 22k followers, use: user_id = '1461295173'
user_id = api.username_id

evaluate_method(api.getTotalFollowers, [user_id], 'api.getTotalFollowers')
evaluate_method(getTotalFollowers, [api, user_id], 'getTotalFollowers')
37 changes: 37 additions & 0 deletions examples/evaluation/evaluation_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from time import time, process_time, strftime, localtime
from datetime import timedelta


def time_to_str(elapsed=None):
if elapsed is None:
return strftime("%Y-%m-%d %H:%M:%S", localtime())
else:
return str(timedelta(seconds=elapsed))


class EvaluationLog():

def start_log(self, s="Start Program"):
self.start = time()
self.cpu_start = process_time()
self.log(s)

def end_log(self, s="End Program"):
self.end = time()
self.cpu_end = process_time()
elapsed_time = self.end - self.start
cpu_time = self.cpu_end - self.cpu_start
self.log(s, time_to_str(elapsed_time), time_to_str(cpu_time))

@staticmethod
def log(s, elapsed_time=None, cpu_time=None):
line = "=" * 40
print(line)
print(time_to_str(), '-', s)

if elapsed_time:
print("Elapsed time:", elapsed_time)
if cpu_time:
print("CPU time:", cpu_time)

print(line)
42 changes: 35 additions & 7 deletions examples/user_followers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,38 @@

from InstagramAPI import InstagramAPI

api = InstagramAPI("login", "password")
api.login() # login
api.tagFeed("cat") # get media list by tag #cat
media_id = api.LastJson # last response JSON
api.like(media_id["ranked_items"][0]["pk"]) # like first media
api.getUserFollowers(media_id["ranked_items"][0]["user"]["pk"]) # get first media owner followers
print(api.LastJson)

def getTotalFollowers(api, user_id):
"""
Returns the list of followers of the user.
It should be equivalent of calling api.getTotalFollowers from InstagramAPI
"""

followers = []
next_max_id = True
while next_max_id:
# first iteration hack
if next_max_id is True:
next_max_id = ''

_ = api.getUserFollowers(user_id, maxid=next_max_id)
followers.extend(api.LastJson.get('users', []))
next_max_id = api.LastJson.get('next_max_id', '')
return followers


if __name__ == "__main__":
api = InstagramAPI("username", "password")
api.login()

# user_id = '1461295173'
user_id = api.username_id

# List of all followers
followers = getTotalFollowers(api, user_id)
print('Number of followers:', len(followers))

# Alternatively, use the code below
# (check evaluation.evaluate_user_followers for further details).
followers = api.getTotalFollowers(user_id)
print('Number of followers:', len(followers))

0 comments on commit da80ed6

Please sign in to comment.