/AlgoMonad

Primary LanguageHaskellBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

AlgoMonad

Author

Karl Wang

Introduction

AlgoMoond is a haskell TUI that allows user to download LeetCode questions through the TUI to their local machines, edit them in their favorite editors, and submit them through the TUI.

The motivation to create this program is that LeetCode only allows the user to edit and submit the code on their website. However, programmers tend to want to edit code in their favorite editors instead of in the browser. Hence, I would like to create a interface that allows the user to download to local machine and submit from local machine.

Installation

Clone the repository and run stack install to install the TUI. To run the test cases, run stack test

Guide

Login Credentials

AlgoMonad needs to use the cookie credentials from leetcode in order to acquire user leetcode information. If you are on MacOS, you can either acquire the credentials manually or automatically. If you are on other platforms, currently you can only acquire the credentials manually.

Manual

If not already login, login to leetcode account on leetcode.com on a Chromium browser.

After that, go to chrome://settings/cookies/detail?site=leetcode.com to copy the value of LEETCODE_SESSION and csrftoken under Content

Then create the file ~/.algomonad.yaml and write the file as followed:

LEETCODE_SESSION: YOUR_LEETCODE_SESSION_VALUE
csrftoken: YOUR_CSRFTOKEN_VALUE

Automatic

Currently, the automatic cookie extraction only works on MacOS with Chrome browser.

If not already login, login to leetcode account on leetcode.com on Google Chrome browser.

Then run algomonad login on the shell. This should create a file at ~/.algomonad.yaml with two keys, LEETCODE_SESSION and csrftoken.

If your Chrome profile path is not the default profile path, you can go to chrome://version, and copy the profile path under Profile Path. After that, you can acquire the credentials with algomonad login "PROFILE_PATH". An example of using the command should look like algomonad login "/Users/username/Library/Application Support/Google/Chrome/Profile 1".

Example

An example of the config under ~/.algomonad.yaml file should look like the following:

LEETCODE_SESSION: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfYXV0aF91c2VyX2lkIjoiMjAzNDM3MCIsIl9hdXRoX3VzZXJfYmFja2VuZCI6ImFsbGF1dGguYWNjb3VudC5hdXRoX2JhY2tlbmRzLkF1dGhlbnRpY2F0aW9uQmFja2VuZCaaksi9hdXRoX3VzqXJfaGFzaCI6IjM3ZDgxYzI0NTFkZWIxOTAzZDk2ZTMzMTY1YzIwMzgwMTc3YTYxMDciLCJpZCI6MjAzNDM3MCwiZW1haWwiOiJrYXdhbmdAdWNzZC5lZHUiLCJ1c2VybmFtZSI6Imthd2FuZyIsInVzZXJfc2x1ZyI6Imthd2FuZyIsImF2YXRhciI6Imh0dHBzOi8vYXNzZXRzLmxlZXRjb2RlLmNvbS91c2Vycy9rYXdhbmcvYXZhdGFyXzE1NjEyMjg5ODMucG5nIiwiqiwkmVzaGVkX2F0IjoxNjM5MDE0NDkxLCJpcCI6Ijc1LjgwLjEwOC4xNzIiLCJpZGVudGl0eSI6IjAzMjE0NWI4NDc3ZDZiM2ZiMzRlMGU1OTU0YjU5YjI0Iiwic2Vzc2lvbl9pZCI6MTU0ODI2NjZ9.K-jh_WU-KOknhPRYyh4OiRs8vqGnRc1JU8tmILGoHiM
csrftoken: W43GFtqFER8nADnf9apPmfUTYqrkPRz1XN6V712GAJWuE1RxUThv9QlQyGlb5Prh

Usage

With the credentials acquired, now run algomonad on the shell. You should be greeted with the following interface

List

AlgoMonad uses vim keybindings. This means that use k/j to move up and down. l to select question. h to go back to the problem list. Ctrl-d to move half page down and Ctrl-u to move half page up.

If a language is selected, use enter to download the question. The question will be downloaded to the current directory.

When in problem list, use / to do a case insensitive search by title, with n to search the next occurrence. Use = to search by problem id.

Use q to quit.

Architecture

The architecture is split into two parts, frontend and backend. The frontend mainly deals with Brick library to handle user events and updating the states. The backend is responsible for network requests to get problems and user information.

Some of the key components are the following:

  • UserInfo: get the username and whether the user has leetcode premium.
  • Problem: get the list of problems and their statistics.
  • ProblemDetail: get the code and write up of the problem.

Arch

Challenges

The biggest challenge I have is to acquire the cookies from Browser. The location of the cookies are not only browser and platform dependent, but also the decryption of the cookies differs depending on the platforms.

Libraries

Here is a list of essential libraries needed for this program

  • req: to make request
  • brick: to make TUI
  • cryptonite: to decrypt the Chrome cookies
  • sqlite-simple: to connect the sqlite cookie database
  • yaml: to extract the key value pair from yaml file
  • aeson: to handle JSON object from the response

Goals

  • Acquire user token automatically through browser cookies.
  • Browse LeetCode questions.
  • Download and write LeetCode questions to local files.
  • Submit local files to LeetCode and evaluate result.

Inspirations

  • leetcode-cli: Main source of inspiration. However, this is just a pure cli instead of a TUI. Implemented in javascript.
  • vscode-leetcode: A GUI for LeetCode. However, only available on VSCode. Implemented in javascript.