Skip to content

Commit 31da640

Browse files
committed
update
1 parent be728df commit 31da640

File tree

2 files changed

+153
-44
lines changed

2 files changed

+153
-44
lines changed

README.md

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,27 @@
22

33
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/NeserCode/Leetcode-Api"> <img title="GitHub download" src="https://img.shields.io/github/downloads/NeserCode/Leetcode-Api/total"/>
44

5+
[中文文档](README.zh.md)
56

6-
7-
关于 力扣 的网络请求接口。
87
HttpRequest api for leetcode(zh-cn).
98

10-
## 目录
11-
12-
* [免责声明](#免责声明)
13-
* [项目总览](#项目总览)
14-
* [接口相关](#接口相关)
15-
* [接口详情](#接口详情)
9+
## Directory
1610

17-
## 免责声明
11+
* [Disclaimer](#Disclaimer)
12+
* [Project Overview](#Project Overview)
13+
* [About Interface](#About Interface)
14+
* [Interface Detail](#Interface Detail)
1815

19-
**本项目仅供学习交流参考,在任意情况下均不得用于任何商业用途。如若违反声明,后果自负,本人不承担任何责任。**
16+
## Disclaimer
2017

2118
**This project is for study and exchange only, and under no circumstances shall it be used for any commercial purposes. If you violate the statement, you are responsible for the consequences, and I do not assume any responsibility.**
2219

23-
## 项目总览
24-
25-
本项目提供的是 **力扣** 官方网站的 **WebApi** 集合,提出的 Api 均已试验成功。
20+
## Project Overview
2621

2722
This project provides the **WebApi** collection of **Leetcode**, and the proposed Api has been successfully tested.
2823

29-
本项目仅包含一个入口 Javascript 文件,将本文档中提出的 WebApi 全部封装到 $Leetcode 类中。
30-
3124
This project contains only an entry Javascript file that wraps all of the Webapis proposed in this document into the $Leetcode class.
3225

33-
所以,想使用 Leetcode-Api,你只需:
34-
3526
So, to use Leetcode-API, you just need to:
3627

3728
```javascript
@@ -40,19 +31,14 @@ import $Leetcode from "Leetcode-Api"
4031
const variable = new $Leetcode()
4132
```
4233

43-
然后通过上例中的 variable 使用 Leetcode-Api 中的函数方法。
44-
4534
Then use the function in Leetcode-API through the variable in the above example.
4635

47-
## 接口相关
48-
49-
力扣 官网使用了 [**GraphQL**](https://graphql.org/) 技术来获取数据,即 **按需分配数据** 的模式。同时,全部网络请求需要附着 Cookie 访问,部分网络请求需要确认 **Referer** 是否属于力扣域名之下。请使用这些 WebApi 之前制定好相关解决方案。
36+
## About Interface
5037

5138
Leetcode uses [**GraphQL**](https://graphql.org/) technology to obtain data, that is, the mode of **on-demand data distribution**. At the same time, all network requests need to be attached with Cookie access, and some network requests need to confirm whether the **Referer** belongs to Leetcode domain name. Develop a solution before using these WebApis.
5239

53-
### 部分解决方案
40+
### Partial Solution
5441

55-
在传统的 HTML5 项目中,如:
5642
In traditional HTML5 projects, such as:
5743

5844
```html
@@ -65,16 +51,10 @@ In traditional HTML5 projects, such as:
6551
|-index.html
6652
```
6753

68-
这种项目中,由于伪造 Referer 带来的不安全和危险,一般不会允许用户通过 Javascript 脚本去修改 HttpRequest 的表头中的几项,也就是 RequestHeader 中的几项,其中就包含有 Referer 项。
69-
7054
In such projects, due to the safety and risk of forgery of the Referer, it is generally not allowed to modify the items in the HttpRequest header, i.e. the items in the RequestHeader, which contain the Referer item.
7155

72-
而在 Vue 的脚手架中可以使用 Webpack 相关的 **DevProxy** 项设置相关的请求表头,从而起到伪造 Referer 以获取数据的效果。
73-
7456
In Vue-Cil project, the Webpack **DevProxy** item can be used to set up the relevant request headers, thus creating the effect of faking the Referer for data.
7557

76-
这样的例子在互联网上有许多,本人不在这里一一赘述,正好最近在写一个新的 [**Electron**](https://www.electronjs.org/) 项目,用到了伪造 Referer 的功能,在这里可以举例一番。
77-
7858
There are many such examples on the Internet, and I won't repeat them all here. Recently, I was writing a new [**Electron**](https://www.electronjs.org/) project, which used the function of forging Referer. Here I can give some examples.
7959

8060
```javascript
@@ -90,8 +70,6 @@ const {session} = remote
9070
}
9171
```
9272

93-
在给出的实例化的对象中,所有的函数方法都会返回 [**axios**](https://www.axios-http.cn/) 对象。所以,你可以从函数的 **then** 回调中获取数据,例如:
94-
9573
In the given instantiated object, all function methods return [**axios**](https://www.axios-http.cn/) objects. So, you can get data from the function's **then** callback, for example:
9674

9775
```javascript
@@ -116,29 +94,21 @@ const { session } = remote,
11694
}
11795
```
11896

119-
相关的文档请移步相关技术或者自行查询。
120-
12197
For related documents, refer to related technologies or query them by yourself.
12298

123-
## 接口详情
124-
125-
所有接口在文档更新时均已通过测试。
99+
## Interface Detail
126100

127101
All interfaces were tested when the document was updated.
128102

129-
下文表格中提到的 Cookie 项,是指用户在登录力扣官方网站生成的 **LEETCODE_SESSION****x-csrftoken** 项,获取方法:使用浏览器登录成功后,打开开发人员工具中的网络一项,寻找成功状态的 graphql 请求,在请求信息中可以找到这两项的值。
130-
131103
The Cookie items mentioned in the table below refer to the **LEETCODE_SESSION** and **x-csrftoken** items generated when users log in to the official website of Leetcode. The method of obtaining them is as follows: After logging in successfully using the browser, open the network item in the developer tool and look for the graphQL request in the successful state. The values of the two items can be found in the request information.
132104

133-
### 用户状态
105+
### User Status
134106

135107
```javascript
136108
const $leetcode = new $Leetcode()
137109
;$leetcode.getUserStatus() // with cookie [LEETCODE_SESSION,x-csrftoken]
138110
```
139111

140-
请求数据项:
141-
142112
Requested data item:
143113

144114
| Item | Value |
@@ -148,8 +118,6 @@ Requested data item:
148118
| URL | `https://leetcode-cn.com/graphql` |
149119
| Require | Cookie Item [LEETCODE_SESSION, x-csrftoken] |
150120

151-
获取到的数据项:
152-
153121
Obtained data items:
154122

155123
| Key | Value | Describe |

README.zh.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Leetcode-Api
2+
3+
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/NeserCode/Leetcode-Api"> <img title="GitHub download" src="https://img.shields.io/github/downloads/NeserCode/Leetcode-Api/total"/>
4+
5+
[English Document](README.md)
6+
7+
关于 力扣 的网络请求接口。
8+
9+
## 目录
10+
11+
* [免责声明](#免责声明)
12+
* [项目总览](#项目总览)
13+
* [接口相关](#接口相关)
14+
* [接口详情](#接口详情)
15+
16+
## 免责声明
17+
18+
**本项目仅供学习交流参考,在任意情况下均不得用于任何商业用途。如若违反声明,后果自负,本人不承担任何责任。**
19+
20+
## 项目总览
21+
22+
本项目提供的是 **力扣** 官方网站的 **WebApi** 集合,提出的 Api 均已试验成功。
23+
24+
本项目仅包含一个入口 Javascript 文件,将本文档中提出的 WebApi 全部封装到 $Leetcode 类中。
25+
26+
所以,想使用 Leetcode-Api,你只需:
27+
28+
```javascript
29+
import $Leetcode from "Leetcode-Api"
30+
31+
const variable = new $Leetcode()
32+
```
33+
34+
然后通过上例中的 variable 使用 Leetcode-Api 中的函数方法。
35+
36+
## 接口相关
37+
38+
力扣 官网使用了 [**GraphQL**](https://graphql.org/) 技术来获取数据,即 **按需分配数据** 的模式。同时,全部网络请求需要附着 Cookie 访问,部分网络请求需要确认 **Referer** 是否属于力扣域名之下。请使用这些 WebApi 之前制定好相关解决方案。
39+
40+
### 部分解决方案
41+
42+
在传统的 HTML5 项目中,如:
43+
44+
```html
45+
|-images
46+
|--images01.jpg
47+
|-js
48+
|--example.js
49+
|-style
50+
|--style.css
51+
|-index.html
52+
```
53+
54+
这种项目中,由于伪造 Referer 带来的不安全和危险,一般不会允许用户通过 Javascript 脚本去修改 HttpRequest 的表头中的几项,也就是 RequestHeader 中的几项,其中就包含有 Referer 项。
55+
56+
而在 Vue 的脚手架中可以使用 Webpack 相关的 **DevProxy** 项设置相关的请求表头,从而起到伪造 Referer 以获取数据的效果。
57+
58+
这样的例子在互联网上有许多,本人不在这里一一赘述,正好最近在写一个新的 [**Electron**](https://www.electronjs.org/) 项目,用到了伪造 Referer 的功能,在这里可以举例一番。
59+
60+
```javascript
61+
import { remote } from "electron"
62+
const {session} = remote
63+
64+
;function someFn(questionSlug){
65+
session.defaultSession.webRequest.onBeforeSendHeaders({ urls: ['https://leetcode-cn.com/problems/*'] }, (details, callback) => {
66+
details.requestHeaders['Referer'] = `https://leetcode-cn.com/problems/${questionSlug}/submissions/`
67+
callback({ cancel: false, requestHeaders: details.requestHeaders })
68+
})
69+
//...
70+
}
71+
```
72+
73+
在给出的实例化的对象中,所有的函数方法都会返回 [**axios**](https://www.axios-http.cn/) 对象。所以,你可以从函数的 **then** 回调中获取数据,例如:
74+
75+
```javascript
76+
import { remote } from "electron"
77+
const { session } = remote,
78+
$leetcode = new $Leetcode(),
79+
leetcodeUserStatus = null
80+
81+
;function someFn(questionSlug){
82+
session.defaultSession.webRequest.onBeforeSendHeaders({ urls: ['https://leetcode-cn.com/problems/*'] }, (details, callback) => {
83+
details.requestHeaders['Referer'] = `https://leetcode-cn.com/problems/${questionSlug}/submissions/`
84+
callback({ cancel: false, requestHeaders: details.requestHeaders })
85+
})
86+
$leetcode.getUserStatus().then((response)=>{
87+
if(response.status == 200){
88+
const { userStatus } = response.data.data
89+
// leetcodeUserStatus = userStatus
90+
}
91+
else
92+
console.log(response)
93+
})
94+
}
95+
```
96+
97+
相关的文档请移步相关技术或者自行查询。
98+
99+
## 接口详情
100+
101+
所有接口在文档更新时均已通过测试。
102+
103+
下文表格中提到的 Cookie 项,是指用户在登录力扣官方网站生成的 **LEETCODE_SESSION****x-csrftoken** 项,获取方法:使用浏览器登录成功后,打开开发人员工具中的网络一项,寻找成功状态的 graphql 请求,在请求信息中可以找到这两项的值。
104+
105+
### 用户状态
106+
107+
```javascript
108+
const $leetcode = new $Leetcode()
109+
;$leetcode.getUserStatus() // with cookie [LEETCODE_SESSION,x-csrftoken]
110+
```
111+
112+
请求数据项:
113+
114+
| Item | Value |
115+
| :--------: | :-----------------------------------------: |
116+
| Type | POST |
117+
| Parameters | NULL |
118+
| URL | `https://leetcode-cn.com/graphql` |
119+
| Require | Cookie Item [LEETCODE_SESSION, x-csrftoken] |
120+
121+
获取到的数据项:
122+
123+
| Key | Value | Describe |
124+
| :-------------------------: | :-----: | :--------------- |
125+
| commonNojPermissionTypes | Array | 未知 |
126+
| jobsMyCompany | Object | 用户公司 |
127+
| userStatus | Object | 用户状态 |
128+
| userStatus.avatar | String | 用户头像 |
129+
| userStatus.isAdmin | Boolean | 是否是管理员 |
130+
| userStatus.isPhoneVerified | Boolean | 是否通过手机验证 |
131+
| userStatus.isPremium | Boolean | 未知 |
132+
| userStatus.isSignedIn | Boolean | 是否登录 |
133+
| userStatus.isSuperuser | Boolean | 是否是VIP |
134+
| userStatus.isTranslator | Boolean | 是否是翻译 |
135+
| userStatus.isVerified | Boolean | 是否通过身份验证 |
136+
| userStatus.premiumExpiredAt | Number | 未知 |
137+
| userStatus.realName | String | 用户昵称 |
138+
| userStatus.useTranslation | Boolean | 是否使用翻译 |
139+
| userStatus.userSlug | String | 用户标签 |
140+
| userStatus.username | String | 用户名 |
141+

0 commit comments

Comments
 (0)