-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhltvparser.py
79 lines (54 loc) · 3.49 KB
/
hltvparser.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
#!/usr/bin/python
import requests
from lxml import html
from hltvplayer import *
def parseHTLVhtml(r):
#Takes in a request object and returns a player object
# Check to see if got 200 response back, if not end parsing
if r.status_code != 200:
print("Unable to parse html, got status code", r.status_code)
# Continue if got 200 response
# Convert response to strcutred format, create new player object, fill statistics
tree = html.fromstring(r.content)
igname = tree.xpath('//h1[@class="summaryNickname text-ellipsis"]/text()')[0]
player = Player(igname) # Creater player object to fill in stats
# Now have player object, time to fill in all statistics
irlname = tree.xpath('//div[@class="text-ellipsis"]/text()')[0]
player.updateStat("IRL_Name", irlname)
currteam = tree.xpath('//a[@class="a-reset text-ellipsis"]/text()')[0]
player.updateStat("Current_Team", currteam)
age = tree.xpath('//div[@class="summaryPlayerAge"]/text()')[0]
age = int(age.replace(" years", ""))
player.updateStat("Age", age)
totkills = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[1]/div[1]/span[2]/text()')[0]
player.updateStat("Total_Kills", int(totkills))
hsperc = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[1]/div[2]/span[2]/text()')[0]
hsperc = round((float(hsperc.replace("%", "")) / 100.0), 4) #Round to make less ugly
player.updateStat("Headshot_Perc", hsperc)
totdeath = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[1]/div[3]/span[2]/text()')[0]
player.updateStat("Total_Deaths", int(totdeath))
kdratio = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[1]/div[4]/span[2]/text()')[0]
player.updateStat("KD_Ratio", float(kdratio))
dmgpround = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[1]/div[5]/span[2]/text()')[0]
player.updateStat("Damage_Per_Round", float(dmgpround))
nadedmgpround = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[1]/div[6]/span[2]/text()')[0]
player.updateStat("Nade_Damage_Per_Round", float(nadedmgpround))
mapsplayed = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[1]/div[7]/span[2]/text()')[0]
player.updateStat("Maps_Played", int(mapsplayed))
roundsplayed = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[2]/div[1]/span[2]/text()')[0]
player.updateStat("Rounds_Played", int(roundsplayed))
killspround = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[2]/div[2]/span[2]/text()')[0]
player.updateStat("Kills_Per_Round", float(killspround))
assistsperround = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[2]/div[3]/span[2]/text()')[0]
player.updateStat("Assists_Per_Round", float(assistsperround))
deathsperround = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[2]/div[4]/span[2]/text()')[0]
player.updateStat("Deaths_Per_Round", float(deathsperround))
savedbyteam = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[2]/div[5]/span[2]/text()')[0]
player.updateStat("Saved_By_Teammates_Per_Round", float(savedbyteam))
savedteam = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[2]/div[6]/span[2]/text()')[0]
player.updateStat("Saved_Teammates_Per_Round", float(savedteam))
rating = tree.xpath('/html/body/div[2]/div/div[2]/div[1]/div/div[8]/div/div[2]/div[7]/span[2]/text()')[0]
player.updateStat("Rating", float(rating))
#Print full player stats after filling for debugging
#print(player.Player_Statistics.items())
return player