clearloop / leetcode-cli

May the code be with you 👻
MIT License
307 stars 49 forks source link

Failure due to Json Error when Running test/exec #144

Closed v8yte closed 6 months ago

v8yte commented 6 months ago

Despite correctly specifying the session and csrf config, I encounter the following error. I have also performed leetcode data --delete and cleared the cache.

leetcode test 1

[INFO  leetcode_cli::plugins::leetcode] Sending code to judge...
JSON error: expected value at line 2 column 1, please double check your session and csrf config. JSON error: expected value at line 2 column 1, please double check your session and csrf config.

leetcode -d test 1

[2024-01-02T01:08:08Z INFO  leetcode_cli::plugins::leetcode] Sending code to judge...
[2024-01-02T01:08:08Z DEBUG reqwest::connect] starting new connection: https://leetcode.com/
JSON error: expected value at line 2 column 1, please double check your session and csrf config. JSON error: expected value at line 2 column 1, please double check your session and csrf config.
leetcode exec 1

[INFO  leetcode_cli::plugins::leetcode] Sending code to judge...
JSON error: expected value at line 2 column 1, please double check your session and csrf config. JSON error: expected value at line 2 column 1, please double check your session and csrf config.

leetcode -d exec 1

[2024-01-02T01:08:48Z INFO  leetcode_cli::plugins::leetcode] Sending code to judge...
[2024-01-02T01:08:48Z DEBUG reqwest::connect] starting new connection: https://leetcode.com/
JSON error: expected value at line 2 column 1, please double check your session and csrf config. JSON error: expected value at line 2 column 1, please double check your session and csrf config.
cat leetcode.toml

[code]
editor = 'vim'
lang = 'golang'

[cookies]
csrf = "${csrftoken}"
session = "${LEETCODE_SESSION}"

[storage]
cache = 'Problems'
code = 'code'
root = '~/.leetcode'
scripts = 'scripts'
v8yte commented 6 months ago

I tried outputting JSON data as a test.

leetcode-test test 1

[INFO  leetcode_cli::plugins::leetcode] Sending code to judge...
Ok(Response 
{
  "url": {
    "scheme": "https",
    "cannot_be_a_base": false,
    "username": "",
    "password": null,
    "host": {
      "Some(Domain(\"leetcode.com\"))": null
    },
    "port": null,
    "path": "/problems/two-sum/interpret_solution/",
    "query": null,
    "fragment": null
  },
  "status": 499,
  "headers": {
    "date": "Tue, 02 Jan 2024 03:23:29 GMT",
    "content-type": "text/html; charset=utf-8",
    "transfer-encoding": "chunked",
    "connection": "keep-alive",
    "x-frame-options": "DENY",
    "vary": "Cookie",
    "set-cookie": "LEETCODE_SESSION=xxxxxxxxxxxxxxxxxxxxx; Domain=.leetcode.com; expires=Tue, 16 Jan 2024 03:23:29 GMT; HttpOnly; Max-Age=1209600; Path=/; SameSite=Lax; Secure",
    "strict-transport-security": "max-age=31536000; includeSubDomains; preload",
    "cf-cache-status": "DYNAMIC",
    "report-to": "{\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v3?s=udQc2h4AW9GczEMhltij9BKkdJH85lbkfedlAvnEhNcb6Jj8swHjDTW9mcCBMO27akACKCdQNftAA0DqrPeHINUrmbA0ZaRYAxLG0X4F6H23dRGizILhLGva2RM1%2FA%3D%3D\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
    "nel": "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}",
    "x-content-type-options": "nosniff",
    "server": "cloudflare",
    "cf-ray": "83efe2339b28afe8-NRT"
  }
})
Anyhow(JSON error: expected value at line 2 column 1, please double check your session and csrf config.)
clearloop commented 6 months ago

could you please try re-configuring your csrf and session following https://github.com/clearloop/leetcode-cli?tab=readme-ov-file#example

v8yte commented 6 months ago

thx. I encountered an issue with the [cookies] section in the configuration file. Instead of using:

