Skip to content

Commit 48e21c3

Browse files
Merge pull request #32 from Nullifiers/script-parameter
script parameters
2 parents 91c5265 + 2af0e58 commit 48e21c3

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,28 @@ Crawls solutions of hackerrank and stores as local files.
99
- Login with your Hackerrank Credentials
1010
- Enter the limit of successful solutions you want to be crawled
1111
- A new folder with name **Hackerrank** would be created with all your solutions in it
12+
13+
## Options to use while running script
14+
Script `hsc` supports following options
15+
- help: -h or --help
16+
- username: -u or --username -> username of hackerrank profile
17+
- password: -p or --password -> password of hackerrank profile
18+
- limit: -l or --limit -> no. of solutions to be downloaded
19+
- offset: -o or --offset -> crawl solutions starting from this number
20+
- config: -c or --config -> path of config file
21+
22+
Usage:
23+
We can use above script helpers as
24+
```bash
25+
hsc -l 34 -p testpassword -u testuser
26+
```
27+
28+
We can also use config file to download solutions
29+
Let config file be /etc/user.yaml
30+
```yaml
31+
username: testuser
32+
```
33+
34+
```bash
35+
hsc -c /etc/user.yaml -l 34 -p testpassword
36+
```

hsc/crawler.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import requests
44
import getpass
5+
import configargparse
56
from progress.bar import ChargingBar
67

78

@@ -105,6 +106,7 @@ class Crawler:
105106
def __init__(self):
106107
self.session = requests.Session()
107108
self.total_submissions = 0
109+
self.options = {}
108110

109111
def login(self, username, password):
110112
resp = self.session.get(self.login_url, auth=(username, password))
@@ -113,9 +115,19 @@ def login(self, username, password):
113115
self.get_number_of_submissions()
114116
return self.total_submissions != 0
115117

118+
def parse_script(self):
119+
p = configargparse.ArgParser(default_config_files=['./user.yaml'])
120+
p.add('-c', '--config', is_config_file=True, help='config file path')
121+
p.add('-l', '--limit', help='limit to no. of solutions to be crawled')
122+
p.add('-o', '--offset', help='crawl solutions starting from this number')
123+
p.add('-u', '--username', help='hackerrank account username')
124+
p.add('-p', '--password', help='hackerrank account password')
125+
126+
self.options = p.parse_args()
127+
116128
def authenticate(self):
117-
username = input('Hackerrank Username: ')
118-
password = getpass.getpass('Hackerrank Password: ')
129+
username = self.options.username or input('Hackerrank Username: ')
130+
password = self.options.password or getpass.getpass('Hackerrank Password: ')
119131
return self.login(username, password)
120132

121133
def get_number_of_submissions(self):
@@ -223,15 +235,16 @@ def get_submissions(self, submissions):
223235
print('All Solutions Crawled')
224236

225237
def main():
226-
offset = 0
227-
limit = 10 # you should change this
228238

229239
crawler = Crawler()
230-
231-
while(not crawler.authenticate()):
232-
print('Auth was unsuccessful')
233-
234-
limit = input('Enter limit needed to crawl: ')
240+
crawler.parse_script()
241+
if not crawler.authenticate():
242+
print('Auth was unsuccessful. Exiting the program')
243+
exit(1)
244+
245+
limit = crawler.options.limit or crawler.total_submissions
246+
offset = crawler.options.offset or 0
247+
print('Start crawling {} solutions starting from {}'.format(limit, offset))
235248
all_submissions_url = crawler.get_all_submissions_url(offset, limit)
236249

237250
resp = crawler.session.get(all_submissions_url, headers=crawler.headers)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='hsc',
8-
version='1.1.3',
8+
version='1.2.0',
99
author='Nullifiers',
1010
author_email='nullifiersorg@gmail.com',
1111
description='Hackerrank Solution Crawler',
@@ -24,5 +24,5 @@
2424
'hsc=hsc.crawler:main',
2525
],
2626
},
27-
install_requires=['progress', 'requests']
27+
install_requires=['progress', 'requests', 'configargparse']
2828
)

0 commit comments

Comments
 (0)