def main(wf): # Check for an update if wf.update_available: wf.add_item('New version available', 'Action this item to install the update', autocomplete='workflow:update', icon=ICON_INFO) # Check settings for access_token settings = wf.settings if 'primary_access_token' not in settings.keys(): wf.add_item( title='Run the configuration to connect an Instagram account', subtitle='Type: "stalk.configuration"', valid=False, autocomplete='.configuration' ) # Process the query, find the encoded command if one exists from autocomplete # of previously run script else: data = None command = None alt_command = False # Get query passed from Alfred if len(wf.args[0]): # Check for an 'alt' activated command if wf.args[0][0] == unichr(ALT): # ALT command alt_command = True if len(wf.args[0]) == 1: # The delete button was used to eliminate all but the ALT character query = False else: log.debug(u'Found ALT command') query = wf.args[0][1:] else: query = wf.args[0] log.debug(u'Query: <{0}>, len(query) = {1}'.format(query, len(query))) # Process query, the first character may be a non-printing unicode charcter # that indicates the command the script should run if query: firstchar = query[0] unival = ord(firstchar) if unival in AlfredItems.non_printing_univals(): log.debug('Found special unicode! value={0}'.format(unival)) # We have a special character key from autocomplete of previous script # Find the cached information from previously run script containing data # for each actionable alfred item info = wf.cached_data('special_info', max_age=FROM_CACHE) if info and len(query) == 1: # The query passed from Alfred should contain a special, non-printing, # unicode character whose ordinal is the key for the cached info dict command, data = info[unival] # Cache value to exclude it from next time script is run. Alfred will # not rerun the script if the autocomplete value is the same as before wf.cache_data('unival',unival) log.debug(u'COMMAND={0}'.format(COMMANDS[command])) else: # More than one character in query indicates a search command = SEARCH query = query[1:] # Initialize generators for special info previous_unival = unival wf.alfred_items = AlfredItems(wf, previous_unival) if data: # If user scrolls through alfred history the previous command and data # may be recovered if reissued here. The previous unicode value is # excluded from new data that is issued so there is no conflict of # items using the same unicode value old_data = {previous_unival: (command, data) } wf.alfred_items.special_info.send(old_data) else: # No special unicode character found wf.alfred_items = AlfredItems(wf) command = SEARCH else: # There is no query, only the ALT character command = FAVORITES wf.alfred_items = AlfredItems(wf) else: # An empty query means the script is being run for the first time command = FAVORITES # No previous unival in query wf.alfred_items = AlfredItems(wf) log.debug('Final COMMAND:{0}'.format(COMMANDS[command])) # Execute command if command is LOAD_USER: load_user(data['user_id'], data['username']) elif command is FAVORITES: load_favorites() elif command is ADD_FAV: add_favorite(data['user_id'], data['username']) elif command is REMOVE_FAV: remove_favorite(data['user_id'], data['username']) load_favorites() elif command is SEARCH: search_users(query) elif command is CHECK_FOLLOWS: check_user_follows(data['user_id'], data['username']) elif command is MAKE_FAVORITE: add_or_remove_favorite(data['user_id'], data['username']) elif command is GET_LIKES: if alt_command: cached_likes(data['user_id'], data['username']) else: user_id = data['user_id'] process_alias = user_id + '.get_likes' args = ['/usr/bin/python', wf.workflowfile('get_recent_likes.py'), user_id, wf.likesdir, wf.mediadir] if background.is_running(process_alias): log.debug('Process `{alias}` is already running'.format(alias=process_alias)) else: background.run_in_background(process_alias, args) load_user(data['user_id'], data['username']) elif command is CACHED_LIKES: cached_likes(data['user_id'], data['username']) elif command is CHECK_FOLLOWED_BY: check_user_followed_by(data['user_id'], data['username']) elif command is RECENT_MEDIA: recent_media(data['user_id'], data['username']) else: # Bad command log.debug(u'Received unknown command=<{0}>'.format(str(command))) # Write the item data to a file so the item the user selected determines how the script is run next wf.alfred_items.special_info.close() # Send XML formatted result to Alred wf.send_feedback()
def load_user(user_id, username): """Display details for a particular user""" # Get informaiton about user from instagram api user_info = get_user_info(user_id) # Display user user = Grammie(wf(), user_id, username, age_info=5*MINUTE) user.display_info() # Items to display only if there is user information if user_info: # Display item to show recent media special_unicode_value = prepare_item_with_command(RECENT_MEDIA, user_id, username) wf().add_item( title='Recent media ({media} total media)'.format(media=user_info['media_count']), autocomplete=unichr(special_unicode_value), icon=ICON_PICTURE ) # Display item to search for recent likes special_unicode_value = prepare_item_with_command(GET_LIKES, user_id, username) if background.is_running(user_id + '.get_likes'): subtitle = '...Now searching for media liked by {user}'.format(user=username) else: # Check if previous data is availiable likes_path = os.path.join(wf().likesdir, user_id + '.json') if os.path.exists(likes_path): age_of_like_data = get_age_cached_data(likes_path) subtitle = "Age of data: " + age_of_like_data else: subtitle = '' wf().add_item( title='Search for media liked by {user}'.format(user=username), subtitle=subtitle, autocomplete=unichr(special_unicode_value), icon=ICON_FAVORITE, valid=True, arg=unichr(special_unicode_value), modifier_subtitles={ u'alt':'Retrieve previously found likes' } ) # Display item to check new and removed follows special_unicode_value = prepare_item_with_command(CHECK_FOLLOWS, user_id, username) # Find age of data follow_data_path = os.path.join(wf().followdir, user_id + '_follows.json') follow_data_age = get_age_cached_data(follow_data_path) if follow_data_age != '': subtitle = 'Age of data: {age}'.format(age=follow_data_age) else: subtitle = '' wf().add_item( title='New and removed follows ({follows} current follows)'.format(follows=user_info['follows']), subtitle=subtitle, autocomplete=unichr(special_unicode_value), icon=ICON_GROUP ) # Display item to check for new and removed users following our user special_unicode_value = prepare_item_with_command(CHECK_FOLLOWED_BY, user_id, username) # Find age of data followers_data_path = os.path.join(wf().followdir, user_id + '_followed_by.json') followers_data_age = get_age_cached_data(followers_data_path) if followers_data_age != '': subtitle = 'Age of data: {age}'.format(age=followers_data_age) else: subtitle = '' wf().add_item( title='New and removed followers ({followers} current followers)'.format(followers=user_info['followers']), subtitle=subtitle, autocomplete=unichr(special_unicode_value), icon=ICON_GROUP ) # Ask to add or remove as a favorite make_add_or_remove_favorite_item(user_id, username)