素材生产发布线程
| 15 | |
| 16 | |
| 17 | class MaterialThread(QThread): |
| 18 | """素材生产发布线程""" |
| 19 | |
| 20 | log_signal = Signal(str) |
| 21 | |
| 22 | def __init__(self, materials, production_window, material_service): |
| 23 | super().__init__() |
| 24 | self.materials = materials |
| 25 | self.production_window = production_window |
| 26 | self.material_service = material_service |
| 27 | self._running = True |
| 28 | |
| 29 | def run(self): |
| 30 | """运行线程""" |
| 31 | try: |
| 32 | selected_model, api_key, config = user_opt() |
| 33 | if False in [selected_model, api_key]: |
| 34 | self.log_signal.emit("请先配置模型和api_key") |
| 35 | time.sleep(10) |
| 36 | return |
| 37 | |
| 38 | is_publish, is_not_full, api_key, selected_model, prompt = check_user_token() |
| 39 | print(is_publish, is_not_full, api_key, selected_model, prompt) |
| 40 | if not is_publish: |
| 41 | self.log_signal.emit("请先在模型中心配置") |
| 42 | return |
| 43 | |
| 44 | for material in self.materials: |
| 45 | if not self._running: |
| 46 | break |
| 47 | |
| 48 | try: |
| 49 | self.log_signal.emit(f"开始处理素材: {material['title']}") |
| 50 | |
| 51 | # 1. 下载图片 |
| 52 | self.log_signal.emit("开始下载图片") |
| 53 | img_dir = os.path.join("temp", "img_temp", f"zdy_{material['id']}") |
| 54 | os.makedirs(img_dir, exist_ok=True) |
| 55 | |
| 56 | img_paths = [] |
| 57 | headers = { |
| 58 | "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36", |
| 59 | } |
| 60 | for i, img_url in enumerate(material["image_list"]): |
| 61 | print(img_url) |
| 62 | try: |
| 63 | response = requests.get(img_url, headers=headers, timeout=30) |
| 64 | if response.status_code == 200: |
| 65 | img_path = os.path.join(img_dir, f"{i}.jpg") |
| 66 | with open(img_path, "wb") as f: |
| 67 | f.write(response.content) |
| 68 | img_paths.append(img_path) |
| 69 | self.log_signal.emit(f"下载图片 {i + 1}/3 成功") |
| 70 | else: |
| 71 | self.log_signal.emit( |
| 72 | f"下载图片 {i + 1}/3 失败: HTTP {response.status_code}" |
| 73 | ) |
| 74 | except Exception as e: |
no outgoing calls
no test coverage detected