module_online_data.py 6.4 KB

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