-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
317 lines (315 loc) Β· 12.5 KB
/
bot.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import tweepy
from twitter_api import *
from bs4 import BeautifulSoup
from bs4.dammit import EncodingDetector
import requests
from random import choices,sample,randint
from time import sleep
from league_list import *
from
header ={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
def top4leagues(leagueList,index):
rangeOfWork = team_qulfied[index]
defaultLst = []
temIndex = sample(range(12),rangeOfWork)
for i in range(rangeOfWork):
clubsIndex = randint(0,1)
try:
tempClubsLst = []
url = 'https://www.worldfootball.net'+leagueList[clubsIndex]
source = requests.get(url, headers=header)
http_encoding = source.encoding if 'charset' in source.headers.get('content-type', '').lower() else None
html_encoding = EncodingDetector.find_declared_encoding(source.content, is_html=True)
encoding = html_encoding or http_encoding
soup = BeautifulSoup(source.content, 'lxml', from_encoding=encoding)
find_boxS= soup.find('div',class_="scrollable_tables")
the_team_table = find_boxS.find('table', {'class':'standard_tabelle'})
for theTeamAtag in the_team_table.find_all('a',href=True):
if theTeamAtag.text:
tempClubsLst.append(theTeamAtag.text)
y = temIndex[i]
teamNames = tempClubsLst[y]
defaultLst.append(teamNames)
tempClubsLst.pop()
except Exception as e:
print(e)
return defaultLst
def otherTeams(sumOfQ):
remainigTeams = 16- sumOfQ
finalTeams = []
if remainigTeams <=0:
return []
else:
randomTeams = sample(range(10),remainigTeams)
randomTeamsIndex = sample(range(remainigTeams+2),remainigTeams)
randomTeamSelection = choices(range(len(league_qlf_list)),k=remainigTeams)
for i in range(0,remainigTeams):
temp_x = randomTeamSelection[i]
try:
tempClubsLst = []
url = 'https://www.worldfootball.net'+league_qlf_list[temp_x]
source = requests.get(url, headers=header)
http_encoding = source.encoding if 'charset' in source.headers.get('content-type', '').lower() else None
html_encoding = EncodingDetector.find_declared_encoding(source.content, is_html=True)
encoding = html_encoding or http_encoding
soup = BeautifulSoup(source.content, 'lxml', from_encoding=encoding)
find_boxS= soup.find('div',class_="scrollable_tables")
the_team_table = find_boxS.find('table', {'class':'standard_tabelle'})
for theTeamAtag in the_team_table.find_all('a',href=True):
if theTeamAtag.text:
tempClubsLst.append(theTeamAtag.text)
y = randomTeamsIndex[i]
teamNames = tempClubsLst[y]
finalTeams.append(teamNames)
except Exception as e:
print(e)
return finalTeams
def the16(epl,laliga,seriea,bundesliga,other):
tempLst = []
theLstOfLeagues = [epl,laliga,seriea,bundesliga,other]
for i in range(len(theLstOfLeagues)):
for j in range(len(theLstOfLeagues[i])):
tempLst.append(theLstOfLeagues[i][j])
return tempLst
def vsPrint(year,home,away,n):
apndLst = []
for i in range(n):
tag = '#UCL'
results = home[i]+' VS '+away[i]+'\n'
apndLst.append(results)
if len(apndLst) == 8:
mes = str(year)+' UCL - Round of 16\n\n'
f1 = '[1/2] '+tag
c = ''.join(map(str, apndLst[0:4]))
print(mes+c+f1)
api.update_status(status=mes+c+f1)
sleep(100) # change that to 30 sec afther finishing
f2 ='[2/2] '+tag
p =''.join(map(str, apndLst[4:8]))
print(mes+p+f2)
api.update_status(status=mes+p+f2)
sleep(100)
elif len(apndLst) == 4:
mes = str(year)+' UCL - Quarter finals \n\n'
c = ''.join(map(str, apndLst))
print(mes+c+tag)
api.update_status(status=mes+c+tag)
sleep(300)
else:
mes = str(year)+' UCL - Semi finals \n\n'
c = ''.join(map(str, apndLst))
print(mes+c+tag)
api.update_status(status=mes+c+tag)
sleep(300)
def legScores(n):
homeTeam = []
if n == 1:
for i in range(n):
scoreHome = randint(0,4)
homeTeam.append(scoreHome)
else:
for i in range(n):
scoreHome = randint(0,6)
homeTeam.append(scoreHome)
return homeTeam
def resultsTweet(home,away,s1,s2,n):
tag = '#UCL'
apndLst=[]
for i in range(n):
results = home[i]+' '+str(s1[i])+'-'+str(s2[i])+' '+away[i]+'\n'
apndLst.append(results)
if len(apndLst) == 8:
mes = str(year)+' UCL Results - Round of 16\n\n'
f1 = '[1/2] '
c = ''.join(map(str, apndLst[0:4]))
print(mes+c+f1+tag) # Change that to tweepy status update
api.update_status(status=mes+c+f1+tag)
sleep(150) # change that to 30 sec afther finishing
f2 ='[2/2] '
p =''.join(map(str, apndLst[4:8]))
print(mes+p+f2+tag)
api.update_status(status=mes+p+f2+tag)
sleep(150)
elif len(apndLst) == 4:
mes = str(year)+' UCL Results - Quarter finals \n\n'
c = ''.join(map(str, apndLst))
print(mes+c+tag)
api.update_status(status=mes+c+tag)
sleep(600)
else:
mes = str(year)+' UCL Results - Semi finals \n\n'
c = ''.join(map(str, apndLst))
print(mes+c+tag)
api.update_status(status=mes+c+tag)
sleep(600)
def koTeams(h,a,s1,s2,n, year):
tempTeams = []
if n == 2:
for i in range(n):
if s1[i] > s2[i]:
s = str(s1[i])+'-'+str(s2[i])
mes = 'π '+str(year)+', '+h[i]+' beat '+a[i]+' ('+s+') to qualify for the UCL final. #UCLFinal'
print(mes)
tempTeams.append(h[i])
api.update_status(status=mes)
sleep(800)
elif s1[i] < s2[i]:
s= str(s2[i])+'-'+str(s1[i])
mes = 'π '+str(year)+', '+a[i]+' beat '+h[i]+' ('+s+') to qualify for the UCL final. #UCLFinal'
print(mes)
tempTeams.append(a[i])
api.update_status(status=mes)
sleep(800)
else:
t = randint(0,1)
if t == 0:
scr = ['5-3','4-2','5-4','3-2','6-5','4-3']
ran = randint(0,5)
mes='π€ͺπ '+str(year)+', '+h[i]+' defeats '+a[i]+' in shootout ('+scr[ran]+'), advancing to UCL final #UCLFinal '
print(mes)
api.update_status(status=mes)
tempTeams.append(h[i])
sleep(800)
else:
scr = ['5-3','4-2','5-4','3-2','6-5','4-3']
ran = randint(0,5)
s =str(s2[i])+'-'+str(s1[i])
mes='π€ͺπ '+str(year)+', '+a[i]+' defeats '+h[i]+' in shootout ('+scr[ran]+'), advancing to UCL final #UCLFinal'
print(mes)
api.update_status(status=mes)
tempTeams.append(a[i])
sleep(800)
else:
for i in range(n):
if s1[i] > s2[i]:
s = str(s1[i])+'-'+str(s2[i])
mes = 'πΌ '+str(year)+', '+h[i]+' beat '+a[i]+' ('+s+') and qualified to the next UCL round. #UCL'
print(mes)
api.update_status(status=mes)
tempTeams.append(h[i])
sleep(800)
elif s1[i] < s2[i]:
s= str(s2[i])+'-'+str(s1[i])
mes = 'πΌ '+str(year)+', '+a[i]+' beat '+h[i]+' ('+s+') and qualified to the next UCL round. #UCL'
print(mes)
api.update_status(status=mes)
tempTeams.append(a[i])
sleep(800)
else:
t = randint(0,1)
if t == 0:
s = str(s1[i])+'-'+str(s2[i])
mes='π± '+str(year)+', '+h[i]+' triumphed via a penalty shootout, after the match agains '+a[i]+' ended '+ s+' in normal time #UCL'
print(mes)
api.update_status(status=mes)
tempTeams.append(h[i])
sleep(800)
else:
s =str(s2[i])+'-'+str(s1[i])
mes='π± '+str(year)+', '+a[i]+' triumphed via a penalty shootout, after the match agains '+h[i]+' ended '+ s+' in normal time #UCL'
print(mes)
api.update_status(status=mes)
tempTeams.append(a[i])
sleep(800)
return tempTeams
def finalCongrats(h,a,s1,s2,year):
#year = 2045
for i in range(1):
who = str(year)+' π UCL Final'+h[i]+' vs '+a[i]+ ' #UCLFinal'
api.update_status(status=who)
sleep(1800)
if s1[i]>s2[i]:
s = str(s1[i])+'-'+str(s2[i])
mes = str(year)+','+h[i]+' beat '+a[i]+' ('+s+') to win Champions League trophy π \n#UCLFinal'
print(mes)
api.update_status(status=mes)
sleep(150)
s = str(s1[i])+'-'+str(s2[i])
ucl = 'π '+str(year)+' UCL FINAL RESULTπ \n'+h[i]+'βοΈ '+s+' '+a[i]+'\n#UCLFinal'
print(ucl)
api.update_status(status=ucl)
sleep(150)
cong = 'ππ Congratulations to '+ h[i]+' for winning the '+str(year)+' Champions League title \n#UCLFinal'
print(cong)
api.update_status(status=mes)
sleep(150)
elif s1[i]<s2[i]:
s = str(s2[i])+'-'+str(s1[i])
mes = str(year)+','+a[i]+' beat '+h[i]+' ('+s+') to win Champions League trophy π \n#UCLFinal'
api.update_status(status=mes)
sleep(150)
s = str(s1[i])+'-'+str(s2[i])
ucl = 'π '+str(year)+' UCL FINAL RESULTπ \n'+h[i]+' '+s+' βοΈ'+a[i]+'\n#UCLFinal'
print(ucl)
api.update_status(status=ucl)
sleep(150)
cong = 'ππ Congratulations to '+ a[i]+' for winning the '+str(year)+' Champions League title \n#UCLFinal'
api.update_status(status=cong)
print(cong)
sleep(150)
else:
t = randint(0,1)
if t == 0:
scr = ['5-3','4-2','5-4','3-2','6-5','4-3']
ran = randint(0,5)
mes = h[i]+' Beat '+a[i]+' '+scr[ran]+' on penalties in the '+str(year)+' Champions League final π \n#UCLFinal'
api.update_status(status=mes)
sleep(150)
cong ='ππ Congratulations to '+ h[i]+' for winning the '+str(year)+' Champions League title \n#UCLFinal'
api.update_status(status=cong)
sleep(150)
else:
scr = ['5-3','4-2','5-4','3-2','6-5','4-3']
ran = randint(0,5)
mes = a[i]+' Beat '+h[i]+' '+scr[ran]+' on penalties in the '+str(year)+' Champions League final π \n#UCLFinal'
api.update_status(status=mes)
sleep(150)
cong = 'ππ Congratulations to '+ a[i]+' for winning the '+str(year)+' Champions League title \n#UCLFinal'
api.update_status(status=cong)
sleep(150)
print("BOT Started")
# BOT will scrap teams names from worldfootball.net
# using data list will choice sum of list of numbers between 4 to 16
# bot will tweet full time scores R16,Q,SF and Final
for year in range(2054,8000):
try:
data = [1,2,3,4]
team_qulfied = choices(data,k=4)
epl_lst=top4leagues(english_leagues,0)
bundesliga_lst =top4leagues(german_leagues,1)
laliga_lst=top4leagues(spainsh_league,2)
serieA_lst = top4leagues(italian_leagues,3)
sumOfQ = sum(team_qulfied)
otherTeams_lst =otherTeams(sumOfQ)
the16teams = the16(epl_lst,bundesliga_lst,laliga_lst,serieA_lst,otherTeams_lst)
teamA = the16teams[0:8]
teamB = the16teams[8:16]
scores1 = legScores(8)
scores2 = legScores(8)
vsPrint(year,teamA,teamB,8)
t = koTeams(teamA,teamB,scores1,scores2,8,year)
resultsTweet(teamA,teamB,scores1,scores2,8)
scores1 = legScores(4)
scores2 = legScores(4)
teamA = t[0:4]
teamB = t[4:8]
vsPrint(year,teamA,teamB,4)
t = koTeams(teamA,teamB,scores1,scores2,4,year)
resultsTweet(teamA,teamB,scores1,scores2,4)
scores1 = legScores(2)
scores2 = legScores(2)
teamA = t[0:2]
teamB = t[2:4]
vsPrint(year,teamA,teamB,2)
t = koTeams(teamA,teamB,scores1,scores2,2,year)
resultsTweet(teamA,teamB,scores1,scores2,2)
teamA = t[0:1]
teamB = t[1:2]
scores1 = legScores(1)
scores2 = legScores(1)
finalCongrats(teamA,teamB,scores1,scores2,year)
except Exception as e:
print(e)