diff --git a/README.md b/README.md index d11da1b3..f4d4a4a4 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,28 @@ Crawls solutions of hackerrank and stores as local files. - Login with your Hackerrank Credentials - Enter the limit of successful solutions you want to be crawled - A new folder with name **Hackerrank** would be created with all your solutions in it + +## Options to use while running script +Script `hsc` supports following options +- help: -h or --help +- username: -u or --username -> username of hackerrank profile +- password: -p or --password -> password of hackerrank profile +- limit: -l or --limit -> no. of solutions to be downloaded +- offset: -o or --offset -> crawl solutions starting from this number +- config: -c or --config -> path of config file + +Usage: +We can use above script helpers as +```bash +hsc -l 34 -p testpassword -u testuser +``` + +We can also use config file to download solutions +Let config file be /etc/user.yaml +```yaml +username: testuser +``` + +```bash +hsc -c /etc/user.yaml -l 34 -p testpassword +``` diff --git a/hsc/crawler.py b/hsc/crawler.py index 57623946..5b174a2c 100755 --- a/hsc/crawler.py +++ b/hsc/crawler.py @@ -2,6 +2,7 @@ import json import requests import getpass +import configargparse from progress.bar import ChargingBar @@ -105,6 +106,7 @@ class Crawler: def __init__(self): self.session = requests.Session() self.total_submissions = 0 + self.options = {} def login(self, username, password): resp = self.session.get(self.login_url, auth=(username, password)) @@ -113,9 +115,19 @@ def login(self, username, password): self.get_number_of_submissions() return self.total_submissions != 0 + def parse_script(self): + p = configargparse.ArgParser(default_config_files=['./user.yaml']) + p.add('-c', '--config', is_config_file=True, help='config file path') + p.add('-l', '--limit', help='limit to no. of solutions to be crawled') + p.add('-o', '--offset', help='crawl solutions starting from this number') + p.add('-u', '--username', help='hackerrank account username') + p.add('-p', '--password', help='hackerrank account password') + + self.options = p.parse_args() + def authenticate(self): - username = input('Hackerrank Username: ') - password = getpass.getpass('Hackerrank Password: ') + username = self.options.username or input('Hackerrank Username: ') + password = self.options.password or getpass.getpass('Hackerrank Password: ') return self.login(username, password) def get_number_of_submissions(self): @@ -223,15 +235,16 @@ def get_submissions(self, submissions): print('All Solutions Crawled') def main(): - offset = 0 - limit = 10 # you should change this crawler = Crawler() - - while(not crawler.authenticate()): - print('Auth was unsuccessful') - - limit = input('Enter limit needed to crawl: ') + crawler.parse_script() + if not crawler.authenticate(): + print('Auth was unsuccessful. Exiting the program') + exit(1) + + limit = crawler.options.limit or crawler.total_submissions + offset = crawler.options.offset or 0 + print('Start crawling {} solutions starting from {}'.format(limit, offset)) all_submissions_url = crawler.get_all_submissions_url(offset, limit) resp = crawler.session.get(all_submissions_url, headers=crawler.headers) diff --git a/setup.py b/setup.py index 1c8fea63..34f26485 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='hsc', - version='1.1.3', + version='1.2.0', author='Nullifiers', author_email='nullifiersorg@gmail.com', description='Hackerrank Solution Crawler', @@ -24,5 +24,5 @@ 'hsc=hsc.crawler:main', ], }, - install_requires=['progress', 'requests'] + install_requires=['progress', 'requests', 'configargparse'] )