From d82b2edfbcde48a50267a247d4276a87101542d2 Mon Sep 17 00:00:00 2001 From: Rajat Goyal Date: Mon, 5 Aug 2019 01:06:06 +0530 Subject: [PATCH 1/2] Get total number of submissions --- hsc/crawler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hsc/crawler.py b/hsc/crawler.py index 60dc5786..0712227c 100755 --- a/hsc/crawler.py +++ b/hsc/crawler.py @@ -80,13 +80,19 @@ def login(self, username, password): resp = self.session.get(self.login_url, auth=(username, password)) self.cookies = self.session.cookies.get_dict() self.headers = resp.request.headers - return resp.url != self.login_url + self.get_number_of_submissions() + return self.total_number_of_submissions != 0 def authenticate(self): username = input('Hackerrank Username: ') password = getpass.getpass('Hackerrank Password: ') return self.login(username, password) + def get_number_of_submissions(self): + all_submissions_url = self.get_all_submissions_url(0, 0) + resp = self.session.get(all_submissions_url, headers=self.headers) + self.total_number_of_submissions = resp.json()['total'] + def get_all_submissions_url(self, offset, limit): return self.submissions_url.format(offset, limit) From d4bd6f18b658af5da8297e1e7d8da246839ec17e Mon Sep 17 00:00:00 2001 From: Rajat Goyal Date: Wed, 7 Aug 2019 08:35:14 +0530 Subject: [PATCH 2/2] Changes as per review comments --- hsc/crawler.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/hsc/crawler.py b/hsc/crawler.py index 0712227c..34f60c86 100755 --- a/hsc/crawler.py +++ b/hsc/crawler.py @@ -81,7 +81,7 @@ def login(self, username, password): self.cookies = self.session.cookies.get_dict() self.headers = resp.request.headers self.get_number_of_submissions() - return self.total_number_of_submissions != 0 + return self.total_submissions != 0 def authenticate(self): username = input('Hackerrank Username: ') @@ -89,9 +89,11 @@ def authenticate(self): return self.login(username, password) def get_number_of_submissions(self): - all_submissions_url = self.get_all_submissions_url(0, 0) - resp = self.session.get(all_submissions_url, headers=self.headers) - self.total_number_of_submissions = resp.json()['total'] + if not self.total_submissions: + all_submissions_url = self.get_all_submissions_url(0, 0) + resp = self.session.get(all_submissions_url, headers=self.headers) + self.total_submissions = resp.json()['total'] + return self.total_submissions def get_all_submissions_url(self, offset, limit): return self.submissions_url.format(offset, limit)