diff --git a/leetcode-py-cli.py b/leetcode-py-cli.py index 451e32c..b97f42a 100644 --- a/leetcode-py-cli.py +++ b/leetcode-py-cli.py @@ -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: @@ -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 @@ -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() \ No newline at end of file + parser.print_help() diff --git a/src/config.py b/src/config.py index 95ba420..f6a78df 100644 --- a/src/config.py +++ b/src/config.py @@ -2,6 +2,7 @@ from dotenv import load_dotenv import requests import json +import itertools load_dotenv() @@ -65,7 +66,6 @@ def getSubmissionDirectory(): if not os.path.isfile(directory + "submission.json"): open(directory + "submission.json", "w").close() - return directory @@ -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"][ @@ -126,4 +127,36 @@ def getSolvedQuestions(): } ) - return solvedQuestions \ No newline at end of file + 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 diff --git a/src/service_async.py b/src/service_async.py index cca532a..3557339 100644 --- a/src/service_async.py +++ b/src/service_async.py @@ -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()