def remove_secondary(secondary_token): """Delete a secondary account""" wf().settings['secondary_access_tokens'].remove(secondary_token) wf().settings.save() api = InstagramAPI(client_secret=CONFIG['client_secret'], access_token=secondary_token) user = api.user('self') wf().add_item( title='Removed {user}'.format(user=user.username) )
def remove_primary(): """Delete the primary account""" token = wf().settings.pop('primary_access_token') wf().settings.save() api = InstagramAPI(client_secret=CONFIG['client_secret'], access_token=token) user = api.user('self') wf().add_item( title='Removed {user}'.format(user=user.username) )
def remove_account(): """List all accounts registered to remove one of them""" # Get the accounts already associated with the workflow primary_access_token = wf().settings.get('primary_access_token', None) secondary_access_tokens = wf().settings.get('secondary_access_tokens', []) num_secondary_accounts = len(secondary_access_tokens) # First list the primary account primary_api = InstagramAPI(access_token=primary_access_token, client_secret=CONFIG['client_secret']) primary_user = primary_api.user('self') if num_secondary_accounts > 0: special_unicode_value = prepare_item_with_command(NEW_PRIMARY_FROM_SECONDARY) subtitle = 'You will be prompted to select a new primary account' else: special_unicode_value = prepare_item_with_command(REMOVE_PRIMARY) subtitle = 'Warning: you won\'t have any Instagram accounts if you remove this one' wf().add_item( title='Remove {user} (primary)'.format(user=primary_user.username), subtitle=subtitle, valid=False, autocomplete=unichr(special_unicode_value) ) # List all of the secondary accounts for secondary_token in secondary_access_tokens: secondary_api = InstagramAPI(client_secret=CONFIG['client_secret'], access_token=secondary_token) secondary_user = secondary_api.user('self') special_unicode_value = prepare_item_with_command(REMOVE_SECONDARY, secondary_token) wf().add_item( title='Remove {user}'.format(user=secondary_user.username), valid=False, autocomplete=unichr(special_unicode_value) )
def change_primary(): """Change the primary account""" # Get the accounts already associated with the workflow primary_access_token = wf().settings.get('primary_access_token', None) secondary_access_tokens = wf().settings.get('secondary_access_tokens', []) # If a primary doesn't exist, need to make one if not primary_access_token: associate_instagram_account('primary') else: # Otherwise make items for each account primary_api = InstagramAPI(access_token=primary_access_token, client_secret=CONFIG['client_secret']) primary_user = primary_api.user('self') special_unicode_value = prepare_item_with_command(MAKE_PRIMARY, primary_access_token) wf().add_item( title='User: {user} (Currently Primary)'.format(user=primary_user.username), subtitle='Keep as primary', valid=False, autocomplete=unichr(special_unicode_value) ) # Make items for secondary accounts for secondary in secondary_access_tokens: secondary_api = InstagramAPI(client_secret=CONFIG['client_secret'], access_token=secondary) secondary_user = secondary_api.user('self') special_unicode_value = prepare_item_with_command(MAKE_PRIMARY, secondary) wf().add_item( title='User: {user}'.format(user=secondary_user.username), subtitle='Make primary', valid=False, autocomplete=unichr(special_unicode_value) )
def new_primary(secondary_token): """Make a secondary account the primary account, deleting the primary token""" old_primary_token = wf().settings['primary_access_token'] old_primary_api = InstagramAPI(client_secret=CONFIG['client_secret'], access_token=old_primary_token) old_primary = old_primary_api.user('self') wf().settings['primary_access_token'] = secondary_token wf().settings['secondary_access_tokens'].remove(secondary_token) api = InstagramAPI(client_secret=CONFIG['client_secret'], access_token=secondary_token) user = api.user('self') wf().add_item( title='Removed {old_primary}, made {new_primary} primary'.format( old_primary=old_primary.username, new_primary=user.username) )
def new_primary_from_secondary(): """Make a secondary account primary, remove primary account""" # List all secondary accounts secondary_access_tokens = wf().settings.get('secondary_access_tokens', []) for secondary_token in secondary_access_tokens: secondary_api = InstagramAPI(client_secret=CONFIG['client_secret'], access_token=secondary_token) secondary_user = secondary_api.user('self') special_unicode_value = prepare_item_with_command(NEW_PRIMARY, secondary_token) wf().add_item( title='Make {user} the primary user'.format(user=secondary_user.username), subtitle='Removes current primary account', valid=False, autocomplete=unichr(special_unicode_value) )
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)