def cached_likes(user_id, username): """Display cached likes""" # Make a go back item special_unicode_value = prepare_item_with_command(LOAD_USER, user_id, username) wf().add_item( title='Go back to {user}'.format(user=username), valid=False, autocomplete=unichr(special_unicode_value), icon=ICON_BACK) # Retrieve the liked media that is cached user_likes_path = os.path.join(wf().likesdir, user_id + '.json') if os.path.exists(user_likes_path): with open(user_likes_path, 'r') as likes_file: likes = json.load(likes_file) # Convert dictionary to media objects likes = [models.Media.object_from_dictionary(m) for m in likes] # Display likes if len(likes): display_media(likes, wf().mediadir) else: # No recent likes to display wf().add_item( title='No recent likes found', icon=ICON_INFO) else: # No likes searched for wf().add_item(title='No likes found, first search for likes', icon=ICON_INFO)
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("_", " "))
def __init__(self): # Get the access tokens saved in settings.json file info = plistlib.readPlist("info.plist") bundleid = info["bundleid"] settings_path = os.path.join( os.path.expanduser("~/Library/Application Support/Alfred 2/Workflow Data/"), bundleid, "settings.json" ) with open(settings_path, "rb") as settings_file: settings = json.load(settings_file) primary_access_token = settings.get("primary_access_token", None) secondary_access_tokens = settings.get("secondary_access_tokens", []) # Do not initialize without a primary_access_token if primary_access_token: self._num_secondary_tokens = len(secondary_access_tokens) self.api_calls_remaining = [None for i in range(self._num_secondary_tokens)] self.api_last_call = [None for i in range(self._num_secondary_tokens)] # Instantiate primary api self.primary_api = InstagramAPI( access_token=primary_access_token, client_secret=MyInstagramAPI.client_secret ) # Instantiate secondary api's self.secondary_apis = [] for secondary_token in secondary_access_tokens: self.secondary_apis.append( InstagramAPI(access_token=secondary_token, client_secret=MyInstagramAPI.client_secret) ) self.test_api(self.primary_api) self.primary_follows = self.get_primary_follows() self.secondary_idx = -1 # Will be incremented to 0 in new_secondary_api self.secondary_api_gen = self.new_secondary_api() self.secondary_api = self.secondary_api_gen.next() self.secondary_follows = self.get_secondary_follows() self.api_error_handler = HandleAPIErrors(self) self.last_used_api = None