forked from SocksPls/hltv-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
286 lines (219 loc) · 11.5 KB
/
main.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
import re
import requests
import datetime
from bs4 import BeautifulSoup
from python_utils import converters
def get_parsed_page(url):
# This fixes a blocked by cloudflare error i've encountered
headers = {
"referer": "https://www.hltv.org/stats",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
}
return BeautifulSoup(requests.get(url, headers=headers).text, "lxml")
def top5teams():
home = get_parsed_page("http://hltv.org/")
count = 0
teams = []
for team in home.find_all("div", {"class": ["col-box rank"], }):
count += 1
teamname = team.text[3:]
teams.append(teamname)
return teams
def top30teams():
page = get_parsed_page("http://www.hltv.org/ranking/teams/")
teams = page.find("div", {"class": "ranking"})
teamlist = []
for team in teams.find_all("div", {"class": "ranked-team standard-box"}):
newteam = {'name': team.find('div', {"class": "ranking-header"}).select('.name')[0].text.strip(),
'rank': converters.to_int(team.select('.position')[0].text.strip(), regexp=True),
'rank-points': converters.to_int(team.find('span', {'class': 'points'}).text, regexp=True),
'team-id': converters.to_int(team.find('a', {'class': 'details moreLink'})['href'].split('/')[-1]),
'team-players': []}
for player_div in team.find_all("td", {"class": "player-holder"}):
player = {}
player['name'] = player_div.find('img', {'class': 'playerPicture'})['title']
player['player-id'] = converters.to_int(player_div.select('.pointer')[0]['href'].split("/")[-2])
newteam['team-players'].append(player)
teamlist.append(newteam)
return teamlist
def top_players():
page = get_parsed_page("https://www.hltv.org/stats")
players = page.find_all("div", {"class": "col"})[0]
playersArray = []
for player in players.find_all("div", {"class": "top-x-box standard-box"}):
playerObj = {}
playerObj['country'] = player.find_all('img')[1]['alt'].encode('utf8')
buildName = player.find('img', {'class': 'img'})['alt'].split("'")
playerObj['name'] = buildName[0].rstrip() + buildName[2]
playerObj['nickname'] = player.find('a', {'class': 'name'}).text.encode('utf8')
playerObj['rating'] = player.find('div', {'class': 'rating'}).find('span', {'class': 'bold'}).text.encode('utf8')
playerObj['maps-played'] = player.find('div', {'class': 'average gtSmartphone-only'}).find('span', {'class': 'bold'}).text.encode('utf8')
playersArray.append(playerObj)
return playersArray
def get_players(teamid):
page = get_parsed_page("http://www.hltv.org/?pageid=362&teamid=" + str(teamid))
titlebox = page.find("div", {"class": "bodyshot-team"})
players = []
for player_link in titlebox.find_all("a"):
players.append(player_link['title'])
return players
def get_team_info(teamid):
"""
:param teamid: integer (or string consisting of integers)
:return: dictionary of team
example team id: 5378 (virtus pro)
"""
page = get_parsed_page("http://www.hltv.org/?pageid=179&teamid=" + str(teamid))
team_info = {}
team_info['team-name']=page.find("div", {"class": "context-item"}).text.encode('utf8')
current_lineup = _get_current_lineup(page.find_all("div", {"class": "col teammate"}))
team_info['current-lineup'] = current_lineup
historical_players = _get_historical_lineup(page.find_all("div", {"class": "col teammate"}))
team_info['historical-players'] = historical_players
team_stats_columns = page.find_all("div", {"class": "columns"})
team_stats = {}
for columns in team_stats_columns:
stats = columns.find_all("div", {"class": "col standard-box big-padding"})
for stat in stats:
stat_value = stat.find("div", {"class": "large-strong"}).text.encode('utf8')
stat_title = stat.find("div", {"class": "small-label-below"}).text.encode('utf8')
team_stats[stat_title] = stat_value
team_info['stats'] = team_stats
return team_info
def _get_current_lineup(player_anchors):
"""
helper function for function above
:return: list of players
"""
players = []
for player_anchor in player_anchors[0:5]:
player = {}
buildName = player_anchor.find("img", {"class": "container-width"})["alt"].split('\'')
player['country'] = player_anchor.find("div", {"class": "teammate-info standard-box"}).find("img", {"class": "flag"})["alt"]
player['name'] = buildName[0].rstrip() + buildName[2]
player['nickname'] = player_anchor.find("div", {"class": "teammate-info standard-box"}).find("div", {"class": "text-ellipsis"}).text
player['maps-played'] = int(re.search(r'\d+', player_anchor.find("div", {"class": "teammate-info standard-box"}).find("span").text).group())
players.append(player)
return players
def _get_historical_lineup(player_anchors):
"""
helper function for function above
:return: list of players
"""
players = []
for player_anchor in player_anchors[5::]:
player = {}
buildName = player_anchor.find("img", {"class": "container-width"})["alt"].split('\'')
player['country'] = player_anchor.find("div", {"class": "teammate-info standard-box"}).find("img", {"class": "flag"})["alt"].encode('utf8')
player['name'] = buildName[0].rstrip() + buildName[2]
player['nickname'] = player_anchor.find("div", {"class": "teammate-info standard-box"}).find("div", {"class": "text-ellipsis"}).text.encode('utf8')
player['maps-played'] = int(re.search(r'\d+', player_anchor.find("div", {"class": "teammate-info standard-box"}).find("span").text).group())
players.append(player)
return players
def get_matches():
matches = get_parsed_page("http://www.hltv.org/matches/")
matches_list = []
upcomingmatches = matches.find("div", {"class": "upcoming-matches"})
matchdays = upcomingmatches.find_all("div", {"class": "match-day"})
for match in matchdays:
matchDetails = match.find_all("table", {"class": "table"})
for getMatch in matchDetails:
matchObj = {}
matchObj['date'] = match.find("span", {"class": "standard-headline"}).text.encode('utf8')
matchObj['time'] = getMatch.find("td", {"class": "time"}).text.encode('utf8').lstrip().rstrip()
if (getMatch.find("td", {"class": "placeholder-text-cell"})):
matchObj['event'] = getMatch.find("td", {"class": "placeholder-text-cell"}).text.encode('utf8')
elif (getMatch.find("td", {"class": "event"})):
matchObj['event'] = getMatch.find("td", {"class": "event"}).text.encode('utf8')
else:
matchObj['event'] = None
if (getMatch.find_all("td", {"class": "team-cell"})):
matchObj['team1'] = getMatch.find_all("td", {"class": "team-cell"})[0].text.encode('utf8').lstrip().rstrip()
matchObj['team2'] = getMatch.find_all("td", {"class": "team-cell"})[1].text.encode('utf8').lstrip().rstrip()
else:
matchObj['team1'] = None
matchObj['team2'] = None
matches_list.append(matchObj)
return matches_list
def get_results():
results = get_parsed_page("http://www.hltv.org/results/")
results_list = []
pastresults = results.find_all("div", {"class": "results-holder"})
for result in pastresults:
resultDiv = result.find_all("div", {"class": "result-con"})
for res in resultDiv:
getRes = res.find("div", {"class": "result"}).find("table")
resultObj = {}
if (res.parent.find("span", {"class": "standard-headline"})):
resultObj['date'] = res.parent.find("span", {"class": "standard-headline"}).text.encode('utf8')
else:
dt = datetime.date.today()
resultObj['date'] = str(dt.day) + '/' + str(dt.month) + '/' + str(dt.year)
if (res.find("td", {"class": "placeholder-text-cell"})):
resultObj['event'] = res.find("td", {"class": "placeholder-text-cell"}).text.encode('utf8')
elif (res.find("td", {"class": "event"})):
resultObj['event'] = res.find("td", {"class": "event"}).text.encode('utf8')
else:
resultObj['event'] = None
if (res.find_all("td", {"class": "team-cell"})):
resultObj['team1'] = res.find_all("td", {"class": "team-cell"})[0].text.encode('utf8').lstrip().rstrip()
resultObj['team1score'] = converters.to_int(res.find("td", {"class": "result-score"}).find_all("span")[0].text.encode('utf8').lstrip().rstrip())
resultObj['team2'] = res.find_all("td", {"class": "team-cell"})[1].text.encode('utf8').lstrip().rstrip()
resultObj['team2score'] = converters.to_int(res.find("td", {"class": "result-score"}).find_all("span")[1].text.encode('utf8').lstrip().rstrip())
else:
resultObj['team1'] = None
resultObj['team2'] = None
results_list.append(resultObj)
return results_list
def get_results_by_date(start_date, end_date):
# Dates like yyyy-mm-dd (iso)
results_list = []
offset = 0
# Loop through all stats pages
while True:
url = "https://www.hltv.org/stats/matches?startDate="+start_date+"&endDate="+end_date+"&offset="+str(offset)
results = get_parsed_page(url)
# Total amount of results of the query
amount = int(results.find("span", attrs={"class": "pagination-data"}).text.split("of")[1].strip())
# All rows (<tr>s) of the match table
pastresults = results.find("tbody").find_all("tr")
# Parse each <tr> element to a result dictionary
for result in pastresults:
team_cols = result.find_all("td", {"class": "team-col"})
t1 = team_cols[0].find("a").text
t2 = team_cols[1].find("a").text
t1_score = int(team_cols[0].find_all(attrs={"class": "score"})[0].text.strip()[1:-1])
t2_score = int(team_cols[1].find_all(attrs={"class": "score"})[0].text.strip()[1:-1])
map = result.find(attrs={"class": "statsDetail"}).find(attrs={"class": "dynamic-map-name-full"}).text
event = result.find(attrs={"class": "event-col"}).text
date = result.find(attrs={"class": "date-col"}).find("a").find("div").text
result_dict = {"team1": t1, "team2": t2, "team1score": t1_score,
"team2score": t2_score, "date": date, "map": map, "event": event}
# Add this pages results to the result list
results_list.append(result_dict)
# Get the next 50 results (next page) or break
if offset < amount:
offset += 50
else:
break
return results_list
if __name__ == "__main__":
import pprint
pp = pprint.PrettyPrinter()
pp.pprint('top5')
pp.pprint(top5teams())
pp.pprint('top30')
pp.pprint(top30teams())
pp.pprint('top_players')
pp.pprint(top_players())
pp.pprint('get_players')
pp.pprint(get_players('6665'))
pp.pprint('get_team_info')
pp.pprint(get_team_info('6665'))
pp.pprint('get_matches')
pp.pprint(get_matches())
pp.pprint('get_results')
pp.pprint(get_results())
pp.pprint('get_results_by_date')
today_iso = datetime.datetime.today().isoformat().split('T')[0]
pp.pprint(get_results_by_date(today_iso, today_iso))