-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathget_user_information.py
159 lines (124 loc) · 6.51 KB
/
get_user_information.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
"""
get_user_information.py
Arizona State University
J. Hunter Priniski
Hazel Kwon
A more generalized version of get_user_descriptions.py, such that it enables you to collect other data than just descriptions.
"""
from twython import Twython
import sys, string, json, codecs
class Connection:
"""A connection object has the following properties:
Attributes:
Access tokens: ak, ask, at, ats.
The set of credentials gained from apps.Twitter.com
Functionalities: allow users to collect data on up to 100 different Twitter Users. In the near future, this program will
allow users to collect Twitter data without witnessing any of the backend code.
"""
def __init__(self, ak1, ask1, at1, ats1):
self.tokens = [ak1, ask1, at1, ats1]
def connect_to_twitter(self):
self.credentials = Twython(app_key=self.tokens[0],
app_secret=self.tokens[1],
oauth_token=self.tokens[2],
oauth_token_secret=self.tokens[3]
)
print(self.credentials)
def get_users(self, directory_to_users):
self.directory_to_users = directory_to_users
with open(self.directory_to_users, 'r') as file:
self.users_raw = file.read().replace('\n', ' ')
self.users = list(self.users_raw.split())
self.users_list = [self.users[0:100], self.users[100:200], self.users[200:300]]
def search_ids(self):
self.data = self.credentials.lookup_user(user_id=self.users_list[1])
def search_screen_names(self):
self.data = []
for i in range(0, 2):
self.data[i] = self.credentials.lookup_user(screen_names=self.users_list[i])
def write_to_file(self, directory_to_output):
self.directory_to_users = directory_to_output
j = 0
while (j < len(self.data)):
self.profile = self.data[j]
self.contributors_enabled = str(self.profile['contributors_enabled'])
self.created_at = self.profile['created_at']
self.default_profile = str(self.profile['default_profile'])
self.default_profile_image = str(self.profile['default_profile_image'])
self.description = self.profile['description'].replace("\t", " ")
self.entities = str(self.profile['entities'])
self.favourites_count = str(self.profile['favourites_count'])
self.following = str(self.profile['following'])
self.followers_count = str(self.profile['followers_count'])
self.friends_count = str(self.profile['friends_count'])
self.geo_enabled = str(self.profile['geo_enabled'])
self.id_str = str(self.profile['id_str'])
self.is_translator = str(self.profile['is_translator'])
self.lang = self.profile['lang']
self.listed_count = str(self.profile['listed_count'])
self.location = str(self.profile['location'])
self.notifications = str(self.profile['notifications'])
self.profile_background_color = self.profile['profile_background_color']
self.profile_background_image_url = str(self.profile['profile_background_image_url'])
self.profile_background_image_url_https = str(self.profile['profile_background_image_url_https'])
self.profile_image_url = self.profile['profile_image_url']
self.protected = str(self.profile['protected'])
self.screen_name = self.profile['screen_name']
self.statuses_count = str(self.profile['statuses_count'])
self.time_zone = str(self.profile['time_zone'])
self.url = str(self.profile['url'])
self.utc_offset = str(self.profile['utc_offset'])
self.verified = str(self.profile['verified'])
with codecs.open(directory_to_output, 'a', encoding='utf-8') as out:
out.write(u''
+ self.id_str + '\t'
+ self.screen_name + '\t'
+ self.created_at + '\t'
+ self.description + '\t'
+ self.followers_count + '\t'
+ self.time_zone + '\t'
+ self.geo_enabled + '\t'
+ self.utc_offset + '\t'
+ self.location + '\t'
+ self.url + '\t'
+ self.statuses_count + '\t'
+ self.favourites_count + '\t'
+ self.entities + '\t'
+ self.lang + '\t'
+ self.profile_background_color + '\t'
+ self.profile_background_image_url + '\t'
+ self.profile_background_image_url_https + '\t'
+ self.profile_image_url + '\t'
+ self.protected + '\t'
+ self.verified + '\t'
+ self.contributors_enabled + '\t'
+ self.default_profile + '\t'
+ self.default_profile_image + '\t'
+ self.following + '\t'
+ self.friends_count + '\t'
+ self.notifications + '\t'
+ self.listed_count + '\t'
+ self.is_translator + '\t'
)
j = j + 1
out.close()
""" INPUT YOUR SPECIFIC INFORMATION BELOW (STEPS 1-3) """
""" 1.
Input your Keys and Access tokens respecticely:
Consumer Key (API Key), Consumer Secret (API Secret), Access Token, Access Token Secret
"""
connection_object = Connection("", "", "", "")
connection_object.connect_to_twitter()
""" 2.a
Input the complete directory to the file you wish to write the Twitter Information"""
connection_object.get_users("/Directory_to_file_containing_user_IDs_or_screen_names")
""" 2.b
If you have a list of Twitter user IDs, comment out the line connection_object.seach_screen_names(), and instead use the
line of code connection_object.search_ids(). If you have a list of Twitter Screen names in your user file, isntead comment
out the line of code connection_object.search_ids(). """
connection_object.search_ids()
connection_object.search_screen_names()
""" 3.
Input the complete directory to the file you wish to write the Twitter Information"""
connection_object.write_to_file("")