forked from karthiktadepalli1/econ-twitter-network
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_econs.py
More file actions
71 lines (56 loc) · 2.61 KB
/
Copy pathget_econs.py
File metadata and controls
71 lines (56 loc) · 2.61 KB
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
"""
A script to grab economists on Twitter using the Tweepy API, and save them to the dataset econs.csv.
"""
#--------------IMPORTS-----------------
import tweepy
import pandas as pd
import numpy as np
#-------------SET UP TWEEPY--------------
# import API keys, private
keys = pd.read_csv("api.csv")
keys = dict(zip(list(keys.columns), list(keys.iloc[0,])))
# make an AppAuth API for faster rates
auth = tweepy.AppAuthHandler(keys['key'], keys['secret'])
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
#-------------GET REPEC ECONOMISTS---------------
# Use tweepy to scrape the RePEc Twitter list, found at https://twitter.com/i/lists/1087053821786947584
lst = []
repec = tweepy.Cursor(api.list_members, list_id=1087053821786947584).items()
for user in repec:
l = [user.name, user.screen_name, user.id_str, user.friend_count, user.followers_count, user.verified, user.favourites_count,
user.created_at, user]
lst.append(l)
cols = ['name', 'handle', 'id', 'following', 'followers', 'verified', 'favorites', 'join_date', 'object']
repec = pd.DataFrame(lst, columns = cols)
print(repec.info())
#-------------GET ECONOMISTS FROM TWEETS DIRECTLY-----------------
# Supplement the RePEc dataset with people who tweeted on #EconTwitter twice in the last 30 days (grabbed on September 14th, 2020)
ect = tweepy.Cursor(api.search, q = "#econtwitter").items(10000)
repecs = list(repec.handle)
lst = []
dct = {}
for tweet in ect:
auth = tweet.author
if auth.screen_name in dct.keys():
dct[auth.screen_name] = True
else:
dct[auth.screen_name] = False
if auth.screen_name not in repecs and dct[auth.screen_name]:
repecs.append(auth.screen_name)
l = [auth.name, auth.screen_name, auth.id_str, auth.friends_count, auth.followers_count, auth.verified,
auth.favourites_count, auth.created_at, auth]
lst.append(l)
supp = pd.DataFrame(lst, columns = cols)
# identify the humans vs bots/institutional accounts through naming proxies
keywords = ["economics", "econometrics", "bot", "economists", "microeconomics", "economic", "center", "centre", "university"]
def check_human(name):
l = [word in name.lower() for word in keywords]
return(not (True in l))
supp.is_human = supp.name.map(check_human)
#-----------------MERGE AND SAVE-------------------
econs = pd.concat([repec, supp])
econs.dropna(inplace=True)
econs.join_date = pd.to_datetime(econs.join_date)
econs.set_index("id", inplace=T)
econs.to_pickle("econs.pkl") # preserves the user objects so they can be referenced later
econs.to_csv("econs.csv") # write to shareable format