[cookies]
csrf = "${csrftoken}"
session = "${LEETCODE_SESSION}"

I had to directly write the values as follows to make it work:

[cookies]
csrf = 'xxxxxxx'
session = 'xxxxxxxxx'

I have set the environment variables, and they output correctly when I use echo ${csrftoken} or echo ${LEETCODE_SESSION}. However, it seems that the .toml file is not retrieving the values from ${csrftoken} or ${LEETCODE_SESSION} for some reason. The cause is still unknown to me. As a workaround, I am directly including these values in the configuration.

clearloop commented 6 months ago

I encountered an issue with the [cookies] section in the configuration file. Instead of using:

makes sense, need to update the readme

v8yte commented 6 months ago

ok.I will update the relevant part of the readme.

clearloop commented 6 months ago

Closing this since the issue has already been solved by #145

cjiung123 commented 1 month ago

This issue remains unresolved, and I can consistently reproduce it even though I'm directly setting the CSRF and session values in the .toml file.

Steps to reproduce: Run leetcode edit 621 Write the following code:

#include <vector>
#include <queue>

class Solution {
public:
    int leastInterval(std::vector<char>& tasks, int n) {
        std::vector<int> freqMap(26);
        for(char task : tasks) {
            freqMap[task - 'A']++;
        }
        std::priority_queue<int> pq;
        for(int freq : freqMap) {
            if(freq > 0) {
                pq.push(freq);
            }
        }
        int result = 0;
        while(!pq.empty()) {
            int cycle = n + 1;
            int taskCount = 0;
            std::vector<int> remainingTasks;
            while(cycle-- && !pq.empty()) {
                if(pq.top() > 1) {
                    remainingTasks.push_back(pq.top() - 1);
                }
                pq.pop();
                taskCount++;
            }
            for(int nextTask : remainingTasks) {
                pq.push(nextTask);
            }
            result += pq.empty() ? taskCount : n + 1;
        }
        return result;
    }
};

Run leetcode test 621 and/or leetcode exec 621

This should fail with an error message

[INFO  leetcode_cli::plugins::leetcode] Sending code 
to judge...
JSON error: expected value at line 1 column 1, please double check your session and csrf config. JSON error: expected value at line 1 column 1, please double check your session and csrf config.

However, when I swap the conditions on line 22, while(cycle-- && !pq.empty()) to while(!pq.empty() && cycle--), running leetcode test 621 and leetcode exec 621 passes successfully.

And of course, the both version of the code pass without issue when written directly on the Leetcode website.

My .toml file

cat leetcode.toml


[code]
editor = 'nvim'
lang = 'cpp'

[cookies] csrf = '' session = ''

[storage] cache = 'Problems' code = 'code' root = '~/.leetcode' scripts = 'scripts'

clearloop commented 1 month ago

@cjiung123 some of the problems are failed to run the test command due to unknown reasons ))) could you please try problems like 1, 2 or 620 and 622?

jay1999ke commented 1 month ago

I just tried it with 1 and it fails

cjiung123 commented 1 month ago

@clearloop I found out that this is highly likely due to Cloudflare Challenge. So basically when a website is protected by Cloudflare, there are several occasions when it will challenge visitor traffic, and this particular case is due to JS challenge, and it requires no interaction from a visitor, but rather visitor will have to wait until it finishes processing the Javascript, which should be less than five seconds.

If we could implement something that mimics the waiting process (ex. sleep(4000)) then save the cookies, then go for a retry, I think it might be worth a try..

cjiung123 commented 1 month ago

Nevermind, it's way more complex than I thought

v8yte commented 1 month ago

I am currently out and unable to verify, but is this an error that can be reproduced in a specific case every time? Or is it sometimes not reproducible depending on the individual environment? If it is an error related to Cloudflare, it seems like it would be reproducible every time...

cjiung123 commented 1 month ago

I'm able to reproduce everytime on my Ubuntu VM. Try following steps to reproduce on my previous comment when you get a chance.