-
Notifications
You must be signed in to change notification settings - Fork 50
/
ui.py
160 lines (140 loc) · 6.98 KB
/
ui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@Description: 用于GUI界面显示
@Date :2021/08/14
@Author :xhunmon
@Mail :xhunmon@gmail.com
"""
from tkinter import *
from tkinter.filedialog import (askdirectory)
from douyin.dy_download import DouYin
from downloader import Downloader
from kuaishou.ks_download import KuaiShou
from type_enum import PrintType
from utils import *
# from PIL import Image, ImageTk
class Ui(Frame):
def __init__(self, master=None):
global bg_color
bg_color = '#373434'
Frame.__init__(self, master, bg=bg_color)
self.ui_width = 0
self.pack(expand=YES, fill=BOTH)
self.window_init()
self.createWidgets()
def window_init(self):
self.master.title(
'欢迎使用-自媒体资源下载器' + Config.instance().get_version_name() + ',本程序仅用于学习交流!如有疑问请联系:xhunmon@gmail.com')
self.master.bg = bg_color
width, height = self.master.maxsize()
# self.master.geometry("{}x{}".format(width, height))
self.master.geometry("%dx%d+%d+%d" % (width / 2, height / 2, width / 4, height / 4))
self.ui_width = width / 2
def createWidgets(self):
# fm1
self.fm1 = Frame(self, bg=bg_color)
self.fm1.pack(fill='y', pady=10)
# window没有原生PIL 64位支持
# load = Image.open('res/logo.png')
# load.thumbnail((38, 38), Image.ANTIALIAS)
# initIamge = ImageTk.PhotoImage(load)
# self.panel = Label(self.fm1, image=initIamge, bg=bg_color)
# self.panel.image = initIamge
# self.panel.pack(side=LEFT, fill='y', padx=5)
self.titleLabel = Label(self.fm1, text="资源下载器", font=('微软雅黑', 32), fg="white", bg=bg_color)
self.titleLabel.pack(side=LEFT, fill='y')
# fm2
self.fm2 = Frame(self, bg=bg_color)
self.fm2.pack(side=TOP, fill="y")
self.fm2_right = Frame(self.fm2, bg=bg_color)
self.fm2_right.pack(side=RIGHT, padx=0, pady=10, expand=YES, fill='y')
self.fm2_left = Frame(self.fm2, bg=bg_color)
self.fm2_left.pack(side=LEFT, padx=15, pady=10, expand=YES, fill='x')
self.fm2_left_top = Frame(self.fm2_left, bg=bg_color)
self.fm2_left_bottom = Frame(self.fm2_left, bg=bg_color)
self.downloadBtn = Button(self.fm2_right, text='开始下载', fg="#ffffff", bg=bg_color,
font=('微软雅黑', 18), command=self.start_download)
self.downloadBtn.pack(side=RIGHT)
self.dirEntry = Entry(self.fm2_left_top, font=('微软雅黑', 14), width='72', fg='#ffffff', bg=bg_color, bd=1)
self.dirEntry.config(insertbackground='#ffffff')
# self.set_dir(os.path.dirname(os.path.realpath(sys.argv[0])))
self.dirBtn = Button(self.fm2_left_top, text='选择保存目录:', bg=bg_color, fg='#aaaaaa',
font=('微软雅黑', 12), width='10', command=self.save_dir)
self.dirBtn.pack(side=LEFT)
self.dirEntry.pack(side=LEFT, fill='y')
self.fm2_left_top.pack(side=TOP, fill='x')
self.urlEntry = Entry(self.fm2_left_bottom, font=('微软雅黑', 14), width='72', fg='#ffffff', bg=bg_color, bd=1)
self.urlEntry.config(insertbackground='#ffffff')
self.urlButton = Button(self.fm2_left_bottom, text='清空下载地址:', bg=bg_color, fg='#aaaaaa',
font=('微软雅黑', 12), width='10', command=self.download_url)
self.urlButton.pack(side=LEFT)
self.urlEntry.pack(side=LEFT, fill='y')
self.fm2_left_bottom.pack(side=TOP, pady=10, fill='x')
# fm3 任务数状态
self.fm3 = Frame(self, bg=bg_color, height=6)
self.fm3.pack(side=TOP, fill="x")
self.totalLabel = Label(self.fm3, width=10, text="预计总数:0", font=('微软雅黑', 12), fg="white", bg=bg_color)
self.totalLabel.pack(side=LEFT, fill='y', padx=20)
self.downloadingLabel = Label(self.fm3, width=10, text="正在下载:0", font=('微软雅黑', 12), fg="white", bg=bg_color)
self.downloadingLabel.pack(side=LEFT, fill='y', padx=20)
self.successLabel = Label(self.fm3, width=10, text="已完成:0", font=('微软雅黑', 12), fg="white", bg=bg_color)
self.successLabel.pack(side=LEFT, fill='y', padx=20)
self.failLabel = Label(self.fm3, width=10, text="已失败:0", font=('微软雅黑', 12), fg="white", bg=bg_color)
self.failLabel.pack(side=LEFT, fill='y', padx=20)
# fm4
self.fm4 = Frame(self, bg=bg_color)
self.fm4.pack(side=TOP, expand=YES, fill="both")
self.logLabel = Label(self.fm4, anchor='w', wraplength=self.ui_width - 40, text="", font=('微软雅黑', 12),
fg="white",
bg=bg_color)
self.logLabel.pack(side=TOP, fill='both', padx=20)
# 注册回调
Downloader.func_ui_print = self.func_ui_print
# 判断是否有网络
if Downloader.get_beijing_time() == 0:
self.output("获取数据异常,请检查您的网络!")
else:
Downloader.print_hint()
def save_dir(self):
path = askdirectory()
self.set_dir(path)
def set_dir(self, path):
self.dirEntry.delete(0, END)
self.dirEntry.insert(0, path)
def download_url(self):
ground_truth = ''
self.urlEntry.delete(0, END)
self.urlEntry.insert(0, ground_truth)
def output(self, txt):
self.logLabel.config(text=txt)
def func_ui_print(self, txt, print_type: PrintType = None):
if print_type == PrintType.log:
self.logLabel.config(text=txt)
elif print_type == PrintType.total:
self.totalLabel.config(text=txt)
elif print_type == PrintType.downloading:
self.downloadingLabel.config(text=txt)
elif print_type == PrintType.success:
self.successLabel.config(text=txt)
elif print_type == PrintType.failed:
self.failLabel.config(text=txt)
def start_download(self):
# 判断是否有网络
if Downloader.get_beijing_time() == 0:
self.output("获取数据异常,请检查您的网络!")
return
if Downloader.is_expired():
self.output("授权证书已到期,请联系客服!")
return
url = self.urlEntry.get()
path = self.dirEntry.get()
domain = get_domain(url)
if "kwaicdn" in domain or "kuaishou" in domain:
downloader: KuaiShou = KuaiShou()
# downloader.set_cookie()
else:
downloader: Downloader = DouYin()
downloader_t = threading.Thread(target=downloader.start, args=(url, path))
downloader_t.setDaemon(True) # 设置守护进程,避免界面卡死
downloader_t.start()