-
Notifications
You must be signed in to change notification settings - Fork 71
/
update-challenge-list.py
executable file
·72 lines (65 loc) · 2.47 KB
/
update-challenge-list.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
#!/usr/bin/env python
#
# This script will fetch a list of challenges from hackerrank.com
# and save it to '/challenges.json'
#
# Dependency:
# * scrapy >=1.3
#
# This script is compatible with python 2.7 and 3.x (if applicable)
import functools
import json
import os.path
import scrapy
import scrapy.crawler
tracks = []
class HackerSpider(scrapy.Spider):
name = 'list-challenge'
def start_requests(self):
tracks_list = [
{ 'title': 'Algorithms', 'name': 'algorithms' },
{ 'title': 'Data Structures', 'name': 'data-structures' },
{ 'title': 'Mathematics', 'name': 'mathematics' },
]
for i, track in enumerate(tracks_list):
tracks.append({
'title': track['title'],
'name': track['name'],
'chapters': [],
})
url = 'https://www.hackerrank.com/rest/contests/master/tracks/' + track['name'] + '/chapters'
yield scrapy.Request(url=url, callback=functools.partial(self.parse_chapters, d={
'track-id': i,
}))
def parse_chapters(self, response, d):
json_object = json.loads(response.text)
for i, chapter in enumerate(json_object['models']):
tracks[d['track-id']]['chapters'].append({
'title': chapter['name'],
'name': chapter['slug'],
'challenges': [None] * chapter['challenges_count'],
})
for offset in range(0, chapter['challenges_count'], 10):
url = 'https://www.hackerrank.com/rest/contests/master/categories/' \
+ tracks[d['track-id']]['name'] + '%7C' + chapter['slug'] \
+ '/challenges?offset=' + str(offset) + '&limit=10'
yield scrapy.Request(url=url, callback=functools.partial(self.parse_page, d={
'track-id': d['track-id'],
'chapter-id': i,
'offset': offset,
}))
def parse_page(self, response, d):
json_object = json.loads(response.text)
for i, challenge in enumerate(json_object['models']):
tracks[d['track-id']]['chapters'][d['chapter-id']]['challenges'][d['offset'] + i] = {
'title': challenge['name'],
'name': challenge['slug'],
}
if __name__ == '__main__':
process = scrapy.crawler.CrawlerProcess({
'USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0'
})
process.crawl(HackerSpider)
process.start()
with open(os.path.realpath(os.path.dirname(__file__) + '/../challenges.json'), 'w') as f:
f.write(json.dumps({'tracks': tracks}, indent=2, separators=(',', ': ')))