-
Notifications
You must be signed in to change notification settings - Fork 4
/
tweetlib.py
254 lines (182 loc) · 6.71 KB
/
tweetlib.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import twython as t
import operator
import os
import random
def get_passwords(filename):
lines = open(filename,'r').readlines();
passwords = {};
for n,i in enumerate(lines):
if i[0] == '#':
passwords[i[1:].strip()] = lines[n+1].strip();
return passwords;
def get_all_tweets(user,api=None):
"""Returns as much tweets as possible for this twitter user""";
if api==None:
passwords = get_passwords('passwords.txt');
api = t.Twython(passwords['app_key'], passwords['app_secret'],
passwords['oauth_token'], passwords['oauth_token_secret']);
no_tweets = False;
tweets_total = [];
c = 1;
while not no_tweets:
tweets = api.get_user_timeline(screen_name=user,count=200,page=c);
try:
tweets = api.get_user_timeline(screen_name=user,count=200,page=c);
except t.TwythonError:
tweets = [];
print('Twython is sad :(');
break;
if len(tweets) < 1:
no_tweets = True;
else:
for tweet in tweets:
tweets_total.append((str(tweet['id']),str(tweet['created_at']),clean_tweet(tweet['text'].encode('utf8'),0)));
c+= 1;
return tweets_total;
def get_recent_tweets(user,number,api=None):
"""Returns number recent tweets for this twitter user""";
if api==None:
api = t.Twython();
tweets_total = [];
try:
tweets = api.getUserTimeline(screen_name=user,count=number);
except t.TwythonError:
tweets = [];
print('Twython is sad :(');
for tweet in tweets:
tweets_total.append((str(tweet['id']),str(tweet['created_at']),
clean_tweet(tweet['text'].encode('utf8'),0)));
return tweets_total;
def clean_tweet(tweet,severity=2):
"""Removes links, hastags, smilies, addressees""";
result = tweet.replace('\n','');
if severity > 1:
triggers = ['@','#','http',':',';'];
words = result.split();
clean_words = [];
for i in words:
found_trigger = False;
for j in triggers:
if j in i:
found_trigger = True;
break;
if found_trigger:
continue;
clean_words.append(i);
result = ' '.join(clean_words)
return result;
def get_addressees(tweet):
"""Returns all people this tweet was addressed to""";
words = tweet.split(' ');
addressees = [];
for i in words:
if len(i) > 0 and i[0] == '@':
addressees.append(i.replace('@','').replace(':','').replace(',','').replace('.',''));
return addressees;
def is_private(tweeter,api=None):
"""Returns whether this user's profile is private""";
if api==None:
api = t.Twython();
return api.lookupUser(screen_name = tweeter)[0][u'protected'];
def is_dutch(tweet,word_list = None):
"""Returns whether a tweet is Dutch or not""";
words = clean_tweet(tweet).split();
#Half of the words should be in the list
amount_dutch_threshold = round(len(words) / 2);
#Import the list
if word_list == None:
word_list = get_dutch_wordlist();
#See how much words are in the list
found_in_list = 0;
for i in word_list:
if i in words:
found_in_list += 1;
#Enough words Dutch? Return true
if found_in_list >= amount_dutch_threshold:
return True;
break;
return False;
def get_dutch_wordlist():
"""Returns a list of the most frequent Dutch words""";
#TEMP solution
try:
lines = open('/scratch/wstoop/soothsayer/nl.txt','r').readlines();
except:
lines = open('C:\\Users\\Stoop\\Desktop\\Scriptie\\nl.txt','r').readlines();
words = [];
for i in lines:
word,frequency = i.split(' ');
word = word.strip();
frequency = int(frequency);
if frequency > 200:
words.append(word);
return(words);
def get_new_tweeters(feeddir,tweeters):
"""Returns the addressee most mentioned and the addressee most people tweet to""";
#Get addressees from 35 random tweeters
total_addressees = [];
files = os.listdir(feeddir);
random.shuffle(files);
for f in files[:35]:
print('Getting addressees from',f);
total_addressees.append(get_all_addressees_from(feeddir+f));
#Figure out which are most mentioned
most_mentioned_addressees = most_frequent_first(sum_dicts(total_addressees));
addressees_most_people_refer_to = most_frequent_first(most_used_keys(total_addressees));
#Get the two most frequent
most_mentioned_addressee = '';
addressee_most_people_refer_to = '';
print(most_mentioned_addressees[:20]);
for i in most_mentioned_addressees:
possible_tweeter = i[0].lower().strip();
if possible_tweeter not in tweeters:
most_mentioned_addressee = possible_tweeter;
break;
print(addressees_most_people_refer_to[:20]);
for i in addressees_most_people_refer_to:
possible_tweeter = i[0].lower().strip();
if possible_tweeter not in tweeters and possible_tweeter != most_mentioned_addressee:
addressee_most_people_refer_to = possible_tweeter;
break;
return most_mentioned_addressee,addressee_most_people_refer_to;
def get_all_addressees_from(fileloc):
"""Return all addressees mentioned in this file as a dictionary with frequencies""";
wordlist = get_dutch_wordlist();
addressees_total = {};
f = open(fileloc,'r');
for line in f:
tweet = line.split('||')[2];
addressees = get_addressees(tweet);
if len(addressees) > 0 and is_dutch(tweet,wordlist):
for addressee in addressees:
try:
addressees_total[addressee] += 1;
except KeyError:
addressees_total[addressee] = 1;
return addressees_total;
def sum_dicts(dicts):
"""Ads up similar elements in multiple dicts""";
dict1 = {};
for d in dicts:
dict2 = d;
for k,v in dict2.items():
try:
dict1[k] += v;
except KeyError:
dict1[k] = v;
return dict1;
def most_used_keys(dicts):
"""Returns the keys most used in a series of keys""";
score = {};
for d in dicts:
for k in d.keys():
try:
score[k] += 1;
except KeyError:
score[k] = 1;
return score;
def most_frequent_first(d):
"""Changes a dict into a sorted list of key-value tuples, highest value first""";
d = sorted(d.iteritems(), key=operator.itemgetter(1));
d.reverse();
return d;