Skip to content

Commit ff3063b

Browse files
committed
Implement caching for contests
1 parent 7e1ac16 commit ff3063b

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

ACedIt/util.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def check_cache(site, contest, problem):
8080
If not, create the directory structure to store test cases
8181
"""
8282
if problem is None:
83+
if not os.path.isdir(os.path.join(Utilities.cache_dir, site, contest)):
84+
os.makedirs(os.path.join(Utilities.cache_dir, site,
85+
contest))
8386
return False
8487

8588
if os.path.isdir(os.path.join(Utilities.cache_dir, site, contest, problem)):
@@ -117,7 +120,8 @@ def download_problem_testcases(args):
117120
else:
118121
platform = Hackerrank(args)
119122

120-
is_in_cache = Utilities.check_cache(platform.site, platform.contest, platform.problem)
123+
is_in_cache = Utilities.check_cache(
124+
platform.site, platform.contest, platform.problem)
121125

122126
if not args['force'] and is_in_cache:
123127
print "Test cases found in cache..."
@@ -134,6 +138,9 @@ def download_contest_testcases(args):
134138
elif args["site"] == "hackerrank":
135139
platform = Hackerrank(args)
136140

141+
Utilities.check_cache(
142+
platform.site, platform.contest, platform.problem)
143+
137144
platform.scrape_contest()
138145

139146
@staticmethod
@@ -158,7 +165,7 @@ def __init__(self, args):
158165
self.site = args["site"]
159166
self.contest = args["contest"]
160167
self.problem = args["problem"]
161-
168+
self.force_download = args["force"]
162169

163170
def parse_html(self, req):
164171
"""
@@ -213,7 +220,8 @@ def scrape_problem(self):
213220
self.contest + "/problem/" + self.problem
214221
req = Utilities.get_html(url)
215222
inputs, outputs = self.parse_html(req)
216-
Utilities.store_files(self.site, self.contest, self.problem, inputs, outputs)
223+
Utilities.store_files(self.site, self.contest,
224+
self.problem, inputs, outputs)
217225

218226
def scrape_contest(self):
219227
"""
@@ -227,12 +235,22 @@ def scrape_contest(self):
227235
print "Found problems"
228236
print "\n".join(links)
229237

238+
if not self.force_download:
239+
cached_problems = os.listdir(os.path.join(
240+
Utilities.cache_dir, self.site, self.contest))
241+
links = [link for link in links if link.split(
242+
"/")[-1] not in cached_problems]
243+
230244
rs = (grq.get(link) for link in links)
231245
responses = grq.map(rs)
232246

233247
for response in responses:
234248
if response is not None and response.status_code == 200:
235249
inputs, outputs = self.parse_html(response)
250+
self.problem = response.url.split("/")[-1]
251+
Utilities.check_cache(self.site, self.contest, self.problem)
252+
Utilities.store_files(
253+
self.site, self.contest, self.problem, inputs, outputs)
236254

237255

238256
class Codechef:
@@ -244,7 +262,7 @@ def __init__(self, args):
244262
self.site = args["site"]
245263
self.contest = args["contest"]
246264
self.problem = args["problem"]
247-
265+
self.force_download = args["force"]
248266

249267
def parse_html(self, req):
250268
"""
@@ -309,7 +327,8 @@ def scrape_problem(self):
309327
self.contest + "/problems/" + self.problem
310328
req = Utilities.get_html(url)
311329
inputs, outputs = self.parse_html(req)
312-
Utilities.store_files(self.site, self.contest, self.problem, inputs, outputs)
330+
Utilities.store_files(self.site, self.contest,
331+
self.problem, inputs, outputs)
313332

314333
def scrape_contest(self):
315334
"""
@@ -323,6 +342,12 @@ def scrape_contest(self):
323342
print "Found problems"
324343
print "\n".join(links)
325344

345+
if not self.force_download:
346+
cached_problems = os.listdir(os.path.join(
347+
Utilities.cache_dir, self.site, self.contest))
348+
links = [link for link in links if link.split(
349+
"/")[-1] not in cached_problems]
350+
326351
rs = (grq.get(link) for link in links)
327352
responses = grq.map(rs)
328353

@@ -331,6 +356,10 @@ def scrape_contest(self):
331356
for response in responses:
332357
if response is not None and response.status_code == 200:
333358
inputs, outputs = self.parse_html(response)
359+
self.problem = response.url.split("/")[-1]
360+
Utilities.check_cache(self.site, self.contest, self.problem)
361+
Utilities.store_files(
362+
self.site, self.contest, self.problem, inputs, outputs)
334363

335364

336365
class Spoj:
@@ -342,7 +371,7 @@ def __init__(self, args):
342371
self.site = args["site"]
343372
self.contest = args["contest"]
344373
self.problem = args["problem"]
345-
374+
self.force_download = args["force"]
346375

347376
def parse_html(self, req):
348377
"""
@@ -390,7 +419,8 @@ def scrape_problem(self):
390419
url = "http://spoj.com/problems/" + self.problem
391420
req = Utilities.get_html(url)
392421
inputs, outputs = self.parse_html(req)
393-
Utilities.store_files(self.site, self.contest, self.problem, inputs, outputs)
422+
Utilities.store_files(self.site, self.contest,
423+
self.problem, inputs, outputs)
394424

395425

396426
class Hackerrank:
@@ -402,7 +432,7 @@ def __init__(self, args):
402432
self.site = args["site"]
403433
self.contest = args["contest"]
404434
self.problem = "-".join(args["problem"].split()).lower()
405-
435+
self.force_download = args["force"]
406436

407437
def parse_html(self, req):
408438
"""
@@ -461,4 +491,5 @@ def scrape_problem(self):
461491
self.contest + "/challenges/" + self.problem
462492
req = Utilities.get_html(url)
463493
inputs, outputs = self.parse_html(req)
464-
Utilities.store_files(self.site, self.contest, self.problem, inputs, outputs)
494+
Utilities.store_files(self.site, self.contest,
495+
self.problem, inputs, outputs)

0 commit comments

Comments
 (0)