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 update_followers(profile: Profile): """ Scrape profile followers and update db tables 'profiles' and 'followers' """ # TODO: use batches to update # TODO: check for existing db records and remove followers from db if they do not exist 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) # remove existing records because we are going to collect them again cursor.execute( f"DELETE FROM followers WHERE userid = {profile.userid}") # fill profiles table with follower profiles # and fill followers table with profiles relations sql_profiles = "REPLACE INTO profiles (userid, username, full_name) " \ "VALUES (?, ?, ?);" sql_followers = "REPLACE INTO followers (userid, follower_id) " \ "VALUES (?, ?);" for follower in profile.get_followers(): logging.debug( f"Scraped {profile.username}'s follower {follower.username}") # update profiles table row_profiles = (follower.userid, follower.username, follower.full_name) cursor.execute(sql_profiles, row_profiles) # update followers table row_followers = (profile.userid, follower.userid) cursor.execute(sql_followers, row_followers) logging.info("Tables 'profiles' and 'followers' are updated")