1111"""
1212import os
1313from pathlib import Path
14+ from time import time
1415
1516from PyQt5 .QtCore import QObject , QThread , QRunnable
1617from PyQt5 .QtGui import QLinearGradient , QColor
1718import requests
1819
1920from Utils .CommonUtil import AppLog , Signals
20- from Utils .Constants import DirThemes , UrlGetAllCategoriesV2 ,\
21- UrlGetAppsByCategory , DirImages
21+ from Utils .Constants import DirThemes , UrlGetAppsByCategory , DirWallpaper
2222
2323
2424__Author__ = "Irony"
@@ -154,63 +154,56 @@ def run(self):
154154 AppLog .info ('theme thread end' )
155155
156156
157- class GetAllCategoriesRunnable (QRunnable ):
158-
159- def __init__ (self , * args , ** kwargs ):
160- super (GetAllCategoriesRunnable , self ).__init__ (* args , ** kwargs )
161- self .setAutoDelete (True )
162-
163- def run (self ):
164- categories = []
165- try :
166- req = requests .get (UrlGetAllCategoriesV2 )
167- content = req .json ()
168- categories = content .get ('data' , [])
169- AppLog .debug ('errmsg: %s' , content .get ('errmsg' , '' ))
170- AppLog .debug ('consume: %s' , content .get ('consume' , '' ))
171- AppLog .debug ('total: %s' , content .get ('total' , '' ))
172- except Exception as e :
173- AppLog .exception (e )
174- Signals .getCategoriesFinished .emit (categories )
175-
176-
177157class GetAllCategoryRunnable (QRunnable ):
178158
179- def __init__ (self , cid , widget , * args , ** kwargs ):
159+ def __init__ (self , category , widget , * args , ** kwargs ):
180160 super (GetAllCategoryRunnable , self ).__init__ (* args , ** kwargs )
181161 self .setAutoDelete (True )
182- self .cid = cid
162+ self .category = category
183163 self .widget = widget
184164
185- def download (self , index , url ):
165+ def download (self , index , title , url ):
186166 try :
187- dirPath = os .path .join (DirImages , self .cid )
167+ dirPath = os .path .join (DirWallpaper , self .category )
188168 os .makedirs (dirPath , exist_ok = True )
169+ path = os .path .join (dirPath , os .path .basename (url ))
170+ if os .path .exists (path ) and os .path .isfile (path ):
171+ Signals .pictureItemAdded .emit (
172+ self .widget , index , title , path )
173+ return
189174 req = requests .get (url , headers = Headers )
190175 if req .status_code == 200 :
191- path = os .path .join (dirPath , os .path .basename (url ))
192176 with open (path , 'wb' ) as fp :
193177 fp .write (req .content )
194- self .pictureDownloadFinished .emit (self , index , path )
178+ Signals .pictureItemAdded .emit (
179+ self .widget , index , title , path )
195180 except Exception as e :
196181 AppLog .exception (e )
197182
198183 def run (self ):
199- datas = []
200184 try :
201185 req = requests .get (UrlGetAppsByCategory .format (
202- cid = self .cid , start = 0 , count = 200 ))
186+ category = self .category , pageno = 1 , count = 20 , time = time () ))
203187 content = req .json ()
204- datas = content .get ('data' , [])
205- AppLog .debug ('errmsg: %s' , content .get ('errmsg' , '' ))
206- AppLog .debug ('consume: %s' , content .get ('consume' , '' ))
207- AppLog .debug ('total: %s' , content .get ('total' , '' ))
208- # 加载前20个
209- for i , item in enumerate (datas [:20 ]):
210- url = item .get ('url' , None )
188+ errno = content .get ('errno' , 0 )
189+ AppLog .debug ('errno: %s' , errno )
190+ AppLog .debug ('msg: %s' , content .get ('msg' , '' ))
191+ if errno != 0 :
192+ return
193+
194+ content = content .get ('data' , {})
195+ AppLog .debug ('total_count: %s' , content .get ('total_count' , '' ))
196+ AppLog .debug ('total_page: %s' , content .get ('total_page' , '' ))
197+ items = content .get ('list' , [])
198+
199+ for i , item in enumerate (items ):
200+ title = item .get ('title' , '' )
201+ url = item .get ('image' , None )
211202 if not url :
212203 continue
213- self .download (i , url )
204+ self .download (i , title , url )
205+ QThread .msleep (200 )
206+ QThread .yieldCurrentThread ()
214207 except Exception as e :
215208 AppLog .exception (e )
216- Signals .getCategoryFinished .emit (self .widget , datas )
209+ Signals .pictureDownFinished .emit (self .widget )
0 commit comments