-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki_game.py
243 lines (189 loc) · 7.14 KB
/
wiki_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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import asyncio
import aiohttp
import time
from wiki_parser import *
from loguru import logger as Logger
from aiolimiter import AsyncLimiter
class WikiGame:
def play(self, startPageName: str, endPageName: str) -> list[str]:
raise NotImplementedError
class WikiGameDumb(WikiGame):
class Node:
def __init__(self, pageName: str, depth: int, pred=None):
self.pred = pred
self.pageName = pageName
self.depth = depth
def __init__(self):
self.wikiParser = WikiParserDumb()
def play(self, startPageName: str, endPageName: str, maxDepth: int = None) -> list[str]:
Logger.info(
f"Started playing\n\t" +
f"Start page: '{startPageName}'\n\t" +
f"End page: '{endPageName}'\n\t" +
f"Max depth: {maxDepth}"
)
# Implementation is BFS
startNode = self.Node(startPageName, 0)
visitedPages = set(startPageName)
nodesQueue = [startNode]
while len(nodesQueue) != 0:
curNode = nodesQueue.pop(0)
Logger.debug(
f"\n\tParsing '{curNode.pageName}'\n\t" +
f"Depth: {curNode.depth}\n\t" +
f"Queue size: {len(nodesQueue)}"
)
Logger.debug(f"Pred: {curNode.pred.pageName if curNode.pred is not None else curNode.pred}")
links = self.wikiParser.getLinks(curNode.pageName)
for link in links:
if link == endPageName:
Logger.success("Path found!")
linkNode = self.Node(link, curNode.depth + 1, curNode)
return self._getPath(linkNode)
if (link not in visitedPages) and (maxDepth is None or curNode.depth + 1 < maxDepth):
visitedPages.add(link)
linkNode = self.Node(link, curNode.depth + 1, curNode)
nodesQueue.append(linkNode)
Logger.error("Path not found, depth limit reached :(")
return []
def _getPath(self, node: Node) -> list[str]:
path = []
curNode = node
while curNode.pred is not None:
path.append(curNode.pageName)
curNode = curNode.pred
path.append(curNode.pageName)
path.reverse()
Logger.success("Path is:\n\t" + " -> ".join([f"'{p}'" for p in path]))
return path
class WikiGameAsync(WikiGame):
URL = 'https://en.wikipedia.org/w/api.php'
REQ_N = 50
class Node:
def __init__(self, pageName: str, depth: int, pred=None):
self.pred = pred
self.pageName = pageName
self.depth = depth
def __init__(self):
self.ioloop = asyncio.get_event_loop()
self.visitedPages = set()
async def _makeRequest(self, page: str, predNode: Node):
params = {
'action': 'query',
'titles': page,
'format': 'json',
'prop': 'links',
'pllimit': 'max'
}
nd = self.Node(
page,
0 if predNode is None else predNode.depth + 1,
predNode
)
Logger.debug(f"Making request for page '{page}', depth = {nd.depth}")
async with self.limiter:
return (await self.session.get(self.URL, params=params), nd)
@staticmethod
async def _getTasksLinks(responses):
texts = []
for (resp, node) in responses:
try:
rj = await resp.json()
texts.append((rj, node))
except:
print(await resp.text())
raise
# [(await resp.json(), node) for (resp, node) in responses]
text_list = []
for (json, node) in texts:
try:
recTitles = list(json['query']['pages'].values())[0]['links']
text_list.append((recTitles, node))
except KeyError:
# This is needed for links that have no existing page
continue
links = [
(rec['title'], node)
for (recTitles, node) in text_list
for rec in recTitles
if ':' not in rec['title']
]
return links
async def _play(self, startPage: str) -> Node:
self.limiter = AsyncLimiter(150, 1)
finalNode = None
self.visitedPages.add(startPage)
tasks = [
asyncio.create_task(
self._makeRequest(startPage, None)
)
]
startTime = time.time()
parsedLen = 0
linksQueue = []
while True:
done, pending = await asyncio.wait(
tasks,
return_when=asyncio.FIRST_COMPLETED
)
newLinks = await self._getTasksLinks(
[task.result() for task in done]
)
linksQueue.extend(newLinks)
# newLinks = linksQueue[:self.REQ_N]
# linksQueue = linksQueue[self.REQ_N:]
parsedLen += len(done)
rps = round(parsedLen / (time.time() - startTime), 2)
Logger.info(f'RPS: {rps}')
# await asyncio.sleep(1)
newTasks = []
for (page, node) in newLinks:
# Logger.debug(f"Got new page '{page}'")
if page == self.endPageName:
finalNode = self.Node(page, node.depth + 1, node)
return finalNode
if page not in self.visitedPages and node.depth < self.maxDepth:
self.visitedPages.add(page)
newTasks.append(
asyncio.create_task(
self._makeRequest(page, node)
)
)
pending = list(pending)
pending.extend(newTasks)
if len(pending) == 0:
break
tasks = pending
return finalNode
def play(self, startPageName: str, endPageName: str, maxDepth: int = None) -> list[str]:
self.session = aiohttp.ClientSession()
Logger.info(
f"Started playing\n\t" +
f"Start page: '{startPageName}'\n\t" +
f"End page: '{endPageName}'\n\t" +
f"Max depth: {maxDepth}"
)
self.maxDepth = maxDepth
self.endPageName = endPageName
start_timestamp = time.time()
nd = self.ioloop.run_until_complete(self._play(startPageName))
task_time = round(time.time() - start_timestamp, 2)
rps = round(len(self.visitedPages) / task_time, 1)
Logger.info(f"RPS: {rps}")
Logger.info(f"Parsed pages: {len(self.visitedPages)}")
self.ioloop.stop()
if nd is None:
Logger.error("Path not found, depth limit reached :(")
return []
Logger.success("Path found!")
return self._getPath(nd)
def _getPath(self, node: Node) -> list[str]:
path = []
curNode = node
while curNode.pred is not None:
path.append(curNode.pageName)
curNode = curNode.pred
path.append(curNode.pageName)
path.reverse()
Logger.success("Path is:\n\t" + " -> ".join([f"'{p}'" for p in path]))
return path