-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
62 lines (52 loc) · 1.52 KB
/
Game.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
from lxml import html
import requests
import re
class Game:
def __init__(self, gameid='5577'):
self.gameid = gameid
self.game_url = 'http://www.j-archive.com/showgame.php?game_id=' + self.gameid
self.page = requests.get(self.game_url)
# Generate the html page tree
self.tree = html.fromstring(self.page.content)
self.setCategories()
self.setClues()
self.setAnswers()
def setCategories(self):
path = '//*[@class="category_name"]/text()'
self.categories = self.tree.xpath(path)[0:6]
def getCategories(self):
return self.categories
def setClues(self):
self.clues = []
for category in range(0,6):
rows = []
for row in range(0,5):
# Clues
path = '//*[@id="clue_J_' + str(category+1) + '_' + str(row+1) + '"]/text()'
if self.tree.xpath(path):
rows.append(self.tree.xpath(path)[0])
else:
rows.append('')
self.clues.append(rows)
def getClues(self):
return self.clues
def setAnswers(self):
self.answers = []
for category in range(0,6):
rows = []
for row in range(0,5):
# Answers
path = '//div[@onclick="togglestick(\'clue_J_'+ str(category+1) + '_' + str(row+1) + '_stuck\')"]/@onmouseover'
if self.tree.xpath(path):
toggle_js = self.tree.xpath(path)[0]
answer = re.findall(r'correct_response">(.*)</em>', str(toggle_js))
rows.append(answer[0]) if answer else rows_ans.append('')
else:
rows.append('')
self.answers.append(rows)
def getAnswers(self):
return self.answers
def setI(self, i):
self.i = i
def getI(self):
return self.i