-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser_script.py
73 lines (58 loc) · 2.01 KB
/
user_script.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
# Import the necessary Libraries
import pandas as pd
import tweepy
import sys
from login_function import login
# Set up the twitter API
api = login(consumer_key, consumer_secret, access_token, access_secret)
# import the file which contains the tweetData
INPUT_DIR = str(sys.argv[1])
# File where the user data is to be dumped
OUTPUT_DIR = str(sys.argv[2])
# defining the dictionary for the user data
user_dict = {
"user_name":[],
"user_id":[],
"screen_name":[],
"location":[],
"verified":[],
"status_count":[],
"num_followers":[],
"list_followers_user_id":[],
"list_followers_user_name":[],
"num_friends":[],
"list_friends_user_id":[],
"list_friends_user_name":[]
}
# getting the data
df = pd.read_json(INPUT_DIR)
user_ids = df['user_id']
# getting the user information
for i in user_ids:
u = api.get_user(id=i)
user_dict["user_name"].append(u.name)
user_dict["user_id"].append(u.id)
user_dict["screen_name"].append(u.screen_name)
user_dict["location"].append(u.location)
user_dict["verified"].append(u.verified)
user_dict["status_count"].append(u.statuses_count)
user_dict["num_followers"].append(u.followeres_count)
followers_id = " "
followers_name = " "
for user in tweepy.Cursor(api.followers, user_id=i).items():
followers_id = followers_id + user.id + " ," #getting the user_id of all the followers
followers_name = followers_name + user.name + " ,"
user_dict["list_followers_user_id"].append(followers_id)
user_dict["list_followers_user_name"].append(followers_name)
user_dict['num_friends'].append(u.friends_count)
friends_id = " "
friends_name = " "
for user in tweepy.Cursor(api.friends, user_id=i).items():
friends_id = friends_id + user.id + " ,"
friends_name = friends_name + user.name + " ,"
user_dict["list_friends_user_id"].append(friends_id)
user_dict["list_friends_user_name"].append(friends_name)
# Converting the dictionary to dataframe
user_data = pd.DataFrame(user_dict)
# Storing the dataframe in a json file
user_data.to_json(path_or_buf=OUTPUT_DIR, orient='table')