Skip to content

Instantly share code, notes, and snippets.

@ClementC
Created March 16, 2017 19:13
Show Gist options
  • Save ClementC/f74084ac169921572dd9b551366b85f9 to your computer and use it in GitHub Desktop.
Save ClementC/f74084ac169921572dd9b551366b85f9 to your computer and use it in GitHub Desktop.
Checking user followed on Twitter to see which ones are not added to a list.
# coding: utf-8
import twitter
from tqdm import tqdm
from collections import defaultdict
import pickle
import pandas as pd
# Twitter API token
auth = ( , # 25 alphanum characters hash
, # 50 alphanum characters hash
, # 9 numbers - 40 alphanum characters hash
,) # 45 alphanum characters hash
# Including @ at the beginning
my_handle = ""
api = twitter.Api(*auth)
my_lists = api.GetListsList()
following = api.GetFriendIDs()
following_mapping = defaultdict(list)
for l in tqdm(my_lists):
if l.full_name.startswith(my_handle):
for m in api.GetListMembers(l.id):
following_mapping[m.id].append(l)
in_no_list = set(following).difference(set(following_mapping.keys()))
print("%d following not in lists." % len(in_no_list))
in_no_list_members = [api.GetUser(elem) for elem in tqdm(in_no_list)]
with open("in_no_list_members.pickle", "wb") as f:
pickle.dump(in_no_list_members, f)
df = pd.DataFrame(data=[elem.AsDict() for elem in in_no_list_members],
columns=["name", "screen_name"])
df["url"] = "https://www.twitter.com/" + df.screen_name
df.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment