-
Notifications
You must be signed in to change notification settings - Fork 1
/
howlongtobeat.py
93 lines (69 loc) · 2.31 KB
/
howlongtobeat.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
import requests
from lxml import etree
from util import hook
def how_long(inp):
"""
returns a breakdown of the top result from a howlongtobeat.com search
"""
session = requests.Session()
base_url = "http://howlongtobeat.com/"
gamelist = "gamelist_main.php"
data = {
"queryString": inp
}
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
gamelist = etree.HTML(
session.post(
base_url + gamelist,
data=data,
headers=headers
).text
)
try:
gamebreakdown = gamelist.find(
".//a[@title='Full Game Page']").get("href")
except:
return "No results."
html = etree.HTML(
session.get(
base_url + gamebreakdown,
headers=headers
).text
)
title = html.find(".//div[@class='headermain']").text
params = {"url": base_url + gamebreakdown}
link = session.get(
"http://tinyurl.com/api-create.php?", params=params).text
table = html.find(".//table")
main_time = ""
extra_time = ""
complete_time = ""
overall = ""
for tbody in table.findall(".//tbody"):
if tbody.find(".//td").text == "Main Story (Required) Completion":
main_time = "Main time: %s" % tbody.findall(".//td")[3].text.strip()
if tbody.find(".//td").text == "Main + Extras (Quests/Medals/Unlockables)":
extra_time = "Main + Extras: %s" % tbody.findall(".//td")[3].text.strip()
if tbody.find(".//td").text == "Completionists":
complete_time = "100%%: %s" % tbody.findall(".//td")[3].text.strip()
if tbody.find(".//a") is not None:
overall = "Overall: %s" % tbody.findall(".//td")[3].text.strip()
output = "How long to beat %s (%s) (Averages) -" % (title, link)
if not main_time == "":
output += " %s," % main_time
if not extra_time == "":
output += " %s," % extra_time
if not complete_time == "":
output += " %s," % complete_time
output += " %s" % overall
return output
@hook.command('hltb')
@hook.command('howlong')
@hook.command
def howlongtobeat(inp):
".howlongtobeat <game> -- gets the top result from howlongtobeat.com"
return how_long(inp)
if __name__ == "__main__":
print how_long("Bastion", "user")