Skip to content

Commit

Permalink
Merge pull request #32 from Nullifiers/script-parameter
Browse files Browse the repository at this point in the history
script parameters
  • Loading branch information
rishabhsingh971 authored Oct 13, 2019
2 parents 91c5265 + 2af0e58 commit 48e21c3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
31 changes: 22 additions & 9 deletions hsc/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import requests
import getpass
import configargparse
from progress.bar import ChargingBar


Expand Down Expand Up @@ -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))
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -24,5 +24,5 @@
'hsc=hsc.crawler:main',
],
},
install_requires=['progress', 'requests']
install_requires=['progress', 'requests', 'configargparse']
)

0 comments on commit 48e21c3

Please sign in to comment.