Skip to content

Commit

Permalink
add cookies support when fetching status from leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
oddcc committed May 8, 2024
1 parent a69071c commit b535f3a
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 33 deletions.
1 change: 1 addition & 0 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
run: |
echo "LEETCODE_ACCOUNT=${{secrets.LEETCODE_ACCOUNT}}" >> $GITHUB_ENV
echo "LEETCODE_PASSWORD=${{secrets.LEETCODE_PASSWORD}}" >> $GITHUB_ENV
echo "COOKIE_STRING=${{secrets.COOKIE_STRING}}" >> $GITHUB_ENV
# fetch questions info from leetcode
- name: fetch questions info
Expand Down
7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions .idea/leetcode/editor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions .idea/leetcode/statistics.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions .idea/modules.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
Expand Down Expand Up @@ -40,6 +42,8 @@ public class Application implements CommandLineRunner {
@Autowired
private Config config;

private boolean useCookies;

private String outputFile;

private HttpClient client;
Expand All @@ -58,23 +62,60 @@ public void run(String... args) throws Exception {
}

private void prepare(String[] args) throws IOException {
if (config.getAccount() == null || config.getAccount().isEmpty()) {
throw new RuntimeException("account is empty");
}
if (config.getPassword() == null || config.getPassword().isEmpty()) {
throw new RuntimeException("password is empty");
if (config.getCookieString() == null || config.getCookieString().isEmpty()) {
useCookies = false;
if (config.getAccount() == null || config.getAccount().isEmpty()) {
throw new RuntimeException("account is empty");
}
if (config.getPassword() == null || config.getPassword().isEmpty()) {
throw new RuntimeException("password is empty");
}
} else {
useCookies = true;
}

if (args.length > 0) {
outputFile = args[0];
} else {
outputFile = "../all.json";
}
CookieStore cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
setCookies();
client = HttpClientBuilder
.create()
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofSeconds(3)))
.build();
login();
if (useCookies) {
verify();
} else {
login();
}
}

private void setCookies() {
CookieStore cookieStore = new BasicCookieStore();
if (useCookies) {
List<Cookie> cookieList = new ArrayList<>();
String[] cookies = config.getCookieString().split(";");
for (String cookieString : cookies) {
String[] cookie = cookieString.trim().split("=");
if (cookie.length >= 2) {
try {
BasicClientCookie basicClientCookie = new BasicClientCookie(cookie[0], cookie[1]);
basicClientCookie.setDomain(".leetcode.cn");
basicClientCookie.setPath("/");
cookieList.add(basicClientCookie);
} catch (IllegalArgumentException ex) {
log.error("Error when setting cookies", ex);
}
}
}

for (Cookie cookie : cookieList) {
cookieStore.addCookie(cookie);
}
}
httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}

private void updateQuestionsInfo() throws IOException, InterruptedException {
Expand Down Expand Up @@ -180,6 +221,10 @@ private void login() throws IOException {
log.info("login success");
}

verify();
}

private void verify() throws IOException {
ClassicHttpRequest verify = ClassicRequestBuilder
.get("https://leetcode.cn/points/api/")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
public class Config {
private String account;
private String password;
private String cookieString;
}
3 changes: 2 additions & 1 deletion update-lc-questions-jar/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
leetcode:
account: ${LEETCODE_ACCOUNT}
password: ${LEETCODE_PASSWORD}
password: ${LEETCODE_PASSWORD}
cookieString: ${COOKIE_STRING}

0 comments on commit b535f3a

Please sign in to comment.