module_online_data.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # import json
  2. # import time
  3. import copy
  4. import random
  5. import time
  6. import requests
  7. import settings
  8. import json
  9. import numpy as np
  10. import os, io
  11. from PIL import Image
  12. from io import BytesIO
  13. class JsonEncoder(json.JSONEncoder):
  14. """Convert numpy classes to JSON serializable objects."""
  15. def default(self, obj):
  16. if isinstance(obj, (np.integer, np.floating, np.bool_)):
  17. return obj.item()
  18. elif isinstance(obj, np.ndarray):
  19. return obj.tolist()
  20. else:
  21. return super(JsonEncoder, self).default(obj)
  22. class GetOnlineData(object):
  23. def __init__(self):
  24. self.s = requests.session()
  25. self.post_headers = {"Authorization": settings.Headers["Authorization"],
  26. "Origin": settings.Headers["Origin"],
  27. "Host": settings.Headers["Host"],
  28. "Content-Length": "0",
  29. "Content-Type": "application/json",
  30. "Accept": "application/json"}
  31. def refresh_headers(self):
  32. self.post_headers = {"Authorization": settings.Headers["Authorization"],
  33. "Origin": settings.Headers["Origin"],
  34. "Host": settings.Headers["Host"],
  35. "Content-Length": "0",
  36. "Content-Type": "application/json",
  37. "Accept": "application/json"}
  38. def get_key_secret(self):
  39. # 获取抠图剩余次数
  40. url = "{domain}/api/ai_image/client/get_key_serect".format(
  41. domain=settings.DOMAIN)
  42. _s = self.s.post(url=url, headers=self.post_headers, timeout=10)
  43. response_data = _s.json()
  44. return response_data["data"]
  45. def get_cutout_image_times(self):
  46. # 获取抠图剩余次数
  47. url = "{domain}/api/ai_image/client/search_company_balance".format(
  48. domain=settings.DOMAIN)
  49. _s = self.s.post(url=url, headers=self.post_headers, timeout=10)
  50. response_data = _s.json()
  51. if "data" not in response_data:
  52. return False
  53. else:
  54. return response_data["data"]
  55. def search_progress(self, generate_ids):
  56. # 查进度
  57. url = "{domain}/api/ai_image/client/search_progress".format(
  58. domain=settings.DOMAIN
  59. )
  60. data = {"generate_ids": generate_ids}
  61. _s = self.s.post(url=url, headers=self.post_headers, data=json.dumps(data), timeout=60)
  62. response_data = _s.json()
  63. return response_data["data"]
  64. def download_picture(self, url, out_path):
  65. response = requests.get(url, timeout=30)
  66. pic = response.content
  67. if out_path:
  68. with open(out_path, 'wb') as f:
  69. f.write(pic)
  70. else:
  71. return Image.open(BytesIO(pic))
  72. def remove_background(self, images_url):
  73. url = "{domain}/api/ai_image/client/remove_background".format(
  74. domain=settings.DOMAIN
  75. )
  76. data = {"images": images_url}
  77. _s = self.s.post(url=url, headers=self.post_headers, data=json.dumps(data), timeout=30)
  78. response_data = _s.json()
  79. return response_data["data"]["generate_ids"], int(response_data["data"]["balance"])
  80. def get_current_menu(self):
  81. def get_menu(_menu_dict, _data):
  82. for menu in _data:
  83. _menu_dict[menu["key"]] = {}
  84. for mods in menu["mods_arr"]:
  85. _menu_dict[menu["key"]][mods["key"]] = mods["name"]
  86. if "_child" in menu:
  87. get_menu(_menu_dict, menu["_child"])
  88. return _menu_dict
  89. url = "{domain}/api/backend/basic/get_current_menu".format(
  90. domain=settings.DOMAIN,
  91. )
  92. _s = self.s.get(url=url, headers=settings.Headers)
  93. response_data = _s.json()
  94. try:
  95. menu_data = response_data["data"]["pc_menu"]
  96. menu_dict = {}
  97. menu_dict = get_menu(menu_dict, menu_data)
  98. except:
  99. menu_dict = {}
  100. # print(json.dumps(menu_dict,ensure_ascii=False))
  101. # raise 1
  102. return menu_dict
  103. def upload_pic(self, file_path, buffer=None):
  104. if buffer is None:
  105. root_path, file_name = os.path.split(file_path)
  106. e = os.path.splitext(file_name)[1]
  107. __format = {"jpg": "JPEG",
  108. "JPG": "JPEG",
  109. "JPEG": "JPEG",
  110. "jpeg": "JPEG",
  111. "png": "PNG",
  112. "PNG": "PNG", }
  113. _format = __format[e[1:]]
  114. if _format == "JPEG":
  115. buffer = io.BytesIO()
  116. im = Image.open(file_path)
  117. im.save(buffer, format='JPEG')
  118. buffer.seek(0)
  119. else:
  120. with open(file_path, 'rb') as file:
  121. buffer = io.BytesIO(file.read())
  122. files = [
  123. ('file',
  124. (file_path,
  125. buffer,
  126. 'image/{}'.format(_format)))
  127. ]
  128. else:
  129. files = [
  130. ('file',
  131. ("1.jpg",
  132. buffer,
  133. 'image/{}'.format("JPEG")))
  134. ]
  135. url = "{domain}/api/backend/upload".format(
  136. domain=settings.DOMAIN
  137. )
  138. headers = {"Authorization": settings.Headers["Authorization"],
  139. "Origin": settings.Headers["Origin"],
  140. "Host": settings.Headers["Host"],
  141. }
  142. _s = requests.post(url=url, headers=headers, files=files, timeout=60)
  143. # print(_s.text)
  144. response_data = _s.json()
  145. return response_data["data"]["url"]
  146. def get_keys(self):
  147. k = "pxnib99dbchtmdm"
  148. s = "ub9uj5678gs4m2bnrass1t3tn6ughlk065ianosk06akagolcr2u"
  149. return (k,s)
  150. def dispose_point(self,_type):
  151. # 扣分 sub;add为增加分数,每次操作一分
  152. url = "{domain}/api/ai_image/client/dispose_point".format(
  153. domain=settings.DOMAIN)
  154. data = {"type": _type}
  155. _s = self.s.post(url=url, headers=self.post_headers,data=json.dumps(data), timeout=10)
  156. response_data = _s.json()
  157. return response_data
  158. def send_message(self,text):
  159. # 发送钉钉消息
  160. url = "{domain}/api/ai_image/client/send_message".format(
  161. domain=settings.DOMAIN)
  162. data = {"message": text}
  163. _s = self.s.post(url=url, headers=self.post_headers,data=json.dumps(data), timeout=10)
  164. response_data = _s.json()
  165. return response_data