-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
370 lines (305 loc) · 11.9 KB
/
test.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import time
import re
from loguru import logger
from kivy.lang import Builder
from kivymd.material_resources import dp
from kivymd.app import MDApp
from kivy.core.text import LabelBase
from kivy.properties import ObjectProperty, StringProperty
from kivymd.uix.navigationbar import MDNavigationBar, MDNavigationItem
from kivymd.uix.screen import MDScreen
from kivymd.uix.snackbar.snackbar import MDSnackbar, MDSnackbarButtonContainer, MDSnackbarCloseButton ,MDSnackbarActionButton, MDSnackbarText, MDSnackbarSupportingText, MDSnackbarActionButtonText
from kivymd.uix.list.list import MDListItem, MDListItemSupportingText, MDListItemHeadlineText
from kivymd.uix.imagelist.imagelist import MDSmartTileImage,MDSmartTile
class BaseMDNavigationItem(MDNavigationItem):
icon = StringProperty()
text = StringProperty()
class BaseScreen(MDScreen):
image_size = StringProperty()
import core
LabelBase.register(name='Roboto',
fn_regular='SourceHanSansSC-Regular.ttf',
fn_bold='SourceHanSansSC-Bold.ttf')
KV = '''
<BaseMDNavigationItem>
MDNavigationItemIcon:
icon: root.icon
MDNavigationItemLabel:
text: root.text
<BaseScreen>
MDBoxLayout:
orientation: "vertical"
md_bg_color: self.theme_cls.backgroundColor
MDScreenManager:
id: screen_manager
BaseScreen:#主屏幕
name: "主界面"
MDTextField:
adaptive_size: True
multiline: True
id: urlinput
mode: "outlined"
pos_hint: {"center_x": .5, "center_y": .9}
allow_selection: True#允许选择
allow_copy: True#允许复制
MDTextFieldLeadingIcon:
icon: "magnify"
MDTextFieldHelperText:
text: "帮助文本"
mode: "persistent"
MDTextFieldTrailingIcon:
icon: "information"
MDButton:
style: "elevated"
pos_hint: {"center_x": .5, "center_y": .8}
on_release: app.test()
MDButtonIcon:
icon: "plus"
MDButtonText:
text: "开始解析"
BaseScreen:#文章详情屏幕
name: "Article Screen"
MDBoxLayout:
orientation: "vertical"
MDTopAppBar:
type: "small"
size_hint_x: .8
pos_hint: {"center_x": .5}
MDTopAppBarLeadingButtonContainer:
MDActionTopAppBarButton:
icon: "arrow-left"
on_release: app.on_back()#返回主屏幕
MDTopAppBarTitle:
text: "详情"
pos_hint: {"center_x": .5}
MDTopAppBarTrailingButtonContainer:
MDActionTopAppBarButton:
icon: "download"
on_release: app.on_download()
MDScrollView:#滚动视图
do_scroll_x: False#不允许x轴滚动
do_scroll_y: True#允许y轴滚动
MDBoxLayout:
adaptive_height: True#适应高度
id: article
orientation: "vertical"
MDListItem:
AsyncImage: # MDListItemLeadingAvatar
id: article_user_img
icon: "account"
MDListItemHeadlineText:
id: article_title
text: 'article_user_name'
MDListItemSupportingText:
id: article_user_name
text: 'article_time'
MDBoxLayout:
adaptive_height: True
orientation: "vertical"
MDLabel:
adaptive_height: True#适应高度
id: article_content
text: "article_content"
MDBoxLayout:
adaptive_height: True
orientation: "vertical"
FitImage:
orientation: "vertical"
adaptive_height: True#适应高度
pos_hint: {"top": 1}
#pos_hint: {"top": 1}
#size: self.texture_size
fit_mode: "contain"
source: "2.jpg"
BaseScreen:#作者详情屏幕
name: "Author Screen"
MDScrollView:
do_scroll_x: False#不允许x轴滚动
do_scroll_y: True#允许y轴滚动
MDBoxLayout:
id: author_info
orientation: "vertical"
BaseScreen:
name: "任务界面"
MDScrollView:#滚动视图
do_scroll_x: False#不允许x轴滚动
do_scroll_y: True#允许y轴滚动
MDBoxLayout:
id: local_articles_list
orientation: "vertical"
BaseScreen:#设置屏幕
name: "设置界面"
MDTopAppBar:
type: "small"
size_hint_x: .8
pos_hint: {"center_x": .5, "center_y": .5}
MDTopAppBarTitle:
text: "设置"
pos_hint: {"center_x": .5}
MDNavigationBar:
on_switch_tabs: app.on_switch_tabs(*args)
BaseMDNavigationItem
icon: "gmail"
text: "主界面"
active: True
#BaseMDNavigationItem
#icon: "article"
#text: "Article Screen"
BaseMDNavigationItem
icon: "progress-clock"
text: "任务界面"
BaseMDNavigationItem
text: "设置界面"
MDLinearProgressIndicator:
type: "determinate"
id: progress
size_hint_x: .5
size_hint_y: None
height: dp(5)#设置进度条厚度
value: 0
max: 100
radius: self.height / 2#用来给进度条添加圆角
pos_hint: {'center_x': .5, 'center_y': .4}
'''
#提取页面文章列表
def lofter_post_list(url):
page = 0
post = []
while 1==1:
page += 1
if page == 1:
post_url = url
else:
post_url = url+'?page='+str(page)
data = core.gets(post_url)
if data == 'null':
return 'null'
kk = re.compile('"(https://.*?\.lofter\.com/post/.*?_.*?)"')
post_list = kk.findall(data)
logger.debug('正在解析第'+str(page)+'页')
#out.toast('正在解析第'+str(page)+'页',position='left', color='info', duration=1)#弹出提示
post_list = core.dic(post_list)
if len(core.dic(post + post_list)) - len(post) == 0:
break
post = core.dic(post + post_list)
logger.debug('本页提取'+str(len(post_list))+'个文章')
logger.info('已提取'+str(len(post))+'个文章')
#out.toast('已提取'+str(len(post))+'个文章',position='left', color='info', duration=1)#弹出提示
print('共提取'+str(len(post))+'个文章')
#out.toast('已提取'+str(len(post))+'个文章',position='left', color='success', duration=0)#弹出提示
return post
def lofter_print(self,answer):
'''
打印文章信息
'''
logger.debug('作者 "'+answer['writer']['blogname']+'"')
self.root.ids.article_user_name.text = answer['writer']['blogname']
self.root.ids.article_user_img.source = answer['writer']['img']
self.root.ids.article_title.text = answer['title']
self.root.ids.article_content.text = answer['txt']
logger.debug('文章覆盖详情')
return
self.root.ids.article_img.clear_widgets()#清除旧显示图片内容
if len(answer['img']) != 0:
for a in answer['img']:#遍历添加图片
self.root.ids.article_img.add_widget(
MDSmartTile(
MDSmartTileImage(
source=a,
)
)
)
def start_url(url):
if 'lofter' in url:#如果为lofter地址
if '/post/' not in url:#尝试批量获取
list = lofter_post_list(url)
print(list)
#lofter_list_print(list)#显示列表
#清除旧显示内容
#out.clear('info')
return
answer = core.lofter_request_info(url)
if answer == 'error':
logger.warning('解析失败')
return
#显示内容
#lofter_print(answer)
#清除旧显示内容
#out.clear('info')
return answer
def snakebar(self,msg):
'''
显示提示消息
'''
MDSnackbar(
MDSnackbarSupportingText(
text=msg,
),
y = dp(15),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.5,
background_color=self.theme_cls.onPrimaryContainerColor,
).open()
article_data = {}#全局变量
#刷新本地文章列表
def refresh_local_articles(self):
def add_list(self,dir_name):
self.root.ids.local_articles_list.add_widget(
MDListItem(
MDListItemHeadlineText(
text=str(dir_name),
),
MDListItemSupportingText(
text="Supporting text",
),
id=i.stem
)
)
local_articles = core.get_local_articles('.\Download')
b = []
for a in self.root.ids.local_articles_list.children:
b.append(a.id)
for i in local_articles:#检查本地文章列表在不在页面中
#检查页面中有无文章
#检查是否存在重复文章
if not i.stem in b:
add_list(self,i.stem)
logger.info('本地列表添加文章:'+i.stem)
return
class Main(MDApp):
urlinput = ObjectProperty(None)#url输入框
article = ObjectProperty(None)#文章显示小部件
article_user_name = ObjectProperty(None)#文章作者名称
article_user_title = ObjectProperty(None)#文章标题
article_user_img = ObjectProperty(None)#文章作者头像
article_content = ObjectProperty(None)#文章内容
local_articles_list = ObjectProperty(None)#本地文章列表
#article_img = ObjectProperty(None)#文章图片
def test(self):
global article_data
answer = start_url(self.root.ids.urlinput.text)
lofter_print(self,answer)
article_data = answer
self.root.ids.screen_manager.current = 'Article Screen'
def on_download(self):#下载文章
global article_data
core.down(article_data,'F:\Lofter-Saver\Download')
logger.info('"'+article_data['title']+'" 下载成功')
snakebar(self,'"'+article_data['title']+'" 下载成功')
def on_back(self):#返回主屏幕
self.root.ids.screen_manager.current = 'Main Screen'
def on_switch_tabs(
self,
bar: MDNavigationBar,
item: MDNavigationItem,
item_icon: str,
item_text: str,
):#切换屏幕
self.root.ids.screen_manager.current = item_text
if item_text == '任务界面':
refresh_local_articles(self)#刷新本地列表
def build(self):
self.theme_cls.primary_palette = "Green"
return Builder.load_string(KV)
Main().run()