Skip to content

Commit

Permalink
optimize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
prcwcy committed Oct 30, 2022
1 parent fbcdfbf commit 867ab11
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 97 deletions.
Binary file added README.assets/main.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added README.assets/课程ID.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
## SJTU Canvas 视频 (批量) 下载器

本项目使用Github Action自动发布, 保证可执行文件的安全性.
本项目使用GitHub Actions自动发布, 保证可执行文件的安全性.

### 主界面展示

![main](README.assets/main.png)

### 使用说明

课程ID:

![课程ID](README.assets/课程ID.PNG)

### 非Windows用户使用方法

Expand Down
83 changes: 40 additions & 43 deletions sjtu_canvas_video_main_frame.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
from sjtu_login import login_using_cookies
from sjtu_login_frame import LoginFrame
from sjtu_login_alternative_frame import LoginAlternativeFrame
from sjtu_qr_code_login_frame import QRCodeLoginFrame
from sjtu_canvas_video_picker_frame import SinglePickerFrame, MultiplePickerFrame
from sjtu_canvas_video_helper import create_window
Expand All @@ -19,7 +19,6 @@ def __init__(self, master=None):
"https://courses.sjtu.edu.cn/app/oauth/2.0/login?login_type=outer",
"https://oc.sjtu.edu.cn/login/openid_connect"
]
self.url_idx = 0

self.grid(sticky=tk.N+tk.S+tk.W+tk.E)

Expand All @@ -41,19 +40,13 @@ def __init__(self, master=None):
command=self.popup_login,
text="登录jAccount"
)
self.login_button.grid(column=0, row=num_row)
self.login_button.grid(column=1, row=num_row)
self.qr_code_login_button = tk.Button(
self,
command=self.popup_qr_code_login,
text="登录jAccount (二维码)"
)
self.qr_code_login_button.grid(column=1, row=num_row)
# self.login_alternative_button = tk.Button(
# self,
# command=self.popup_login_alternative,
# text="输入JSESSIONID (开发者入口)"
# )
# self.login_alternative_button.grid(column=2, row=num_row, columnspan=2)
self.qr_code_login_button.grid(column=2, row=num_row)
num_row += 1

self.import_label = tk.Label(
Expand Down Expand Up @@ -113,7 +106,7 @@ def __init__(self, master=None):
state=tk.DISABLED
)
self.course_id_entry.grid(
column=1, row=num_row, columnspan=2, sticky=tk.W+tk.E
column=1, row=num_row
)
self.course_id_checkbutton_var = tk.IntVar(
value=False
Expand All @@ -125,6 +118,14 @@ def __init__(self, master=None):
text="使用课程ID"
)
self.course_id_checkbutton.grid(
column=2, row=num_row
)
self.course_id_update_button = tk.Button(
self,
command=self.update_course_id,
text="更新课程ID"
)
self.course_id_update_button.grid(
column=3, row=num_row
)
num_row += 1
Expand All @@ -139,12 +140,12 @@ def __init__(self, master=None):
self.all_courses = []
self.refresh_status_label()

self.cookies = None

def course_id_checkbutton_changed(self):
if self.course_id_checkbutton_var.get():
self.url_idx = 1
self.course_id_entry.config(state=tk.NORMAL)
else:
self.url_idx = 0
self.course_id_entry.config(state=tk.DISABLED)

def refresh_status_label(self):
Expand All @@ -154,31 +155,30 @@ def refresh_status_label(self):
text=f"当前已读取到: {num_subject}个科目, 共{num_course}讲"
)

def refresh_all_courses(self, cookies):
def update_cookies_and_refresh_all_courses(self, cookies):
login_using_cookies(self.urls[1], cookies)
self.cookies = cookies
self.refresh_all_courses()

def refresh_all_courses(self):
if self.course_id_checkbutton_var.get():
if type(cookies) == dict:
self.all_courses = get_real_canvas_videos_using_sub_cookies(
cookies
)
else:
course_id = self.course_id_var.get()
if not course_id:
tkinter.messagebox.showerror("错误", "请输入课程ID")
return
self.all_courses = get_real_canvas_videos(course_id, cookies)
course_id = self.course_id_var.get()
if not course_id:
tkinter.messagebox.showerror("错误", "请输入课程ID")
return
self.all_courses = get_real_canvas_videos(course_id, self.cookies)
else:
self.all_courses = get_all_courses(cookies)
self.all_courses = get_all_courses(self.cookies)
self.refresh_status_label()

def popup_login(self):
if self.course_id_checkbutton_var.get() and not self.course_id_var.get():
tkinter.messagebox.showerror("错误", "请输入课程ID")
return
window = create_window(self.master)
window.geometry("400x200")
LoginFrame(
self.urls[self.url_idx],
lambda cookies: self.refresh_all_courses(cookies),
self.urls[0],
lambda cookies: self.update_cookies_and_refresh_all_courses(
cookies
),
window
)

Expand Down Expand Up @@ -219,22 +219,19 @@ def popup_download_multiple(self):
window.geometry("400x200")
MultiplePickerFrame(self.all_courses, window)

def popup_login_alternative(self):
window = create_window(self.master)
window.geometry("400x100")
LoginAlternativeFrame(
lambda cookies: self.refresh_all_courses(cookies),
window
)

def popup_qr_code_login(self):
if self.course_id_checkbutton_var.get() and not self.course_id_var.get():
tkinter.messagebox.showerror("错误", "请输入课程ID")
return
window = create_window(self.master)
window.geometry("300x300")
QRCodeLoginFrame(
self.urls[self.url_idx],
lambda cookies: self.refresh_all_courses(cookies),
self.urls[0],
lambda cookies: self.update_cookies_and_refresh_all_courses(
cookies
),
window
)

def update_course_id(self):
if self.cookies == None:
tkinter.messagebox.showerror("错误", "请登录")
else:
self.refresh_all_courses()
11 changes: 11 additions & 0 deletions sjtu_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ def login(username, password, uuid, captcha, params, cookies):
for i in r.history:
cookies.update(i.cookies)
return cookies


def login_using_cookies(url, cookies):
r = requests.get(
url,
headers={"accept-language": "zh-CN"},
cookies=cookies
)
cookies.update(r.cookies)
for i in r.history:
cookies.update(i.cookies)
53 changes: 0 additions & 53 deletions sjtu_login_alternative_frame.py

This file was deleted.

0 comments on commit 867ab11

Please sign in to comment.