[App Sharing] 【Python】中小学电子课本解析下载工具 Resolved
Tofloor
poster avatar
lizipeng0013
deepin
2023-08-24 02:07
Author

图片.png

已打包程序:https://shengmingsu.lanzoue.com/iEBSw167bf3e

原代码(Windows):https://github.com/happycola233/tchMaterial-parser

修改的代码(支持Linux):

# 版本:1.2

# 导入相关库
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import threading
import requests
import os
import json
import pyperclip

# 定义解析URL的函数
def analyze(url):
    try:
        # 提取URL中的contentId
        for q in url[url.find('?') + 1:].split('&'):
            if q.split('=')[0] == 'contentId':
                contentId = q.split('=')[1]
                break

        response = requests.get(f"https://s-file-1.ykt.cbern.com.cn/zxx/ndrv2/resources/tch_material/details/{contentId}.json")
        data = json.loads(response.text)
        for item in list(data["ti_items"]):
            if item["lc_ti_format"] == "pdf": # 找到存有PDF链接列表的项
                pdf_url = item["ti_storages"][0].replace("-private","") # 获取并构建PDF的URL
                break

        return pdf_url, contentId
    except:
        return None, None # 如果解析失败,返回None

# 获取默认文件名
def get_default_filename(contentId):
    response = requests.get(f"https://s-file-1.ykt.cbern.com.cn/zxx/ndrv2/resources/tch_material/details/{contentId}.json")
    try:
        data = json.loads(response.text)
        return data["title"] # 返回教材标题
    except:
        return None

# 下载文件的函数
def download_file(url, save_path):
    response = requests.get(url, stream=True)
    with open(save_path, 'wb') as file:
        for chunk in response.iter_content(chunk_size=8192): # 分块下载
            file.write(chunk)
    messagebox.showinfo("完成", f"文件已下载到:{save_path}") # 显示完成对话框

# 解析并复制链接的函数
def analyze_and_copy():
    urls = [line.strip() for line in url_text.get("1.0", tk.END).splitlines() if line.strip()] # 获取所有非空行
    pdf_links = []
    failed_links = []

    for url in urls:
        pdf_url, _ = analyze(url)
        if not pdf_url:
            failed_links.append(url) # 添加到失败链接
            continue
        pdf_links.append(pdf_url)

    if failed_links:
        failed_msg = "以下链接无法解析:\n" + '\n'.join(failed_links)
        messagebox.showwarning("警告", failed_msg) # 显示警告对话框

    if pdf_links:
        pyperclip.copy("\n".join(pdf_links)) # 将链接复制到剪贴板
        messagebox.showinfo("提示", "PDF链接已复制到剪贴板")

# 下载PDF文件的函数
def download():
    urls = [line.strip() for line in url_text.get("1.0", tk.END).splitlines() if line.strip()] # 获取所有非空行
    failed_links = []

    if len(urls) > 1:
        messagebox.showinfo("提示", "您选择了多个链接,将在选定的文件夹中使用教材名称作为文件名进行下载。")
        dir_path = filedialog.askdirectory() # 选择文件夹
        if not dir_path:
            return
    else:
        dir_path = None

    for url in urls:
        pdf_url, contentId = analyze(url)
        if not pdf_url:
            failed_links.append(url) # 添加到失败链接
            continue

        if dir_path:
            default_filename = get_default_filename(contentId) or "download"
            save_path = os.path.join(dir_path, f"{default_filename}.pdf") # 构造完整路径
        else:
            default_filename = get_default_filename(contentId) or "download"
            save_path = filedialog.asksaveasfilename(defaultextension=".pdf", filetypes=[("PDF files", "*.pdf"), ("All files", "*.*")], initialfile=default_filename) # 选择保存路径
            if not save_path:
                return

        threading.Thread(target=download_file, args=(pdf_url, save_path)).start() # 开始下载

    if failed_links:
        failed_msg = "以下链接无法解析:\n" + '\n'.join(failed_links)
        messagebox.showwarning("警告", failed_msg) # 显示警告对话框

