| 170 | |
| 171 | |
| 172 | class GetAllCategoryRunnable(QRunnable): |
| 173 | |
| 174 | def __init__(self, category, widget, *args, **kwargs): |
| 175 | super(GetAllCategoryRunnable, self).__init__(*args, **kwargs) |
| 176 | self.setAutoDelete(True) |
| 177 | self.category = category |
| 178 | self.widget = widget |
| 179 | |
| 180 | def download(self, index, title, url): |
| 181 | try: |
| 182 | dirPath = os.path.join(DirWallpaper, self.category) |
| 183 | os.makedirs(dirPath, exist_ok=True) |
| 184 | path = os.path.join(dirPath, os.path.basename(url)) |
| 185 | if os.path.exists(path) and os.path.isfile(path): |
| 186 | Signals.pictureItemAdded.emit(self.widget, index, title, path) |
| 187 | return |
| 188 | req = requests.get(url, headers=Headers) |
| 189 | if req.status_code == 200: |
| 190 | with open(path, 'wb') as fp: |
| 191 | fp.write(req.content) |
| 192 | Signals.pictureItemAdded.emit(self.widget, index, title, path) |
| 193 | except Exception as e: |
| 194 | AppLog.exception(e) |
| 195 | |
| 196 | def run(self): |
| 197 | try: |
| 198 | req = requests.get( |
| 199 | UrlGetAppsByCategory.format(category=self.category, |
| 200 | pageno=1, |
| 201 | count=20, |
| 202 | time=time())) |
| 203 | content = req.json() |
| 204 | errno = content.get('errno', 0) |
| 205 | AppLog.debug('errno: %s', errno) |
| 206 | AppLog.debug('msg: %s', content.get('msg', '')) |
| 207 | if errno != 0: |
| 208 | return |
| 209 | |
| 210 | content = content.get('data', {}) |
| 211 | AppLog.debug('total_count: %s', content.get('total_count', '')) |
| 212 | AppLog.debug('total_page: %s', content.get('total_page', '')) |
| 213 | items = content.get('list', []) |
| 214 | |
| 215 | for i, item in enumerate(items): |
| 216 | title = item.get('title', '') |
| 217 | url = item.get('image', None) |
| 218 | if not url: |
| 219 | continue |
| 220 | self.download(i, title, url) |
| 221 | QThread.msleep(200) |
| 222 | QThread.yieldCurrentThread() |
| 223 | except Exception as e: |
| 224 | AppLog.exception(e) |
| 225 | Signals.pictureDownFinished.emit(self.widget) |