diff --git a/examples/evaluation/__init__.py b/examples/evaluation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/evaluation/evaluate_user_followers.py b/examples/evaluation/evaluate_user_followers.py new file mode 100644 index 0000000..91a7f72 --- /dev/null +++ b/examples/evaluation/evaluate_user_followers.py @@ -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') diff --git a/examples/evaluation/evaluation_log.py b/examples/evaluation/evaluation_log.py new file mode 100644 index 0000000..86d047d --- /dev/null +++ b/examples/evaluation/evaluation_log.py @@ -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) diff --git a/examples/user_followers.py b/examples/user_followers.py index 2858bc7..9df764a 100644 --- a/examples/user_followers.py +++ b/examples/user_followers.py @@ -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))