From 50564fb560afd558ea4fc2ed346bde0bc3a6b00e Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 18 Jun 2021 19:00:35 +0530 Subject: [PATCH] make getting initial data faster --- BurbnBot/burbnbot.py | 2 +- example.py | 95 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/BurbnBot/burbnbot.py b/BurbnBot/burbnbot.py index ed5d652..99ddf96 100644 --- a/BurbnBot/burbnbot.py +++ b/BurbnBot/burbnbot.py @@ -239,7 +239,6 @@ def open_profile(self, username: str, open_post: bool = False) -> bool: bool: The return value. True for success, False otherwise. """ try: - self.d.app_stop_all() url = "https://www.instagram.com/{}/".format(username) uiautomator2.logger.info("Opening profile {}.".format(url)) self.d.shell("am start -a android.intent.action.VIEW -d {}".format(url)) @@ -263,6 +262,7 @@ def open_profile(self, username: str, open_post: bool = False) -> bool: return True except Exception as e: uiautomator2.logger.info(e) + self.d.app_stop_all() return False def open_tag(self, tag: str, tab: str = "Recent", check_banned: bool = True) -> bool: diff --git a/example.py b/example.py index 2f021d9..2285a9e 100644 --- a/example.py +++ b/example.py @@ -1,7 +1,102 @@ +from typing import Dict from BurbnBot import Burbnbot +from InstagramAPI import InstagramAPI +import json +import time bot = Burbnbot() + +#faster way to get data +api = InstagramAPI(USERNAME, PASS) +api.login() +user_id = api.username_id + +def getTotalFollowers(): + 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 + +def getTotalFollowing(): + followers = [] + next_max_id = True + while next_max_id: + # first iteration hack + if next_max_id is True: + next_max_id = '' + + _ = api.getUserFollowings(user_id, maxid=next_max_id) + followers.extend(api.LastJson.get('users', [])) + next_max_id = api.LastJson.get('next_max_id', '') + return followers + +def nonFollowers(followers, following): + nonFollowers = {} + dictFollowers = {} + for follower in followers: + dictFollowers[follower['username']] = follower['pk'] + + for followedUser in following: + if followedUser['username'] not in dictFollowers: + nonFollowers[followedUser['username']] = followedUser['pk'] + + return nonFollowers + +def getAllData(): + followers = getTotalFollowers() + following = getTotalFollowing() + nonFollow = nonFollowers(followers, following) + i = len(nonFollow) + print('Number of followers:', len(followers)) + with open("followers.txt","w", encoding="utf-8") as f: + f.write(str(followers)) + + time.sleep(3) + print('Number of following:', len(following)) + with open("following.txt","w", encoding="utf-8") as f: + f.write(str(following)) + + time.sleep(3) + print('Number of nonFollowers:', i) + with open("nonFollowers.txt","w", encoding="utf-8") as f: + f.write(str(nonFollow)) + + time.sleep(3) + +#call this only once to fetch data and store it +getAllData() + +#For unfollowing non followers +i = 1 +with open("nonFollowers.json","r", encoding="utf-8") as f: + users : Dict = json.load(f) + usernames = list(Dict.fromkeys(users)) + print(usernames) +with open("unfollwed.txt","a") as f: + for user in usernames: + # unfollow who don't follow you back + bot.unfollow(username=user) + f.writelines(str(user) + "\n") + print(f"unfollowed: {i}") + + users.pop(user) + f.flush() + with open("nonFollowers.json","w", encoding="utf-8") as f2: + json.dump(users,f2) + #set limit for no of unfollows + if(i==30): + break + i=i+1 + time.sleep(3) + # get the following list (take a long time) users_following = bot.get_following_list()