def grab_follower_metadata(loader, user: str) -> []: try: profile = Profile.from_username(loader.context, user) except ProfileNotExistsException: print(f'Follower {user} not found! Logging in to retry...') loader.login(username, password) try: profile = Profile.from_username(loader.context, user) except: print(f'Follower {user} not found ! Skipping...') loader = instaloader.Instaloader() return loader = instaloader.Instaloader() # logout ret_user = [user, profile.followers, profile.followees] return ret_user
def instaloader_init(ig_user=USERNAME, ig_passwd=PASSWORD): global SESSION_FILE SESSION_FILE = f"sessions/{ig_user}-SESSION" # Get instance loader = instaloader.Instaloader( dirname_pattern=DOWNLOAD_PATH, sleep=True, download_pictures=False, post_metadata_txt_pattern="", compress_json=False, download_geotags=False, filename_pattern="{date_utc:%Y-%m-%d_%H-%M-%S}-{shortcode}", save_metadata=True, download_comments=False, download_videos=False, download_video_thumbnails=False) if not exists(SESSION_FILE): logger.info(f"Logging-in with account '{ig_user}'...") loader.login(ig_user, ig_passwd) # (login) loader.save_session_to_file(filename=SESSION_FILE) # L.interactive_login(USER) # (ask password on terminal) else: loader.load_session_from_file(ig_user, SESSION_FILE) return loader
def target(self, fraud_target: str): # reload to avoid Profile.from_username bug self.loader = loader = instaloader.Instaloader() try: self.fraud_target = Profile.from_username(loader.context, fraud_target) print(f'Target found: {fraud_target}') except: print(f'Failed to acquire lock on profile {fraud_target},', f'logging in to retry...') try: loader.login(self.username, self.password) self.fraud_target = Profile.from_username( loader.context, fraud_target) print(f'Target found: {fraud_target}') self.loader = instaloader.Instaloader() # log out except: print(f'Failed to acquire lock on profile {fraud_target}') return
def __init__(self): logger.debug('instaloader setup') user = os.getenv('IG_USER') password = os.getenv('IG_PASS') # instaloader -l i2t_bot self.L = instaloader.Instaloader() filename = os.path.join(os.getcwd(), 'session-i2t_bot') self.L.load_session_from_file(user, filename=filename) # self.L.login(user, password) self.L.post_metadata_txt_pattern = '' self.L.download_comments = False self.L.compress_json = False self.L.save_metadata = False self.L.download_video_thumbnails = False
def __init__(self, login, password): self.login = login self.password = password self.loader = instaloader.Instaloader( sleep=False, quiet=False, user_agent=None, dirname_pattern=config.DATA_DIRECTORY_NO_RSS + '{profile}', filename_pattern='{date:%Y-%m-%d_%H-%M-%S}', download_videos=instaloader.Tristate.always, download_video_thumbnails=instaloader.Tristate.always, download_geotags=instaloader.Tristate.always, save_captions=instaloader.Tristate.always, download_comments=instaloader.Tristate.always, save_metadata=instaloader.Tristate.always, max_connection_attempts=3)
def check_for_fraud(self): if not hasattr(self, 'fraud_target') or \ not hasattr(self.fraud_target, 'username'): print(f'Please lock-on to a target first.') return target = self.fraud_target.username if os.path.exists(f'{target}_dataframe.csv'): self.show_distribution() return filename = f'{target}_followers.csv' followers = [] # Look for first checkpoint if os.path.exists(filename): print('First checkpoint found!') print(f'Loading {target}\'s followers...') f = open(filename, 'r') followers = f.read().splitlines() f.close() else: self.loader.login(self.username, self.password) # Load first checkpoint post_iterator = self.fraud_target.get_followers() try: for follower in post_iterator: print(' .', end='', flush=True) followers.append(follower.username) # Create first checkpoint df = pd.DataFrame(data=followers) df.to_csv(filename, index=False, header=False) print(f'Wrote {str(len(followers))} followers to file.') except: print(f'Could not find followers for profile', f'{target}') # TODO: SAVE POST ITERATOR quit() self.followers = followers self.loader = instaloader.Instaloader() # 'logout' print('Followers loaded.') self.__get_metrics()
def build_dataframe(self): if not hasattr(self, 'fraud_target') or \ not hasattr(self.fraud_target, 'username'): print(f'Please lock-on to a target first.') return target = self.fraud_target.username build_file_1 = f'{target}_build_file_0' build_file_2 = f'{target}_build_file_1' build_file_3 = f'{target}_build_file_2' build_file_4 = f'{target}_build_file_3' # Read the build_file into a dataframe with column labels try: df_1 = pd.read_csv(build_file_1, header=None, names=['username', 'followers', 'following']) df_2 = pd.read_csv(build_file_2, header=None, names=['username', 'followers', 'following']) df_3 = pd.read_csv(build_file_3, header=None, names=['username', 'followers', 'following']) df_4 = pd.read_csv(build_file_4, header=None, names=['username', 'followers', 'following']) df = df_1.append(df_2.append(df_3.append(df_4))) except FileNotFoundError: print('No build file found. Could not build dataframe.') return df_filename = f'{self.fraud_target.username}_dataframe.csv' df.to_csv(df_filename, index=False) self.df_filename = df_filename self.loader = instaloader.Instaloader() print(f'Dataframe written to {df_filename}') return df
def __init__(self, login, password): try: self.user_instance = UserProfile.objects.get(username=login) self.user = self.user_instance.user self.password = password self.logger = self.user_instance.get_user_logger() self.configurations = self.user_instance.configuration try: fallback = random.sample(LIST_OF_FAKE_UA, 1) fake_ua = fake_useragent.UserAgent(fallback=fallback[0]) self.user_agent = self.check_and_insert_user_agent( str(fake_ua)) except Exception as e: self.logger.warning('Exception in creating fake user', e) fake_ua = random.sample(LIST_OF_FAKE_UA, 1) self.user_agent = self.check_and_insert_user_agent( str(fake_ua[0])) self.instaloader = instaloader.Instaloader() self.time_in_day = 24 * 60 * 60 self.unfollow_whitelist = list( WhiteListedUser.objects.values_list('username', flat=True)) self.like_delay = self.time_in_day / self.configurations.likes_per_day try: self.bot_session = BotSession.objects.create( user=self.user_instance, bot_creation_time=timezone.now()) except Exception as e: print('Exception in adding bot session in model' + str(e)) return self.unlike_per_day = self.configurations.unlike_per_day if self.configurations.unlike_per_day and self.configurations.unlike_per_day != 0: self.unlike_per_day = self.time_in_day / self.unlike_per_day if self.configurations.follow_per_day != 0: self.follow_delay = self.time_in_day / self.configurations.follow_per_day if self.configurations.comments_per_day != 0: self.comments_delay = self.time_in_day / self.configurations.comments_per_day if self.configurations.unfollow_per_day != 0: self.unfollow_delay = self.time_in_day / self.configurations.unfollow_per_day self.bot_creation_time = datetime.datetime.now() self.bot_start_time = time.time() self.bot_mode = 0 self.session_file = f"{self.user_instance.username}.session" self.session_1 = requests.Session() self.session_2 = requests.Session() self.set_blacked_list_user_dict() print('initialize Media Manager') self.media_manager = MediaManager(self) print('Initialized media manger', self.media_manager) self.insta_explorer = Explorer(self) self.insta_explorer.populate_user_blacklist() self.like_manager = LikeManager(self) self.comment_manager = CommentManager(self) self.follow_manager = FollowManager(self) self.unfollow_manager = UnfollowManager(self) self.login() signal.signal(signal.SIGINT, self.stop_bot) signal.signal(signal.SIGTERM, self.stop_bot) atexit.register(self.stop_bot) except User.DoesNotExist as e: self.logger.error('User does not exist', str(e)) except Exception as e: print(e)
def __init__(self, username: str, password: str): self.loader = instaloader.Instaloader() self.username = username self.password = password
def worker(pool: [], worker_idx: int, payload: []): pool_loader = instaloader.Instaloader() fraud_target_username = payload[0] username = payload[1] password = payload[2] headers = payload[3][0] filename = f'{fraud_target_username}_build_file_{str(worker_idx)}' build_file = [] res = [] def grab_follower_metadata(loader, user: str) -> []: try: profile = Profile.from_username(loader.context, user) except ProfileNotExistsException: print(f'Follower {user} not found! Logging in to retry...') loader.login(username, password) try: profile = Profile.from_username(loader.context, user) except: print(f'Follower {user} not found ! Skipping...') loader = instaloader.Instaloader() return loader = instaloader.Instaloader() # logout ret_user = [user, profile.followers, profile.followees] return ret_user # Test proxy connections # Fall back to local connection should any problems occur try: http_proxy = payload[3][1][worker_idx] os.environ['http_proxy'] = f'http://{http_proxy}' os.environ['https_proxy'] = f'https://{http_proxy}' except: print(f'Proxy not found for worker {worker_idx}.', f'Defaulting to local IP') del os.environ['http_proxy'] del os.environ['https_proxy'] http_proxy = 'localhost' try: res = requests.get('http://instagram.com', headers=headers, timeout=5) if res.status_code != 200: print(f'Pool worker {worker_idx} encountered an error accessing', f'instagram through {http_proxy}. Defaulting to local IP') del os.environ['http_proxy'] del os.environ['https_proxy'] http_proxy = 'localhost' except InvalidProxyURL: print(f'Please check proxy URL {worker_idx}, it is possibly', f'malformed. Defaulting to local IP') del os.environ['http_proxy'] del os.environ['https_proxy'] http_proxy = 'localhost' except (ConnectTimeout, ProxyError): print(f'Worker {worker_idx} could not connect to proxy.', f'Defaulting to local IP') del os.environ['http_proxy'] del os.environ['https_proxy'] http_proxy = 'localhost' except Exception as e: print(f'Worker {worker_idx}: QUE?? Defaulting to local IP') print(e) del os.environ['http_proxy'] del os.environ['https_proxy'] http_proxy = 'localhost' print(f'Worker {worker_idx} passed proxy test via {http_proxy}!') # Try to open build file if os.path.exists(filename): build_file = open(filename, 'r', newline='') last_loaded_follower = 0 for _ in build_file.readlines(): last_loaded_follower += 1 build_file.close() build_file = open(filename, 'a', newline='') print(f'Continuing from line {str(last_loaded_follower)}', flush=True) pool = pool[last_loaded_follower:] else: build_file = open(filename, 'w', newline='') csv_writer = csv.writer(build_file) # Continue loading from checkpoint try: for user in tqdm(pool): new_user = grab_follower_metadata(pool_loader, user) csv_writer.writerow(new_user) # write to checkpoint except KeyboardInterrupt: print("\nKeyboard interrupt detected, exiting...") build_file.close() print(f'Build file written to {filename}') sys.exit() except ConnectionException: print("429 Too many requests: redirected at login") build_file.close() print(f'Build file written to {filename}') return build_file.close() print(f'Build file written to {filename}') return True
#First install the tool instaloader by entering following command in cmd #pip install instaloader from instaloader import instaloader insta = instaloader.Instaloader() username = input(">>>Username:"******">>>Password:"******"[+] Logging in...") insta.login(username, password) print("[+] Loggin Successful!") target = input(">>>Target username(Must be public or you follow them):") try: insta.check_profile_id(target) profile = instaloader.Profile.from_username(insta.context, username=target) print("[+]Getting List of followers...") followers = set(profile.get_followers()) print("[+]Getting List of followings...") print("Got the List!") following = set(profile.get_followees()) """Property of set if a={1,2,3,4,5,6} and b={1,2,6,7,3} a-b={4,5} and b-a={7}""" limit = input(">>>Limit of followers of target(none for no limit):") unfollowers = following - followers count = 0 print(">>>List of Non-followers") for users in unfollowers: if limit == "none": print(users.username) count += 1 elif users.followers < int(limit):
def get_posters_from_shortcodes(hashtag: str, loader: instaloadercontext) -> list: shortcodes = {} posters = [] limited_accounts = [USERNAME] if "#" in hashtag: hashtag = hashtag.replace("#", "") logger.info(f"Finding posts with hashtag '{hashtag}'") if exists(TEMP_SHORTCODES) and exists(TEMP_POSTERS): posters = load_from_file(TEMP_POSTERS) shortcodes = load_from_file(TEMP_SHORTCODES) logger.info(f"Found temp files. Resuming...\ posts found so far: {len(posters)}") else: # post_iterator = instaloader.Hashtag.from_name(loader.context, hashtag).get_posts() post_iterator = NodeIterator( loader.context, "9b498c08113f1e09617a1703c22b2f32", lambda d: d['data']['hashtag']['edge_hashtag_to_media'], lambda n: instaloader.Post(loader.context, n), {'tag_name': hashtag}, f"https://www.instagram.com/explore/tags/{hashtag}/" ) for post in post_iterator: loader.download_post(post, target=hashtag) jsons = os.listdir(DOWNLOAD_PATH) for json_file in jsons: with open(f"{DOWNLOAD_PATH}/{json_file}", "r") as rf: post = json.load(rf) shortcode = post["node"]["shortcode"] shortcodes.setdefault(shortcode, False) not_visited = [x for x, visited in shortcodes.items() if not visited] logger.info( f"'{len(shortcodes)}' posts. not visited = {len(not_visited)}.") for shortcode, visited in shortcodes.items(): if not visited: try: post = Post.from_shortcode(loader.context, shortcode=shortcode) sleep(round(random.uniform(1.000, 2.100), 3)) posters.append(post.owner_username) print(f"{post.owner_username:<30}UTC {post.date}") shortcodes[shortcode] = True if len(posters) % 50 == 0: print(f"{get_time():>50}\tposts found so far: {len(posters)}") # except instaloader.QueryReturnedBadRequestException as e: # remove_file(SESSION_FILE) # dump_to_file(shortcodes, TEMP_SHORTCODES) # dump_to_file(posters, TEMP_POSTERS) # logger.error( # f"Bad Request Exception. Probably the account '{USERNAME}' was limited by instagram.\n\ # To solve this: First try to *(solve captcha)* from instagram web and *(verify phone number)* and change password # if required.\n") # telegram_send(TELEGRAM_ID, "Account limited", # f"Account {USERNAME} was limited, solve captcha and when it was no longer limited, press enter") except KeyError: logger.info(f"KeyError, Post {shortcode} not found.") except Exception as e: remove_file(SESSION_FILE) dump_to_file(shortcodes, TEMP_SHORTCODES) dump_to_file(posters, TEMP_POSTERS) logger.error(f"Exception while fetching posters! Details: {e}") logger.info( "Saved posters and shortcodes. Trying to switch account...") for uname in LOGIN_CREDS.keys(): if uname not in limited_accounts: limited_accounts.append(uname) logger.info( f"Switched to account {uname}. Go to login activity and 'click This Was Me'") loader = instaloader_init( ig_user=uname, ig_passwd=LOGIN_CREDS[uname]) break else: logger.info( "All accounts were limited. 1. solve the captcha 2. change password if needed. Then press ENTER to login interactively") uname = input("enter instagram username: "******"{date_utc:%Y-%m-%d_%H-%M-%S}-{shortcode}", sleep=True, download_pictures=False, post_metadata_txt_pattern="", compress_json=False, download_geotags=False, save_metadata=True, download_comments=False, download_videos=False, download_video_thumbnails=False) loader.interactive_login(uname) except KeyboardInterrupt: dump_to_file(shortcodes, TEMP_SHORTCODES) dump_to_file(posters, TEMP_POSTERS) logger.error("keyboardInterrupt. Saved posters and shortcodes") sys.exit("Exited 1") # Double check shortcodes and remove temporary files if all shortcodes are visited for shortcode, visited in shortcodes.items(): if not visited: print(f"shortcode '{shortcode}' is not visited yet.") remove_file(TEMP_POSTERS) remove_file(TEMP_SHORTCODES) shutil.rmtree(DOWNLOAD_PATH) return posters
def main(): l = instaloader.Instaloader() user = login(l) download(l, user) return
# import the moudle from instaloader import instaloader #crate an instancenof instaloder class bot = instaloader.Instaloader() #load a profile from instagram handel profile = instaloader.Profile.from_username(bot.context, 'pkhoddami') print(type(profile)) print(profile)