-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgetMatchIDs.py
71 lines (53 loc) · 2.15 KB
/
getMatchIDs.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
from html import getHTML
import re
print("Initialized script.")
def getMatchIDs(stop):
# Create an offset variable for lists that are paginated on HLTV
offset = 0
# Create an array of all of the Demo URLs on the page
matchIDs = findMatchIDsAtURL("https://www.hltv.org/results?offset=%s" % (offset))
# Determine if we need to paginate and create a variable to keep track of pages
morePages = endCheck(matchIDs, stop)
page = 1
print("Parsed page %s. %s IDs found so far." % (page, len(matchIDs)))
while morePages:
# Offset by 100 to get the next 100 matches
offset += 100
moreMatchIDs = findMatchIDsAtURL("https://www.hltv.org/results?offset=%s" % (offset))
# Append the new IDs to the master list
for m in moreMatchIDs:
matchIDs.append(m)
# Continue paginating and updating the user
page += 1
print("Parsed page %s. %s IDs found so far." % (page, len(matchIDs)))
morePages = endCheck(matchIDs, stop)
# Ensure that there have been no changes to the page layout
if len(matchIDs) % 100 != 0:
print("HLTV altered results page layout for offset %s" % (offset))
# Determines where to stop the array
slice = matchIDs.index(stop)
# Remove unecessary entries
matchIDs = matchIDs[:slice]
# Adds the unique match identifier as an array to each item
for i in range(0, len(matchIDs)):
string = matchIDs[i]
split = string.split("/", 1)[0:1]
split.append(string)
matchIDs[i] = split
# Reverse the array so the most recent match is last
matchIDs = matchIDs[::-1]
print("Parsed %s page(s)." % (page))
return matchIDs
def endCheck(matchIDs, stop):
if stop in matchIDs:
return False
return True
def findMatchIDsAtURL(url):
# Get the HTML using getHTML()
html = getHTML(url)
# Create an array of all of the Match URLs on the page
matchIDs = re.findall('"(.*?000"><a href="/matches/.*?)"', html)
# Loop through the messy array and removes the pesky parts
for i in range(0, len(matchIDs)):
matchIDs[i] = matchIDs[i].split('/', 2)[-1]
return matchIDs