+def get_slug():
+ """
+ Get Slug Decorator
+ """
+ # Get list name
+ list_name = raw_input(light_magenta('Give me the list\'s name: '))
+ # Get list name and owner
+ try:
+ owner, slug = list_name.split('/')
+ if slug.startswith('@'):
+ slug = slug[1:]
+ return owner, slug
+ except:
+ printNicely(
+ light_magenta('List name should follow "@owner/list_name" format.'))
+ raise Exception('Wrong list name')
+
+
+def show_lists(t):
+ """
+ List list
+ """
+ rel = t.lists.list(screen_name=g['original_name'])
+ if rel:
+ print_list(rel)
+ else:
+ printNicely(light_magenta('You belong to no lists :)'))
+
+
+def list_home(t):
+ """
+ List home
+ """
+ owner, slug = get_slug()
+ res = t.lists.statuses(
+ slug=slug,
+ owner_screen_name=owner,
+ count=c['LIST_MAX'],
+ include_entities=False)
+ for tweet in res:
+ draw(t=tweet)
+ printNicely('')
+
+
+def list_members(t):
+ """
+ List members
+ """
+ owner, slug = get_slug()
+ # Get members
+ rel = {}
+ next_cursor = -1
+ while next_cursor != 0:
+ m = t.lists.members(
+ slug=slug,
+ owner_screen_name=owner,
+ cursor=next_cursor,
+ include_entities=False)
+ for u in m['users']:
+ rel[u['name']] = '@' + u['screen_name']
+ next_cursor = m['next_cursor']
+ printNicely('All: ' + str(len(rel)) + ' members.')
+ for name in rel:
+ user = ' ' + cycle_color(name)
+ user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ')
+ printNicely(user)
+
+
+def list_subscribers(t):
+ """
+ List subscribers
+ """
+ owner, slug = get_slug()
+ # Get subscribers
+ rel = {}
+ next_cursor = -1
+ while next_cursor != 0:
+ m = t.lists.subscribers(
+ slug=slug,
+ owner_screen_name=owner,
+ cursor=next_cursor,
+ include_entities=False)
+ for u in m['users']:
+ rel[u['name']] = '@' + u['screen_name']
+ next_cursor = m['next_cursor']
+ printNicely('All: ' + str(len(rel)) + ' subscribers.')
+ for name in rel:
+ user = ' ' + cycle_color(name)
+ user += color_func(c['TWEET']['nick'])(' ' + rel[name] + ' ')
+ printNicely(user)
+
+
+def list_add(t):
+ """
+ Add specific user to a list
+ """
+ owner, slug = get_slug()
+ # Add
+ user_name = raw_input(light_magenta('Give me name of the newbie: '))
+ if user_name.startswith('@'):
+ user_name = user_name[1:]
+ try:
+ t.lists.members.create(
+ slug=slug,
+ owner_screen_name=owner,
+ screen_name=user_name)
+ printNicely(light_green('Added.'))
+ except:
+ printNicely(light_magenta('I\'m sorry we can not add him/her.'))
+
+
+def list_remove(t):
+ """
+ Remove specific user from a list
+ """
+ owner, slug = get_slug()
+ # Remove
+ user_name = raw_input(light_magenta('Give me name of the unlucky one: '))
+ if user_name.startswith('@'):
+ user_name = user_name[1:]
+ try:
+ t.lists.members.destroy(
+ slug=slug,
+ owner_screen_name=owner,
+ screen_name=user_name)
+ printNicely(light_green('Gone.'))
+ except:
+ printNicely(light_magenta('I\'m sorry we can not remove him/her.'))
+
+
+def list_subscribe(t):
+ """
+ Subscribe to a list
+ """
+ owner, slug = get_slug()
+ # Subscribe
+ try:
+ t.lists.subscribers.create(
+ slug=slug,
+ owner_screen_name=owner)
+ printNicely(light_green('Done.'))
+ except:
+ printNicely(
+ light_magenta('I\'m sorry you can not subscribe to this list.'))
+
+
+def list_unsubscribe(t):
+ """
+ Unsubscribe a list
+ """
+ owner, slug = get_slug()
+ # Subscribe
+ try:
+ t.lists.subscribers.destroy(
+ slug=slug,
+ owner_screen_name=owner)
+ printNicely(light_green('Done.'))
+ except:
+ printNicely(
+ light_magenta('I\'m sorry you can not unsubscribe to this list.'))
+
+
+def list_own(t):
+ """
+ List own
+ """
+ rel = []
+ next_cursor = -1
+ while next_cursor != 0:
+ res = t.lists.ownerships(
+ screen_name=g['original_name'],
+ cursor=next_cursor)
+ rel += res['lists']
+ next_cursor = res['next_cursor']
+ if rel:
+ print_list(rel)
+ else:
+ printNicely(light_magenta('You own no lists :)'))
+
+
+def list_new(t):
+ """
+ Create a new list
+ """
+ name = raw_input(light_magenta('New list\'s name: '))
+ mode = raw_input(light_magenta('New list\'s mode (public/private): '))
+ description = raw_input(light_magenta('New list\'s description: '))
+ try:
+ t.lists.create(
+ name=name,
+ mode=mode,
+ description=description)
+ printNicely(light_green(name + ' list is created.'))
+ except:
+ printNicely(red('Oops something is wrong with Twitter :('))
+
+
+def list_update(t):
+ """
+ Update a list
+ """
+ slug = raw_input(light_magenta('Your list that you want to update: '))
+ name = raw_input(light_magenta('Update name (leave blank to unchange): '))
+ mode = raw_input(light_magenta('Update mode (public/private): '))
+ description = raw_input(light_magenta('Update description: '))
+ try:
+ if name:
+ t.lists.update(
+ slug='-'.join(slug.split()),
+ owner_screen_name=g['original_name'],
+ name=name,
+ mode=mode,
+ description=description)
+ else:
+ t.lists.update(
+ slug=slug,
+ owner_screen_name=g['original_name'],
+ mode=mode,
+ description=description)
+ printNicely(light_green(slug + ' list is updated.'))
+ except:
+ printNicely(red('Oops something is wrong with Twitter :('))
+
+
+def list_delete(t):
+ """
+ Delete a list
+ """
+ slug = raw_input(light_magenta('Your list that you want to update: '))
+ try:
+ t.lists.destroy(
+ slug='-'.join(slug.split()),
+ owner_screen_name=g['original_name'])
+ printNicely(light_green(slug + ' list is deleted.'))
+ except:
+ printNicely(red('Oops something is wrong with Twitter :('))
+
+
+def list():
+ """
+ Twitter's list
+ """
+ t = Twitter(auth=authen())
+ # List all lists or base on action
+ try:
+ g['list_action'] = g['stuff'].split()[0]
+ except:
+ show_lists(t)
+ return
+ # Sub-function
+ action_ary = {
+ 'home': list_home,
+ 'all_mem': list_members,
+ 'all_sub': list_subscribers,
+ 'add': list_add,
+ 'rm': list_remove,
+ 'sub': list_subscribe,
+ 'unsub': list_unsubscribe,
+ 'own': list_own,
+ 'new': list_new,
+ 'update': list_update,
+ 'del': list_delete,
+ }
+ try:
+ return action_ary[g['list_action']](t)
+ except:
+ printNicely(red('Please try again.'))
+
+