# GUI
root = tk.Tk()

root.title("国家中小学智慧教育平台 电子课本解析") # 设置窗口标题

container_frame = ttk.Frame(root)
container_frame.pack(anchor='center',expand='yes', padx=20, pady=20) # 容器的中心位置放置,允许组件在容器中扩展,水平外边距40,垂直外边距40

title_label = ttk.Label(container_frame, text="国家中小学智慧教育平台 电子课本解析", font=("微软雅黑", 16, "bold")) # 添加标题标签
title_label.pack(pady=5) # 设置垂直外边距

description = '''请在下面的文本框中粘贴一个或多个课本原网址(支持批量每个URL一行)。
例如:
https://basic.smartedu.cn/tchMaterial/detail?contentType=
assets_document&contentId=b8e9a3fe-dae7-49c0-86cb-d146f88
3fd8e&catalogType=tchMaterial&subCatalog=tchMaterial
点击下载按钮后,程序会解析并下载所有PDF文件。'''
description_label = ttk.Label(container_frame, text=description, justify="left") # 添加描述标签
description_label.pack(pady=5) # 设置垂直外边距

url_text = tk.Text(container_frame, width=70, height=12) # 添加URL输入框,长度和宽度不使用缩放!!!
url_text.pack(padx=15, pady=15) # 设置水平外边距、垂直外边距

download_btn = ttk.Button(container_frame, text="下载", command=download) # 添加下载按钮
download_btn.pack(side="left", padx=40, pady=5, ipady=5) # 设置水平外边距、垂直外边距,设置按钮高度

copy_btn = ttk.Button(container_frame, text="解析并复制", command=analyze_and_copy) # 添加“解析并复制”按钮
copy_btn.pack(side="right", padx=40, pady=5, ipady=5) # 设置水平外边距、垂直外边距,设置按钮高度

root.mainloop() # 开始主循环

重要说明:因为是修改自Windows的,复制下载链接到剪切板存在小问题,如果需要使用此功能,请先安装xclip sudo apt install xclip


20240227更新2.0版已打包程序:https://shengmingsu.lanzouw.com/iVXt21pj1y0b

新版界面和功能更新,可在程序中选择电子书自动填入链接,进行批量下载:
1.png

Reply Favorite View the author
All Replies
y***6@126.com
deepin
2023-08-24 02:54
#1

这个资源好

Reply View the author
蒙笛
deepin
2023-08-24 03:14
#2

正用的到,感谢applaud

Reply View the author
阿尼樱奈奈
Moderator
2023-08-24 03:16
#3

like

Reply View the author
hanzn-zzx
deepin
2023-08-24 03:57
#4

like赞!(学生党狂喜)

Reply View the author
MMHMM
Moderator
2023-08-24 07:46
#5

感谢分享

kissing_heart

Reply View the author
babyfengfjx
Super Moderator
CQA
2023-08-24 17:59
#6

我能夸你很优秀么~

Reply View the author
晚秋(lateautumn)
Moderator
2023-08-24 23:13
#7

棒!正在下载kissing_heart

Reply View the author
秋胜春朝
deepin
2023-08-27 05:00
#8

like

Reply View the author
深圳市耀影科技有限公司
deepin
2023-10-22 09:55
#9

支持

Reply View the author
发面饼
deepin
2024-02-04 22:08
#10

Windows下怎么用?

Reply View the author
lizipeng0013
deepin
2024-02-05 00:26
#11
发面饼

Windows下怎么用?

Windows的原代码GitHub Releases有已打包程序下载,我发帖后到现在已更新到2.0版本,用法是一样的

Reply View the author
发面饼
deepin
2024-02-06 13:08
#12
lizipeng0013

Windows的原代码GitHub Releases有已打包程序下载,我发帖后到现在已更新到2.0版本,用法是一样的

谢谢,好不容易下载下来了。

Reply View the author