Esempio n. 1
0
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()
Esempio n. 2
0
def main(wf):
    """Configure options and access tokens for the Instastalk workflow"""
    
    
    # Get query passed from alfred
    query = wf.args[0]
    # An empty query means the script is being run for the first time
    if query == '':
        wf.alfred_items = AlfredItems(wf, name='configure_special_info')
        # Check if the settings file exists
        settings = wf.settings
        if not settings:
            configure_initial()
        else:
            configure_main()
    
    # Otherwise find the previously selected item from query
    else:
        firstchar = query[0]
        # Default values for command and data
        command = None
        data = None
        unival = ord(firstchar)
        if unival in AlfredItems.non_printing_univals():
            # Found a special character key from autocomplete of previous script
            info = wf.cached_data('configure_special_info', max_age=FROM_CACHE)
            # Get the selected command and associated data from previously run script
            #  'info' contains a list of all options to select from previous script
            #  'unival' is the ordinal of the unicode character passed from autocompleting
            #     the actionied item from previous script
            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('Received special unicode value, command: {command}'.format(command=COMMANDS[command]))
            
            # Initialize generators for special info
            # First find previously chosen unival to exclude it from new range
            previous_unival = unival
            wf.alfred_items = AlfredItems(wf, previous_unival, name='configure_special_info')
            # Send previous info to alfred_items in case user accesses the previous information in alfred by scrolling
            wf.alfred_items.special_info.send({previous_unival: (command, data)})
        else:
            wf.alfred_items = AlfredItems(wf, name='configure_special_info')

        # Execute navigtion function appropriate to the command
        if command == CHANGE_PRIMARY:
            change_primary()
        elif command == MAKE_PRIMARY:
            new_primary_token = data
            exchange_primary_and_secondary_tokens(new_primary_token)
            configure_main()
        elif command == NEW_ACCOUNT:
            associate_instagram_account('secondary')
        elif command == REMOVE_ACCOUNT:
            remove_account()
        elif command == NEW_PRIMARY_FROM_SECONDARY:
            new_primary_from_secondary()
        elif command == REMOVE_PRIMARY:
            remove_primary()
        elif command == REMOVE_SECONDARY:
            secondary_token = data
            remove_secondary(secondary_token)
        elif command == NEW_PRIMARY:
            secondary_token = data
            new_primary(secondary_token)
    # 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()