#!/usr/bin/python
# -*- coding: utf-8 -*-

import tweepy
from particle.common import DEBUG

def connect(config):
	"""
	Given 4 Environmental Variables, Connect to Twitter
	"""
	
	# load credentials
	consumer_key = config['twitter']['consumer_key']
	consumer_secret = config['twitter']['consumer_secret']
	access_token = config['twitter']['access_token']
	access_token_secret = config['twitter']['access_token_secret']

	# authenticate
	auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
	auth.set_access_token(access_token, access_token_secret)
	api = tweepy.API(auth)

	return api

def generate_list(config):
	# parse handles
	if isinstance(config['twitter']['list_screen_names'], basestring):
		screen_names = [config['twitter']['list_screen_names']]
	else:
		screen_names = config['twitter']['list_screen_names']

	api = connect(config)
	try:
		slug = config['twitter']['list_slug']
		owner_screen_name = config['twitter']['list_owner']
		api.create_list(slug)

	except tweepy.error.TweepError as e:
		print "ERROR\tTWT\t%s Already Exists for user %s" % (slug, owner_screen_name)
		print e
		return None
	else:
		for screen_name in screen_names:
			try:
				api.add_list_member(
					owner_screen_name = owner_screen_name,
					slug = slug,
					screen_name = screen_name
				)
			except:
				print "%s doesn't exist" % screen_name
			else:
				print "INFO\tTWT\tadding %s to list: %s for user: %s" % (screen_name, slug, owner_screen_name)

def get_list_timeline(config):
	api = connect(config)
	list_tweets = api.list_timeline(
					owner_screen_name = config['twitter']['list_owner'], 
					slug =  config['twitter']['list_slug'],
					count = config['twitter']['limit']
				)
	return [lt for lt in list_tweets]

@@ -610,16 +610,206 @@
 onfig):%0A
+%09slug = config%5B'twitter'%5D%5B'list_slug'%5D%0A%09owner_screen_name = config%5B'twitter'%5D%5B'list_owner'%5D%0A%09print %22INFO%5CtTWT%5Ctcreating or updating twitter list %25s for user: %25s%22 %25 (slug, owner_screen_name)%0A
 %09# parse
@@ -1040,102 +1040,8 @@
 ry:%0A
-%09%09slug = config%5B'twitter'%5D%5B'list_slug'%5D%0A%09%09owner_screen_name = config%5B'twitter'%5D%5B'list_owner'%5D%0A
 %09%09ap
@@ -1216,49 +1216,214 @@
 :%0A%09%09
-for screen_name in screen_names:%0A
+# get current screen names%0A%09%09list_members = frozenset(%5Bm.screen_name for m in  api.list_members(owner_screen_name, slug)%5D)%0A%09%09for screen_name in screen_names:%0A%09%09%09if screen_name not in list_members:%0A%09
 %09%09%09try:%0A
 %09%09%09%09
@@ -1418,16 +1418,17 @@
 %09%09%09try:%0A
+%09
 %09%09%09%09api.
@@ -1444,16 +1444,17 @@
 member(%0A
+%09
 %09%09%09%09%09own
@@ -1490,24 +1490,25 @@
 _name,%0A%09%09%09%09%09
+%09
 slug = slug,
@@ -1508,16 +1508,17 @@
 = slug,%0A
+%09
 %09%09%09%09%09scr
@@ -1544,18 +1544,20 @@
 ame%0A%09%09%09%09
+%09
 )%0A
+%09
 %09%09%09excep
@@ -1559,16 +1559,17 @@
 except:%0A
+%09
 %09%09%09%09prin
@@ -1606,22 +1606,24 @@
 name%0A%09%09%09
+%09
 else:%0A
+%09
 %09%09%09%09prin

check for list membership before adding