-
Notifications
You must be signed in to change notification settings - Fork 7
/
twitter_friends.py
executable file
·277 lines (243 loc) · 7.02 KB
/
twitter_friends.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#! /usr/bin/python -u
# -*- coding: utf-8 -*-
#
import twitter
from time import *
from sys import exit, argv
from random import randrange, random, SystemRandom
import re
import os
import ConfigParser
from py_compile import compile
import httplib
"""
Add it into your crontab
30 9 * * 5 python /path/to/twitter_friends.py
"""
configuration = ".twitterc"
configuration = "%s/%s" % (os.environ.get("HOME"), configuration)
user = 'helioloureiro'
dryrun = False
CONTROLE = {} # control for the ones already done or not so lucky
FFS = {} # all followers
SIZE = 140
MSG = ""
PCTG = 10 # % of chance to post. Lower means less posts.
counter = 1 # users count
threshold = 60 # number of posts
TESTE = False
if twitter.__version__ != '3.2':
print "Only python-twitter 3.2 is supported"
exit(1)
def isLucky():
#result = int(random() * 100)
result = int(SystemRandom().random() * 100)
if (result <= PCTG):
return True
else:
return False
def SendingList(api, tag, list_name):
"""
Sending message
"""
global FFS, CONTROLE, counter, dryrun
print "Running for %s: %d" % (tag, len( FFS[list_name] ))
MSG = tag + " "
for name in FFS[list_name]:
if CONTROLE.has_key(name):
continue
else:
CONTROLE[name] = 1
print counter, name
name = '@' + name
if ( len(MSG + " " + name) > SIZE):
print MSG
if not dryrun:
api.PostUpdate(MSG)
sleep(randrange(1,30) * 60)
MSG = tag + " " + name
else:
MSG += " " + name
counter += 1
print MSG
if not dryrun:
try:
if not re.search("@", MSG):
# empty
return
api.PostUpdate(MSG)
except twitter.TwitterError as e:
if ( re.search('Status is a duplicate.', e.message) ):
pass
sleep(randrange(1,30) * 60)
def ReadConfig():
"""
Configuration from file ~/.twitterc
"""
global cons_key, cons_sec, acc_key, acc_sec
cfg = ConfigParser.ConfigParser()
print "Reading configuration: %s" % configuration
if not os.path.exists(configuration):
print "Failed to find configuration file %s" % configuration
exit(1)
cfg.read(configuration)
cons_key = cfg.get("TWITTER", "CONS_KEY")
cons_sec = cfg.get("TWITTER", "CONS_SEC")
acc_key = cfg.get("TWITTER", "ACC_KEY")
acc_sec = cfg.get("TWITTER", "ACC_SEC")
def GetDateTag():
"""
Check comemorative days
"""
# Find great tags:
month = strftime("%B",localtime())
day = strftime("%d",localtime())
tag = "#FF" #default
if (day, month) == ('02', 'November'):
tag = "#FollowFinados"
elif (day, month) == ('25', 'December'):
tag = "#FollowXmas"
elif (day, month) == ('01', 'January'):
tag = "#FollowHappyNewYear"
elif (day, month) == ('25', 'January'):
tag = "#FollowSPb-day"
elif (day, month) == ('31', 'October'):
tag = "#FollowHalloween"
elif (day, month) == ('15', 'November'):
tag = "#FollowRepublic"
elif (day, month) == ('08', 'March'):
tag = "#FollowWomenDay"
elif (day, month) == ('14', 'February'):
tag = "#FollowStValentine"
elif (day, month) == ('17', 'February'):
tag = "#FollowStPatrick"
elif (day, month) == ('01', 'May'):
tag = "#FollowLaborDay"
elif (day, month) == ('19', 'June'):
tag = "#FollowCorpusChristi"
elif (day, month) == ('20', 'June'):
tag = "#FollowCorpusChristi"
elif (day, month) == ('28', 'November'):
tag = "#FollowBlackFriday"
elif (day, month) == ('29', 'November'):
tag = "#FollowBlackFriday"
else:
"""
Only days to check
"""
if (day == '13'):
tag = "#FollowFriday13th"
return tag
### MAIN ###
def main():
global FFS, CONTROLE, dryrun
if (argv[-1] == "-c"):
compile(argv[0])
exit(1)
if argv[-1] in [ "-t", "-d" ]:
dryrun = True
print "Activated dryrun mode"
time_i = time()
print "[" + ctime() + "] starting your twitter_friends..."
dow = strftime("%A", localtime())
if (dow != 'Friday'):
print "Today isn't #FF day, because today isn't Friday, doh!", str(dow)
if not (argv[-1] == "--force"):
print "Aborting..."
exit(1)
else:
print "Called \"force\". Enforcing."
tag = GetDateTag()
print "Running on tag:", tag
ReadConfig()
print "Autenticating in Twitter"
# App python-tweeter
# https://dev.twitter.com/apps/815176
try:
api = twitter.Api(
consumer_key = cons_key,
consumer_secret = cons_sec,
access_token_key = acc_key,
access_token_secret = acc_sec
)
except:
print "Failed to authenticate on Twitter."
exit(1)
print "Gathering lists"
all_lists = api.GetLists(screen_name = user)
#print all_lists
"""
>>> for l in mylists:
... print l.slug, l.name, l.id
...
python Python 99979587
ff FF 71764323
freebsd FreeBSD 61981324
linux Linux 53798850
amigos Amigos 40323100
misc Misc 40166658
futebol Futebol 32886245
articulistas Articulistas 21454410
piadistas-infames piadistas_infames 13674852
"""
for l in all_lists:
#print "Checking list %s" % l.slug
# lists that I want to promote
if not l.slug.lower() in [
'linux',
'ff',
'python',
'ultrafofos'
]:
continue
print "\n ###### Parsing list: %s ###### " % l.slug
FFS[l.slug.lower()] = []
members = api.GetListMembers(list_id=l.id)
#print members
limit_counter = 0
for m in members:
if isLucky() and limit_counter < threshold:
FFS[l.slug].append(m.screen_name)
limit_counter += 1
else:
CONTROLE[m.screen_name] = 1
print "Total: %d" % len(FFS[l.slug])
try:
msg = "Automated python-twitter-" + tag + " mode=on"
if not dryrun:
api.PostUpdates(msg)
else:
print msg
except twitter.TwitterError as e:
if ( re.search('Status is a duplicate.', str(e.message)) ):
pass
"""
Processing the main one, ff list
"""
SendingList(api, tag, "ff")
"""
Python list
"""
SendingList(api, "#FFPython", "python")
"""
Linux list
"""
SendingList(api, "#FFLinux", "linux")
"""
Ultrafofos list
"""
SendingList(api, "#UltraFoFo", "ultrafofos")
"""
Happy ending...
"""
try:
if not dryrun:
api.PostUpdates("Python Twitter #rockz!!!")
api.PostUpdates("Automated python-twitter-" + tag + " mode=off")
except twitter.TwitterError as e:
if ( re.search('Status is a duplicate.', e.message) ):
pass
time_e = time()
print "[%s] finished twitter_friends... (%0.2f s)" % (ctime(), time_e - time_i)
if __name__ == '__main__':
main()