def get_recent_likes(user_id): """Find recently liked media""" # Find recent likes likes = scratch.stalk_likes_in_follows(user_id) likes = likes[0:NUM_UNICODE_CHARS] # Only process up to first 30 items # Convert likes to a saveable format likes = [models.media.dictionary_from_object(u) for u in likes] # Save the liked media user_likes_dir = os.path.join(wf().likesdir, user_id + '.json') with open(user_likes_dir, 'wb') as likes_file: likes_file.write(json.dumps(likes)) # Download the media scratch.download_images(likes, wf().mediadir, resolution='thumbnail')
def wrapped_follow_function(user_id, update_data=True, data_dir=None): if not data_dir: data_dir = os.path.join(HOME, "Documents", "Instagram", "user_data") # Call instagram api for follows of followed_by current_data = self.instagram_api_function(user_id, max_pages=500) # Retrieve stored version of data if exists stored_data_exists = False data_path = os.path.join(data_dir, user_id + "_{0}.json".format(self.follow_type)) if os.path.exists(data_path): with open(data_path, "r") as data_file: temp_user_data = json.load(data_file) # Convert data to user objects stored_data = [models.User.object_from_dictionary(u) for u in temp_user_data] stored_data_exists = True # Update stored data to current version if update_data: # Make directory structure if not os.path.exists(data_dir): os.makedirs(data_dir) with open(data_path, "w") as data_file: data_dict = [u.dictionary_from_object() for u in current_data] data_file.write(json.dumps(data_dict)) # Compare current and stored data (if stored version exists) if stored_data_exists: # Convert data objects to UserSets set_stored_data = UserSet(stored_data) set_current_data = UserSet(current_data) # Compare sets to find new and removed users new_users = set_current_data - set_stored_data removed_users = set_stored_data - set_current_data myprint("New {0}:".format(self.follow_type).replace("_", " ")) for u in new_users: myprint("\t{0}".format(u.username)) myprint("Removed {0}:".format(self.follow_type).replace("_", " ")) for u in removed_users: myprint("\t{0}".format(u.username)) return (list(new_users), list(removed_users)) else: myprint("No stored version of ".format(self.follow_type).replace("_", " ")) return None if update_data: myprint("Updated {0} data to current information".format(self.follow_type).replace("_", " "))
# Issue notification that a search is started Notifier.notify( title="Instastalk", subtitle="User: {user}".format(user=username), message="Searching {follows} follows for likes".format(follows=count_follows), ) # Find recent likes likes = scratch.stalk_likes_in_follows(user_id) likes_recent = likes[0:MAX_LIKED_MEDIA] # Only process up to first 50 items # Convert likes to a saveable format likes_recent_dict = [models.Media.dictionary_from_object(u) for u in likes] # Save the liked media if not os.path.exists(likes_dir): os.makedirs(likes_dir) user_likes_path = os.path.join(likes_dir, user_id + ".json") with open(user_likes_path, "wb") as likes_file: likes_file.write(json.dumps(likes_recent_dict)) # Download the media scratch.download_images(likes, media_dir, resolution="thumbnail") # Issue notification that the search is complete num_likes = len(likes_recent_dict) Notifier.notify( title="Instastalk", subtitle="User: {user}".format(user=username), message="Found {likes} liked media".format(likes=num_likes), sound="default", )