Skip to content

Functionality to gather statistics of completion #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions leetcode-py-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,16 @@
default=0,
)

parser.add_argument("-v", "--version", action="version", version="%(prog)s 1.0")
parser.add_argument(
"-b",
"--stats",
help="Get your stats",
action='store_true',
default=0,
)

parser.add_argument("-v", "--version", action="version",
version="%(prog)s 1.0")
args = parser.parse_args()

if args.all:
Expand All @@ -73,14 +82,18 @@
elif args.submission:
downloadSubmission(int(args.submission))

elif args.list_questions:
elif args.stats:
listStats()

elif args.list_questions:
listSubmission()
ls = []
for i in init.jsonfile:
j = list(i.values())
j = list(i.values())
ls.append([j[0], j[1], j[4], j[5], j[6], j[7]])

print(tabulate(sorted(ls, key= lambda x : x[0]), headers=["No.","Title", 'Submitted On', 'Memory', 'Time', 'Langauge'], tablefmt='fancy_grid'))
print(tabulate(sorted(ls, key=lambda x: x[0]), headers=[
"No.", "Title", 'Submitted On', 'Memory', 'Time', 'Langauge'], tablefmt='fancy_grid'))

elif args.login:
# Add a check
Expand All @@ -91,24 +104,25 @@
set_key('.env', 'LEETCODE_CSRFTOKEN', csrf)
else:
if not init.login_flag:
inp = input("You are already Logged in. Do you still want to change the details.[Y/n] : ")
inp = input(
"You are already Logged in. Do you still want to change the details.[Y/n] : ")
if inp.lower() == 'y':
session = input("Enter LEETCODE_SESSION : ")
csrf = input("\nEnter csrftoken : ")
set_key('.env', 'LEETCODE_SESSION', session)
set_key('.env', 'LEETCODE_CSRFTOKEN', csrf)

elif args.github:
# Add a check
if not checkGithubToken():
git = input('Enter Github token : ')
set_key('.env', 'GITHUB_TOKEN', git)
os.environ["GITHUB_TOKEN"] = git
load_dotenv()
load_dotenv()
downloadAllSubmissions()
initGit()
initGit()
elif args.force_update:
init.availableSubmissions = []
downloadAllSubmissions()
else:
parser.print_help()
parser.print_help()
37 changes: 35 additions & 2 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dotenv import load_dotenv
import requests
import json
import itertools

load_dotenv()

Expand Down Expand Up @@ -65,7 +66,6 @@ def getSubmissionDirectory():
if not os.path.isfile(directory + "submission.json"):
open(directory + "submission.json", "w").close()


return directory


Expand Down Expand Up @@ -118,6 +118,7 @@ def getSolvedQuestions():
question_list = json.loads(resp.content.decode("utf-8"))
for question in question_list["stat_status_pairs"]:
if question["status"] == "ac":

solvedQuestions.update(
{
question["stat"]["frontend_question_id"]: question["stat"][
Expand All @@ -126,4 +127,36 @@ def getSolvedQuestions():
}
)

return solvedQuestions
return solvedQuestions


def getStats():
resp = requests.get(
getBaseURL() + "/api/problems/all/", cookies=getCookies(), timeout=10
)
question_list = json.loads(
resp.content.decode("utf-8"))["stat_status_pairs"]
levels_summary = {}
translator = {
1: "e",
2: "m",
3: "h",
4: "super_hard",
"ac": "accepted",
None: "todo",
"notac": "in_progress"
}
status_set = set([x['status'] for x in question_list])
level_set = set([x['difficulty']['level'] for x in question_list])
levels_summary['a_total'] = len(question_list)
for status in status_set:
levels_summary[f'a_{translator[status]}'] = len(
[x for x in question_list if x['status'] == status])
for level in level_set:
levels_summary[f'{translator[level]}_total'] = len(
[x for x in question_list if x['difficulty']['level'] == level])
for level, status in itertools.product(level_set, status_set):
levels_summary[f'{translator[level]}_{translator[status]}'] = len(
[x for x in question_list if x['difficulty']['level'] == level and x['status'] == status])

return levels_summary
32 changes: 32 additions & 0 deletions src/service_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,38 @@ def downloadAllSubmissions():
spinner.stop()


class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'


def listStats():
spinner = Halo(text='Gathering your statistics', spinner='dots')
spinner.start()
stats = getStats()
init.stats = stats
spinner.succeed("Stats loaded successfully")
print(
f"All: \t{stats['a_todo']:6d} todo, {stats['a_accepted']:6d} done, "
f"{stats['a_in_progress']:6d} in progress, {stats['a_total']:6d} total")
print(
f"{bcolors.OKGREEN}Easy: \t{stats['e_todo']:6d} todo, {stats['e_accepted']:6d} done, "
f"{stats['e_in_progress']:6d} in progress, {stats['e_total']:6d} total{bcolors.ENDC}")
print(
f"{bcolors.WARNING}Medium: {stats['m_todo']:6d} todo, {stats['m_accepted']:6d} done, "
f"{stats['m_in_progress']:6d} in progress, {stats['m_total']:6d} total{bcolors.ENDC}")
print(
f"{bcolors.FAIL}Hard: \t{stats['h_todo']:6d} todo, {stats['h_accepted']:6d} done, "
f"{stats['h_in_progress']:6d} in progress, {stats['h_total']:6d} total{bcolors.ENDC}")


def listSubmission():
spinner = Halo(text='Gathering questions', spinner='dots')
spinner.start()
Expand Down