/
09_collect_congress.py
81 lines (57 loc) · 2.44 KB
/
09_collect_congress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Collect Twitter IDs and party affiliations of congresspeople.
We obtain the list of all Democrats and Republicans in Congress (both House
and Senate). This will later be used to validate our model.
Assumptions:
- API authorization is properly setup
"""
import csv
import itertools
import tweepy
from auth import authorize
def write_users(users, party):
"""Given list of users, write relevant information to CSV file.
For Republicans, info written to congress_republicans.csv.
For Democrats, info written to congress_democrats.csv.
users: iterator of users
party: string, "R" (republican) or "D" (democrat)
"""
accounts = []
# Get party, user ID, username, and number of followers (the useful info)
for user in users:
party = party
user_id = user.id_str
username = user.name
followers = user.followers_count
user_info = (user_id, username, followers, party)
accounts.append(user_info)
# Write to appropriate file
if party == "R":
fname = "congress_republicans.csv"
elif party == "D":
fname = "congress_democrats.csv"
with open(fname, "w") as outfile:
writer = csv.writer(outfile)
for acct_info in accounts:
try:
writer.writerow(acct_info)
except:
continue
print("Wrote list to {}".format(fname))
def get_congresspeople(auth):
"""Get lists of congresspeople and writes information to CSV file."""
# Authorize API
api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True)
# List of all Republicans in Congress from @SenateRepublicans and
# @HouseRepublicans
house_reps = tweepy.Cursor(api.list_members, "HouseGOP", "house-republicans").items()
senate_reps = tweepy.Cursor(api.list_members, "senategop", "senaterepublicans").items()
republicans = itertools.chain(house_reps, senate_reps)
write_users(republicans, "R")
# List of all Democrats in Congress from @TheDemocrats
house_dems = tweepy.Cursor(api.list_members, "TheDemocrats", "house-democrats").items()
senate_dems = tweepy.Cursor(api.list_members, "TheDemocrats", "senate-democrats").items()
democrats = itertools.chain(house_dems, senate_dems)
write_users(democrats, "D")
if __name__ == "__main__":
auth = authorize()
get_congresspeople(auth)