async def menu(message: types.Message): global user, userid, profile row = db.execute_one_query( conn, f'SELECT * FROM users WHERE user_id = "{message.from_user.id}"') msg = message.text if '/s/' in msg: await message.reply(key[row[1]][12]) elif 'instagram.com/p/' in msg: SHORTCODE = msg.split('/p/')[1].split('/?')[0] post = Post.from_shortcode(L.context, SHORTCODE) if post.owner_profile.is_private == True: await message.reply(key[row[1]][1]) else: await post_downloader(message, post=post) elif 'instagram.com/' in msg: user = msg.split('.com/')[1][0:-1] if '?' in msg: user = msg.split('.com/')[1].split('?')[0] profile = Profile.from_username(L.context, user) userid = profile.userid print(userid) if profile.is_private == True: await message.reply(key[row[1]][1]) else: await post_downloader(message, posts=1) else: try: msg = msg.replace('@', '') profile = Profile.from_username(L.context, msg) userid = profile.userid if profile.is_private == True: await message.reply(key[row[1]][1]) else: await post_downloader(message, posts=1) except: await message.reply(key[row[1]][2])
def __init__(self, user_profile: instaloader.Profile, old=False) -> None: self.username = user_profile.username self.unghosted_users = [] self.ghoster_users = [] self.follower_likes = {} self.unfollower_likes = {} self.sus = [] self.weishenmefollow = [] if old: print("Loading from file...") self.username += "_old" self.load_from_file() self.loaded_from_file = True print("Load from old successful") self.ghoster_init() self.follower_likes_init() self.why_following() self.sus_check() return if self.username in os.listdir(): check = input('Local copy ' 'of information available use that? y/n:\n') if check in ['y', 'Y']: print("Loading from file...") self.load_from_file() self.loaded_from_file = True print("Load successful") self.ghoster_init() self.follower_likes_init() self.why_following() self.sus_check() return print("Getting posts...") self.posts = list(user_profile.get_posts()) print("Done posts") print("Getting followers...") self.followers = list(user_profile.get_followers()) print("Done followers") print("Getting following...") self.following = list(user_profile.get_followees()) print("Done following") self.follower_amounts = {} # print("Getting following followers") # for following in self.following: # self.follower_amounts[following] = following.mediacount # print("Got following followers") self.likes = {} for i in self.posts: print(f"Getting likes:" f" {self.posts.index(i)}/{len(self.posts)} posts", end='\r') self.likes[i] = list(i.get_likes()) print(f"Getting likes: {len(self.posts)}/{len(self.posts)} posts") print("Done likes") self.ghoster_init() self.follower_likes_init() self.why_following() self.sus_check() self.loaded_from_file = False
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
def showDialog5(self): # finding the content of current item in combo box content = self.combo_box3.currentText() if content=="Top 5": text, ok = QInputDialog.getText(self, 'Input Dialog', 'Der Instagram Name:') if ok: PROFILE = text L = Instaloader(save_metadata=True, compress_json=False, download_video_thumbnails=False, download_comments=False, post_metadata_txt_pattern="{likes}") profile = Profile.from_username(L.context, PROFILE) posts_sorted_by_likes = sorted(profile.get_posts(), key=lambda post: post.likes, reverse=False) for post in islice(posts_sorted_by_likes, ceil(5)): L.download_post(post, PROFILE) if content=="Top 10": text, ok = QInputDialog.getText(self, 'Input Dialog', 'Der Instagram Name:') if ok: PROFILE = text L = Instaloader(save_metadata=True, compress_json=False, download_video_thumbnails=False, download_comments=False, post_metadata_txt_pattern="{likes}") profile = Profile.from_username(L.context, PROFILE) posts_sorted_by_likes = sorted(profile.get_posts(), key=lambda post: post.likes, reverse=False) for post in islice(posts_sorted_by_likes, ceil(10)): L.download_post(post, PROFILE) if content=="Alle": text, ok = QInputDialog.getText(self, 'Input Dialog', 'Der Instagram Name:') if ok: PROFILE = text L = Instaloader(save_metadata=True, compress_json=False, download_video_thumbnails=False, download_comments=False, post_metadata_txt_pattern="{likes}") profile = Profile.from_username(L.context, PROFILE) posts_sorted_by_likes = sorted(profile.get_posts(), key=lambda post: post.likes, reverse=False) for post in islice(posts_sorted_by_likes, ceil(99999999999999)): L.download_post(post, PROFILE)
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
async def inline_kb_answer_callback_handler(query: types.CallbackQuery): answer_data = query.data global highlight global userid global user id = query.from_user.id text = query.message.caption if answer_data == 'ru' or answer_data == 'en': db.execute_query( conn, f'UPDATE users SET lang = "{answer_data}" WHERE user_id = "{id}"') await bot.edit_message_text(chat_id=id, message_id=query.message.message_id, text=key[answer_data][0]) elif len(answer_data) > 1: loop, pool, it = map(str, answer_data.split('-')) if loop == 'posts': user = pool await send_posts(id, posts=Profile.from_username(L.context, user).get_posts()) else: userid = int(pool) if loop == 'stories': await send_stories(id) elif loop == 'highlights': await info_highlights(id) elif loop == 'highlight': i = 0 for highlight in L.get_highlights(userid): if i == int(it): highlight = highlight await send_highlights(id) break i += 1
def download(l, user): # Download of the NUMBER_OF_POSTS photos for each of our followees personal_profile = Profile.from_username(l.context, user) target_profiles = personal_profile.get_followees() if not os.path.isdir('./profiledata'): os.mkdir('./profiledata') os.chdir('./profiledata') for profile in target_profiles: profile_directory = profile.username if os.path.isdir(profile_directory): continue else: os.mkdir(profile_directory) for i, posts in enumerate(profile.get_posts()): image_path = profile_directory + '/' + str(i) if posts.typename == 'GraphImage': l.download_pic(image_path, posts.url, posts.date_local, filename_suffix=None, _attempt=1) elif posts.typename == 'GraphVideo': continue elif posts.typename == 'GraphSidecar': for j, node in enumerate(posts.get_sidecar_nodes()): l.download_pic(image_path + str(j), node.display_url, posts.date_local, filename_suffix=None, _attempt=1) just_crop(profile_directory) os.chdir('../')
def info(update, context): query = update.message.text[6:] context.bot.send_chat_action(chat_id=update.message.chat_id, action="typing") try: user = Profile.from_username(L.context, query) infoMsg = f''' <b>Account Information</b> Name - {user.full_name} UserName - {user.username} Bio - {user.biography} Bio Url - {user.external_url} Followers - {user.followers} Following - {user.followees} Posts - {user.mediacount} IGTV Videos - {user.igtvcount} Account Type - {acc_type(user.is_private)} ''' update.message.reply_photo(photo=user.profile_pic_url, caption=infoMsg, parse_mode='HTML') except: update.message.reply_text( f'''Something Went Wrong..\nMaybe Username {query} not Available..''' )
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 get_followings(usernames, loader): """find followings of admin users (subscribed users and VIP users)""" followings = [] logger.info(f"Fetching admin:{usernames} followees...") try: for username in usernames: username_followings = [] profile = Profile.from_username(loader.context, username) print(f"Fetching {username} followings...") for followee in profile.get_followees(): followings.append(str(followee.username).lower()) username_followings.append(str(followee.username).lower()) dump_to_file(username_followings, f"temp/{username.lower()}_followings.list") except instaloader.QueryReturnedBadRequestException as e: remove_file(SESSION_FILE) logger.exception(f"Exception details:\n {e}") 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 the captcha, change password if needed. Run again and " f"click 'This Was Me' in Login Activity.") logger.debug(f"admin(s) {usernames} followings: {followings}") return list(set(followings))
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 save_new_middle_posts_by_profile(pfn): # pfn: profile name profile = Profile.from_username(IL.context, pfn) posts_sorted_by_likes = sorted(profile.get_posts(), key=lambda p: p.likes + p.comments, reverse=True) save_post_data_to_csv_by_profile('csvs', '_Profile_Posts_Export.csv', posts_sorted_by_likes)
async def check_instagram(self): channel = hkd.get_updates_channel(self.bot.guilds) with suppress(Exception): for instagram_id in self.firebase_ref.child('last_instagram_posts').get().keys(): last_post_id = int(self.firebase_ref.child('last_instagram_posts/{0}'.format(instagram_id)).get()) profile = Profile.from_username(self.insta_api.context, instagram_id) user_name = profile.full_name posted_updates = [] for post in profile.get_posts(): if post.mediaid <= last_post_id: break post_text = post.caption post_pic = post.url post_link = 'https://www.instagram.com/p/{0}/'.format(post.shortcode) posted_updates.append(post.mediaid) if instagram_id in hkd.WUG_INSTAGRAM_IDS.values(): colour = hkd.get_oshi_colour(hkd.get_wug_guild(self.bot.guilds), hkd.dict_reverse(hkd.WUG_INSTAGRAM_IDS)[instagram_id]) else: colour = Colour(0x242424) author = {} author['name'] = '{0} (@{1})'.format(user_name, instagram_id) author['url'] = 'https://www.instagram.com/{0}/'.format(instagram_id) author['icon_url'] = profile.profile_pic_url await channel.send(embed=hkd.create_embed(author=author, title='Post by {0}'.format(user_name), description=post_text, colour=colour, url=post_link, image=post_pic)) if posted_updates: self.firebase_ref.child('last_instagram_posts/{0}'.format(instagram_id)).set(str(max(posted_updates))) self.check_instagram.change_interval(minutes=random.randint(20, 30))
def get_user_hashtags(self): """ Defined the list of user hashtags from all user posts """ self.tags = set(chain( *[post.caption_hashtags for post in Profile.from_username(self.insta_loader.context, self.user).get_posts()]))
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 scrape_userstories(profilename_list): scraper = get_instance() scraper.login("", "") for profile_name in profilename_list: db_name = db.instagram.user_stories profile = Profile.from_username(scraper.context, profile_name) current_count = 0 skip_count = 0 for story in scraper.get_stories(): for item in story.get_items(): profile = item.profile storyitem = item.url storytype = item.typename timestamp = item.date_local print(profile) print(storyitem) print(storytype) print(timestamp) story_item = { "profile_name": profile, "story_type": storytype, "story_item": storyitem, "timestamp": timestamp, } result_status = db_name.insert_one(story_item)
def top_post(profile_detail): scraper = get_instance() X_percentage = 10 for profile_name in profile_detail: profile = Profile.from_username(scraper.context, profile_name) posts_sorted_by_likes = sorted(profile.get_posts(), key=lambda p: p.likes + p.comments, reverse=True) for post in islice(posts_sorted_by_likes, ceil(profile.mediacount * X_percentage / 100)): time = post.date_utc post_type = post.typename post_detail = post.url caption = post.caption hashtags = post.caption_hashtags likes = post.likes comment = post.comments item = { "Cafe": profile_name, "timestamp": time, "post_type": post_type, #The differetnt types of post will gather how many likes "post_detail": post_detail, "caption": caption, "number_of_likes": likes, #Unique number of likes "number_of_comments": comment, } result_status = db_name.insert_one(item) print(result_status)
def save_new_bottom_posts_by_profile(pfn): # pfn: profile name profile = Profile.from_username(IL.context, pfn) posts_sorted_by_time = sorted(profile.get_posts(), key=lambda p: p.date_utc, reverse=False) save_post_data_to_csv_by_profile('csvs', '_Profile_Posts_Export.csv', posts_sorted_by_time)
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)
def update_followings(profile: Profile): # TODO: use batches to update """ Scrape profile followings (profiles that are followed by profile) and update db :param profile: instaloader.Profile - whose followings to scrape :return: """ with sqlite3.connect(DB_LOCATION) as conn: cursor = conn.cursor() # add user profile to profiles sql_main_user = "******" \ "VALUES (?, ?, ?);" row = (profile.userid, profile.username, profile.full_name) cursor.execute(sql_main_user, row) sql_profiles = "REPLACE INTO profiles (userid, username, full_name) " \ "VALUES (?, ?, ?);" sql_followers = "REPLACE INTO followers (userid, follower_id) " \ "VALUES (?, ?);" for following in profile.get_followees(): logging.debug( f"Scraped {profile.username}'s following {following.username}") # update profiles table row_profiles = (following.userid, following.username, following.full_name) cursor.execute(sql_profiles, row_profiles) # update followers table row_followers = (following.userid, profile.userid) cursor.execute(sql_followers, row_followers) logging.info("Tables 'profiles' and 'followers' are updated")
def feed(update, context): fullmsg = update.message.text if fullmsg == "/feed": update.message.reply_text( '/feed [instagram username]\nPlease read /help') else: msg = fullmsg.replace("/feed ", "") 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) media = profile.mediacount update.message.reply_text( "Cooking your request 👨🍳\nProfile : " + query + "\nMedia Count : " + str(media) + "\nThis may take longer, take a nap I can handle this without you.") posts = profile.get_posts() try: L.posts_download_loop(posts, query) except Exception as e: context.bot.send_message(chat_id=update.message.chat_id, text="<b>ERROR\n" + str(e), parse_mode=telegram.ParseMode.HTML) return update.message.reply_text("Download Completed.\n🗄 Archiving files...") zf = zipfile.ZipFile(f"{query}.zip", "w") for dirname, subdirs, files in os.walk(query): zf.write(query) for filename in files: zf.write(os.path.join(dirname, filename)) zf.close() update.message.reply_text("Uploading to Telegram...") for zip_file in glob.glob("*.zip"): context.bot.send_document(chat_id=update.message.chat_id, document=open(zip_file, 'rb')) try: shutil.rmtree(query) os.remove(f"{query}.zip") except Exception: pass
def getProfileData(loginInstance, nickname): data = {} profile = Profile.from_username(loginInstance.context, nickname) data['fullName'] = profile.full_name data['biography'] = profile.biography data['imgUrl'] = profile.profile_pic_url return data
def scrape_user(self, username, interest): print(username) profile = Profile.from_username(L.context, username) print(profile) self.dataset[username] = {} data_dict = self.dataset[username] data_dict['interests'] = interest posts = [post for post in profile.get_posts()] self.scrape_posts(posts, data_dict)
def getProfilePosts(USERNAME): #mkdir("training/" + USERNAME) #save_path = 'training/' + USERNAME profile = Profile.from_username(L.context, USERNAME) save_target = USERNAME for post in profile.get_posts(): #, target=save_target if (not L.download_post(post)): print("ERROR: Problem downloading post - Line 30")
def get_profile(self, user: str): """Returns the profile of the given username Args: user (str): The username of the profile to be returned Returns: instaloader.Profile: The profile of the username given """ return Profile.from_username(self.__I_session.context, user)
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 get_tagged_users_per_each_post(USER): user_profile = Profile.from_username(loader.context, USER) posts = get_recent_posts(user_profile) tags_per_post = [] for post in posts: tagged = post.tagged_users print(tagged) print("Location: {}".format(post.location)) tags_per_post.append(tagged) print("All users per tag: {}".format(tags_per_post))
def imgs(update, context): profile = Profile.from_username(L.context, 'a_land3') count = 0 for post in profile.get_posts(): count += 1 context.bot.send_photo(chat_id=update.effective_chat.id, photo=post.url, caption=post.caption) if count == 5: break
def scrape_following_followers(filename): #Get instance scraper = get_instance() # Login scraper.login("jackthesmurffff2", "jack555") target_profiles = read_file(filename) for profile_name in target_profiles: profile = Profile.from_username(scraper.context, profile_name) print("Getting 'following'") following = profile.get_followees() newFollowing = sorted(following, key=lambda x: x.userid, reverse=False) print("Getting 'followers'") followers = profile.get_followers() newFollowers = sorted(followers, key=lambda x: x.userid, reverse=False) # saving following to csv print("Saving" + profile_name + "following to file...") fCount = 0 with open(profile_name + "_following.csv", 'w', newline='') as followingCSV: followingWriter = csv.writer(followingCSV, delimiter=',') for f in newFollowing: fCount += 1 followingWriter.writerow([f.userid, f.username]) os.renames(profile_name + "_following.csv", profile_name + "_" + str(fCount) + "_following.csv") # saving follower to csv print("Saving" + profile_name + "follower to file") fCount = 0 with open(profile_name + "_followers.csv", 'w', newline='') as followerCSV: followerWriter = csv.writer(followerCSV, delimiter=',') followerWriter.writerow([ 'User ID', 'Username', 'Fullname', 'Followed by you', 'Blocked by you', 'Follows you', 'Profile URL', 'Avatar URL' ]) for f in newFollowers: fCount += 1 followerWriter.writerow([f.userid, f.username]) # , f.followed_by_viewer, f.blocked_by_viewer, f.follows_viewer, f.profile_pic_url, f.get_profile_pic_url(), # f.external_url os.renames(profile_name + "_followers.csv", profile_name + "_" + str(fCount) + "_followers.csv") # --------------------------------------------------------------------------------------------- print("Saved") scraper.close()
def set_date_user(self, profile: Profile) -> datetime: """Obtains the latest post of the profile given and returns the date stamp of the post in UTC Args: profile (Profile): The profile to find the latest post of Returns: datetime: The UTC date stamp of the profiles latest post """ date_stamp = next(profile.get_posts()).date_utc return date_stamp