-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved code & use requests.session (#14)
* improved code & use requests.session * remove headers comment
- Loading branch information
Showing
3 changed files
with
252 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
""" | ||
Copyright (C) 2023 Imken Luo <[email protected]> | ||
Copyright (C) 2023 Imken Luo <[email protected]> | ||
Licensed under GNU General Public License | ||
version 3 or above <https://www.gnu.org/licenses/>. | ||
This program is a part of Yangcong-tools. See `main.py` for more details. | ||
""" | ||
|
||
import json | ||
from random import randint | ||
import requests, json | ||
|
||
import requests | ||
|
||
AUTH_TOKEN = "" | ||
|
||
|
@@ -32,62 +34,59 @@ | |
"Referrer-Policy": "strict-origin-when-cross-origin", | ||
} | ||
|
||
session = requests.session() | ||
|
||
|
||
def login(username, password): | ||
global AUTH_TOKEN | ||
ctx = requests.post( | ||
url="https://school-api.yangcong345.com/public/login", | ||
ctx = session.post( | ||
"https://school-api.yangcong345.com/public/login", | ||
headers=headers, | ||
json={ | ||
"name": username, | ||
"password": password, | ||
}, | ||
) | ||
# print(password, 'endl') | ||
if ctx.status_code != 200: | ||
return None | ||
else: | ||
AUTH_TOKEN = ctx.headers.get("authorization") | ||
headers["authorization"] = AUTH_TOKEN | ||
return AUTH_TOKEN | ||
AUTH_TOKEN = ctx.headers.get("authorization") | ||
headers["authorization"] = AUTH_TOKEN | ||
session.headers = headers | ||
return AUTH_TOKEN | ||
|
||
|
||
def get_unfinished_homework(): | ||
if AUTH_TOKEN == "": | ||
return | ||
ctx = requests.get( | ||
url="https://school-api.yangcong345.com/student/ai-homework/all/unfinish", | ||
headers=headers, | ||
ctx = session.get( | ||
"https://school-api.yangcong345.com/student/ai-homework/all/unfinish", | ||
) | ||
return json.loads(ctx.content.decode("utf-8")) | ||
|
||
|
||
def get_expired_homework(): | ||
if AUTH_TOKEN == "": | ||
return | ||
ctx = requests.get( | ||
url="https://school-api.yangcong345.com/student/ai-homework/all/expired", | ||
headers=headers, | ||
ctx = session.get( | ||
"https://school-api.yangcong345.com/student/ai-homework/all/expired", | ||
) | ||
return json.loads(ctx.content.decode("utf-8")) | ||
|
||
|
||
def get_topic_detail(topic_id): | ||
if AUTH_TOKEN == "": | ||
return | ||
ctx = requests.get( | ||
url=f"https://school-api.yangcong345.com/course/topics/{topic_id}/detail-universal", | ||
headers=headers, | ||
ctx = session.get( | ||
f"https://school-api.yangcong345.com/course/topics/{topic_id}/detail-universal", | ||
) | ||
return json.loads(ctx.content.decode("utf-8")) | ||
|
||
|
||
def submit_video(topic_id, homework_id, video_id, duration): | ||
if AUTH_TOKEN == "": | ||
return | ||
requests.put( | ||
url="https://school-api.yangcong345.com/student/previews/video", | ||
headers=headers, | ||
session.put( | ||
"https://school-api.yangcong345.com/student/previews/video", | ||
json={ | ||
"topicId": topic_id, | ||
"videoId": video_id, | ||
|
@@ -99,9 +98,8 @@ def submit_video(topic_id, homework_id, video_id, duration): | |
"videoRecords": [1] * duration, | ||
}, | ||
) | ||
requests.post( | ||
url="https://api-v5-0.yangcong345.com/learn/video", | ||
headers=headers, | ||
session.post( | ||
"https://api-v5-0.yangcong345.com/learn/video", | ||
json={ | ||
"duration": duration + 3, | ||
"timepoint": duration, | ||
|
@@ -117,9 +115,8 @@ def submit_video(topic_id, homework_id, video_id, duration): | |
def submit_video_problem(problem_id, homework_id, topic_id, answers): | ||
if AUTH_TOKEN == "": | ||
return | ||
return requests.post( | ||
url="https://api-v5-0.yangcong345.com/learn/practice", | ||
headers=headers, | ||
return session.post( | ||
"https://api-v5-0.yangcong345.com/learn/practice", | ||
json={ | ||
"problemId": problem_id, | ||
"topicId": topic_id, | ||
|
@@ -137,32 +134,28 @@ def get_task_problem(homework_id): | |
if AUTH_TOKEN == "": | ||
return | ||
uid = json.loads( | ||
requests.get( | ||
url="https://school-api.yangcong345.com/user-auths/order/auth", | ||
headers=headers, | ||
session.get( | ||
"https://school-api.yangcong345.com/user-auths/order/auth", | ||
).content.decode("utf-8") | ||
)["userId"] | ||
detail_id = json.loads( | ||
requests.post( | ||
url=f"https://school-api.yangcong345.com/student/tasks/{homework_id}/task-detail", | ||
headers=headers, | ||
session.post( | ||
f"https://school-api.yangcong345.com/student/tasks/{homework_id}/task-detail", | ||
json={"userId": uid}, | ||
).content.decode("utf-8") | ||
)["taskDetailId"] | ||
return json.loads( | ||
requests.get( | ||
url=f"https://school-api.yangcong345.com/student/task-detail/{detail_id}/problems", | ||
headers=headers, | ||
session.get( | ||
f"https://school-api.yangcong345.com/student/task-detail/{detail_id}/problems", | ||
).content.decode("utf-8") | ||
) | ||
|
||
|
||
def commit_problem_progress(group_id, is_finished: bool, problems): | ||
if AUTH_TOKEN == "": | ||
return | ||
return requests.put( | ||
url=f"https://school-api.yangcong345.com/student/group-detail/{group_id}/problems-progress", | ||
headers=headers, | ||
return session.put( | ||
f"https://school-api.yangcong345.com/student/group-detail/{group_id}/problems-progress", | ||
json={"isFinished": is_finished, "problems": problems}, | ||
) | ||
|
||
|
@@ -171,9 +164,8 @@ def get_practice_problems(homework_id): | |
if AUTH_TOKEN == "": | ||
return | ||
return json.loads( | ||
requests.get( | ||
url=f"https://school-api.yangcong345.com/student/practices/{homework_id}", | ||
headers=headers, | ||
session.get( | ||
f"https://school-api.yangcong345.com/student/practices/{homework_id}", | ||
).content.decode("utf-8") | ||
)["problems"] | ||
|
||
|
@@ -186,9 +178,8 @@ def get_practice_problems(homework_id): | |
def submit_practice_problem(homework_id, problems, state): | ||
if AUTH_TOKEN == "": | ||
return | ||
requests.put( | ||
url="https://school-api.yangcong345.com/student/practices", | ||
headers=headers, | ||
session.put( | ||
"https://school-api.yangcong345.com/student/practices", | ||
json={ | ||
"homeworkId": homework_id, | ||
"problems": problems, | ||
|
@@ -200,9 +191,8 @@ def submit_practice_problem(homework_id, problems, state): | |
def get_vacations(): | ||
if AUTH_TOKEN == "": | ||
return | ||
ctx = requests.get( | ||
url="https://school-api.yangcong345.com/vacation/student/homework/list", | ||
headers=headers, | ||
ctx = session.get( | ||
"https://school-api.yangcong345.com/vacation/student/homework/list", | ||
) | ||
ctx = ctx.content.decode("utf-8") | ||
return json.loads(ctx)["vacations"] | ||
|
@@ -211,10 +201,9 @@ def get_vacations(): | |
def get_vacation_details(vacation_id): | ||
if AUTH_TOKEN == "": | ||
return | ||
ctx = requests.get( | ||
url=f"https://school-api.yangcong345.com/vacation/student/homework/timeline/task/list" | ||
ctx = session.get( | ||
"https://school-api.yangcong345.com/vacation/student/homework/timeline/task/list" | ||
+ f"?measuringID={vacation_id}", | ||
headers=headers, | ||
) | ||
ctx = ctx.content.decode("utf-8") | ||
return json.loads(ctx) | ||
|
@@ -223,22 +212,18 @@ def get_vacation_details(vacation_id): | |
def get_vacation_video_detail(topic_id, task_id): | ||
if AUTH_TOKEN == "": | ||
return | ||
ctx = requests.get( | ||
url=( | ||
f"https://school-api.yangcong345.com/course/topics/{topic_id}/detail-video" | ||
+ f"?taskID=f{task_id}" | ||
), | ||
headers=headers, | ||
ctx = session.get( | ||
f"https://school-api.yangcong345.com/course/topics/{topic_id}/detail-video" | ||
+ f"?taskID=f{task_id}" | ||
) | ||
return json.loads(ctx.content.decode("utf-8")) | ||
|
||
|
||
def submit_vacation_video(topic_id, video_id, task_id, duration): | ||
if AUTH_TOKEN == "": | ||
return | ||
requests.put( | ||
url="https://school-api.yangcong345.com/vacation/student/homework/user-topic-video-record", | ||
headers=headers, | ||
session.put( | ||
"https://school-api.yangcong345.com/vacation/student/homework/user-topic-video-record", | ||
json={ | ||
"taskID": task_id, | ||
"videoID": video_id, | ||
|
@@ -255,9 +240,8 @@ def submit_vacation_practice( | |
): | ||
if AUTH_TOKEN == "": | ||
return | ||
requests.post( | ||
url="https://school-api.yangcong345.com/vacation/student/homework/user-topic-problem-record", | ||
headers=headers, | ||
session.post( | ||
"https://school-api.yangcong345.com/vacation/student/homework/user-topic-problem-record", | ||
json={ | ||
"taskID": task_id, | ||
"problemId": problem_id, | ||
|
@@ -275,12 +259,9 @@ def get_vacation_stage_problem(task_id, stage_id, subject_id, homework_id): | |
if AUTH_TOKEN == "": | ||
return | ||
return json.loads( | ||
requests.get( | ||
url=( | ||
"https://school-api.yangcong345.com/vacation/student/homework/stage-practise" | ||
+ f"?taskID={task_id}&stageId={stage_id}&subjectId={subject_id}&homeworkId={homework_id}" | ||
), | ||
headers=headers, | ||
session.get( | ||
"https://school-api.yangcong345.com/vacation/student/homework/stage-practise" | ||
+ f"?taskID={task_id}&stageId={stage_id}&subjectId={subject_id}&homeworkId={homework_id}" | ||
).content.decode("utf-8") | ||
)["problems"] | ||
|
||
|
@@ -297,9 +278,8 @@ def submit_vacation_stage_problem( | |
): | ||
if AUTH_TOKEN == "": | ||
return | ||
requests.post( | ||
url="https://school-api.yangcong345.com/vacation/student/homework/user-stage-problem-record", | ||
headers=headers, | ||
session.post( | ||
"https://school-api.yangcong345.com/vacation/student/homework/user-stage-problem-record", | ||
json={ | ||
"taskID": str(task_id), | ||
"stageId": str(stage_id), | ||
|
@@ -330,9 +310,8 @@ def submit_vacation_stage_problem( | |
def finalsubmit_vacation_stage_problem(task_id, problem_records): | ||
if AUTH_TOKEN == "": | ||
return | ||
requests.post( | ||
url="https://school-api.yangcong345.com/vacation/student/homework/user-stage-problem-record", | ||
headers=headers, | ||
session.post( | ||
"https://school-api.yangcong345.com/vacation/student/homework/user-stage-problem-record", | ||
json={ | ||
"taskID": task_id, | ||
"finished": True, | ||
|
Oops, something went wrong.