-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsloppyseconds.py
72 lines (53 loc) · 1.77 KB
/
sloppyseconds.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
import requests
from bs4 import BeautifulSoup
from termcolor import colored, cprint
def find_href(td_elems):
cprint("Looking for Game manual button.. ", end='', color='yellow')
for elem in td_elems:
if 'Game Manual' in elem.text:
cprint("Got it!", end='', color='yellow')
link = elem.nextSibling.contents[0].attrs['href']
return link
def href_create_game_obj(href, url):
if href:
name = href.split('/')[-1]
else:
name = href
return {
'name': name,
'url': url
}
def sanitize_to_find_href(name):
name = name.replace('&', 'and')
for c in r"'+\\/:":
name = name.replace(c, '')
name = name.replace(' ', ' ')
# Remove leading and trailing whitespaces
name = name.strip()
for c in r" ":
name = name.replace(c, '-')
return name
def href_find_link_by_name(system, game):
BASE = 'http://www.gamesdatabase.org/game/{}/{}'
MANUAL_BASE = 'http://www.gamesdatabase.org{}'
system = sanitize_to_find_href(system)
game = sanitize_to_find_href(game)
cprint("Game: {} ".format(game), end='', color='yellow')
url = BASE.format(system, game)
resp = requests.get(url)
soup = BeautifulSoup(resp.text, 'html.parser')
try:
table_elems = soup.find_all('table')[5]
except IndexError as i:
return False
td_elems = table_elems.find_all('td')
href = find_href(td_elems)
if href:
proper_url = MANUAL_BASE.format(href)
cprint(proper_url, 'yellow')
game_obj = href_create_game_obj(href, proper_url)
return game_obj
else:
notfound_obj = href_create_game_obj(False, 'Not found')
cprint('Button not found', 'yellow')
return notfound_obj