SHARE
TWEET

Untitled

a guest Aug 25th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import twitter
  2.  
  3. api = twitter.Api(consumer_key='consumer_key',
  4.                   consumer_secret='consumer_secret',
  5.                   access_token_key='access_token_key',
  6.                   access_token_secret='access_token_secret')
  7.  
  8. u = api.VerifyCredentials()
  9.  
  10. def create_list(name='Untitled', mode='private'):
  11.     return api.CreateList(name, mode)
  12.  
  13. def in_batches_of(n, items, func):
  14.     batches = [items[i:i+n] for i in range(0, len(items), n)]
  15.     for batch in batches:
  16.         func(batch)
  17.  
  18. def set_list(list_id=None, new_members=set()):
  19.     old_members = api.GetListMembers(list_id = list_id)
  20.     old_members = set(map(lambda x: x.id, old_members))
  21.     to_add = new_members - old_members
  22.     to_remove = old_members - new_members
  23.    
  24.     if len(to_add) > 0:
  25.         in_batches_of(100, list(to_add), lambda ids: api.CreateListsMember(list_id=list_id, user_id=ids))
  26.     if len(to_remove) > 0:
  27.         in_batches_of(100, list(to_remove), lambda ids: api.DestroyListsMember(list_id=list_id, user_id=ids))
  28.  
  29. def create_list_of_my_followers(mode="private"):
  30.     following_me_list = create_list("Following Me", mode=mode)
  31.     my_followers = set(api.GetFollowerIDs())
  32.     set_list(list_id=following_me_list.id, new_members=my_followers)
  33.  
  34. def create_list_of_my_mutuals(mode="private"):
  35.     new_twitter_list = create_list("My Mutuals", mode=mode)
  36.     my_followers = set(api.GetFollowerIDs())
  37.     people_i_follow = set(api.GetFriendIDs())
  38.     set_list(list_id=new_twitter_list.id, new_members=my_followers.intersection(people_i_follow))
  39.  
  40. # Other fun stuff
  41.  
  42. def get_intersection_following(handles=[]): # ! Rate Limiting
  43.     following = []
  44.     for u in handles:
  45.         api.GetFriends(screen_name=u)
  46.         following.append(
  47.             set(api.GetFriends(screen_name=u))
  48.         )
  49.     return set.intersection(*following)
  50.  
  51. def get_intersection_followers(handles=[]): # ! Rate Limiting
  52.     followers = []
  53.     for u in handles:
  54.         api.GetFollowers(screen_name=u)
  55.         followers.append(
  56.             set(api.GetFollowers(screen_name=u))
  57.         )
  58.     return set.intersection(*followers)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top