|
9 | 9 | @file: Utils.ThemeThread |
10 | 10 | @description: |
11 | 11 | """ |
| 12 | +import os |
12 | 13 | from pathlib import Path |
13 | 14 |
|
14 | | -from PyQt5.QtCore import QObject, QThread |
| 15 | +from PyQt5.QtCore import QObject, QThread, QRunnable |
15 | 16 | from PyQt5.QtGui import QLinearGradient, QColor |
| 17 | +import requests |
16 | 18 |
|
17 | 19 | from Utils.CommonUtil import AppLog, Signals |
18 | | -from Utils.Constants import DirThemes |
| 20 | +from Utils.Constants import DirThemes, UrlGetAllCategoriesV2,\ |
| 21 | + UrlGetAppsByCategory, DirImages |
19 | 22 |
|
20 | 23 |
|
21 | 24 | __Author__ = "Irony" |
22 | 25 | __Copyright__ = "Copyright (c) 2019" |
23 | 26 |
|
| 27 | +Headers = { |
| 28 | + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', |
| 29 | + 'Accept-Language': 'zh-CN,zh;q=0.9', |
| 30 | + 'Cache-Control': 'max-age=0', |
| 31 | + 'Upgrade-Insecure-Requests': '1', |
| 32 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6824.400 QQBrowser/10.3.3137.400' |
| 33 | +} |
| 34 | + |
24 | 35 |
|
25 | 36 | def splistList(src, length): |
26 | 37 | # 等分列表 |
@@ -141,3 +152,65 @@ def run(self): |
141 | 152 |
|
142 | 153 | Signals.themeItemAddFinished.emit() |
143 | 154 | AppLog.info('theme thread end') |
| 155 | + |
| 156 | + |
| 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 | + |
| 177 | +class GetAllCategoryRunnable(QRunnable): |
| 178 | + |
| 179 | + def __init__(self, cid, widget, *args, **kwargs): |
| 180 | + super(GetAllCategoryRunnable, self).__init__(*args, **kwargs) |
| 181 | + self.setAutoDelete(True) |
| 182 | + self.cid = cid |
| 183 | + self.widget = widget |
| 184 | + |
| 185 | + def download(self, index, url): |
| 186 | + try: |
| 187 | + dirPath = os.path.join(DirImages, self.cid) |
| 188 | + os.makedirs(dirPath, exist_ok=True) |
| 189 | + req = requests.get(url, headers=Headers) |
| 190 | + if req.status_code == 200: |
| 191 | + path = os.path.join(dirPath, os.path.basename(url)) |
| 192 | + with open(path, 'wb') as fp: |
| 193 | + fp.write(req.content) |
| 194 | + self.pictureDownloadFinished.emit(self, index, path) |
| 195 | + except Exception as e: |
| 196 | + AppLog.exception(e) |
| 197 | + |
| 198 | + def run(self): |
| 199 | + datas = [] |
| 200 | + try: |
| 201 | + req = requests.get(UrlGetAppsByCategory.format( |
| 202 | + cid=self.cid, start=0, count=200)) |
| 203 | + 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) |
| 211 | + if not url: |
| 212 | + continue |
| 213 | + self.download(i, url) |
| 214 | + except Exception as e: |
| 215 | + AppLog.exception(e) |
| 216 | + Signals.getCategoryFinished.emit(self.widget, datas) |
0 commit comments