aigc_decode_server.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from typing import Dict, List
  2. from app.infra.shared import AsyncHttpClient
  3. class AigcDecodeServer:
  4. base_url: str = "https://aigc-api.aiddit.com"
  5. @staticmethod
  6. def _sanitize_images(posts: List[Dict]) -> List[Dict]:
  7. for post in posts:
  8. images = post.get("images")
  9. if images:
  10. post["images"] = [
  11. img
  12. for img in images
  13. if img and img.startswith(("http://", "https://"))
  14. ]
  15. return posts
  16. async def submit_decode(
  17. self, config_id: int, posts: List[Dict], skip_completed: bool = False
  18. ) -> Dict:
  19. """批量提交帖子解构
  20. POST /aigc/api/task/decode
  21. """
  22. url = f"{self.base_url}/aigc/api/task/decode"
  23. headers = {"Content-Type": "application/json"}
  24. payload = {
  25. "params": {
  26. "configId": config_id,
  27. "skipCompleted": skip_completed,
  28. "posts": self._sanitize_images(posts),
  29. }
  30. }
  31. async with AsyncHttpClient(timeout=180) as client:
  32. return await client.post(url, json=payload, headers=headers)
  33. async def query_decode_results(
  34. self, config_id: int, channel_content_ids: List[str]
  35. ) -> Dict:
  36. """批量查询解构结果
  37. POST /aigc/api/task/decode/result
  38. """
  39. url = f"{self.base_url}/aigc/api/task/decode/result"
  40. headers = {"Content-Type": "application/json"}
  41. payload = {
  42. "params": {"configId": config_id, "channelContentIds": channel_content_ids}
  43. }
  44. async with AsyncHttpClient(timeout=60) as client:
  45. return await client.post(url, json=payload, headers=headers)
  46. async def cancel_decode_tasks(
  47. self, config_id: int, channel_content_ids: List[str]
  48. ) -> Dict:
  49. """取消待执行解构任务
  50. POST /aigc/api/task/decode/cancel
  51. """
  52. url = f"{self.base_url}/aigc/api/task/decode/cancel"
  53. headers = {"Content-Type": "application/json"}
  54. payload = {
  55. "params": {"configId": config_id, "channelContentIds": channel_content_ids}
  56. }
  57. async with AsyncHttpClient(timeout=30) as client:
  58. return await client.post(url, json=payload, headers=headers)
  59. __all__ = ["AigcDecodeServer"]