Beispiel #1
0
def associate_instagram_account(account_type):
    """Get permission from user to associate their instrgram account"""
    # Start a subprocess to load a webpage to get instagram permission
    unauthorized_api = InstagramAPI(**CONFIG)
    scope = ['comments', 'likes', 'relationships']
    authorize_url = unauthorized_api.get_authorize_url(scope=None)
    args = ['/usr/bin/python', wf().workflowfile('instagram_connect.py'), account_type]
    # if background.is_running('connect'):
    #     log.debug('Process `connect` is already running')
    # else:
    background.run_in_background('connect', args, timeout=120)
                              
    wf().add_item( title='Connect your instagram account', 
                   valid=True,
                   arg=authorize_url)
Beispiel #2
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()