Skip to content

Commit

Permalink
improved code & use requests.session (#14)
Browse files Browse the repository at this point in the history
* improved code & use requests.session

* remove headers comment
  • Loading branch information
immccn123 authored Aug 13, 2023
1 parent ce9fd4b commit 8203d79
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 288 deletions.
129 changes: 54 additions & 75 deletions api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Copyright (C) 2023 Imken Luo <immccn123@outlook.com>
Copyright (C) 2023 Imken Luo <me@imken.moe>
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 = ""

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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},
)

Expand All @@ -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"]

Expand All @@ -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,
Expand All @@ -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"]
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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"]

Expand All @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 8203d79

Please sign in to comment.