|
|
@@ -9,17 +9,26 @@ import numpy as np
|
|
|
import os, io
|
|
|
from PIL import Image
|
|
|
from io import BytesIO
|
|
|
-Authorization = "Bearer e4ffe8dfe772670ca9c9d234104a7be67a31d689"
|
|
|
-Origin = "http://my2.pubdata.cn"
|
|
|
-Host = "mybackend2.pubdata.cn"
|
|
|
-DOMAIN = "http://mybackend2.pubdata.cn"
|
|
|
-Headers = {
|
|
|
- "Authorization": Authorization,
|
|
|
- "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",
|
|
|
- "Origin": Origin,
|
|
|
- "Host": Host,
|
|
|
- "Content-Type": "application/json;charset=UTF-8",
|
|
|
-}
|
|
|
+import configparser, json
|
|
|
+
|
|
|
+# ===============默认数据配置=====================
|
|
|
+config = configparser.ConfigParser()
|
|
|
+config_name = "config.ini"
|
|
|
+config.read(config_name)
|
|
|
+debug = config.get("app", "debug")
|
|
|
+if bool(debug) == False:
|
|
|
+ origin = config.get("prod", "origin")
|
|
|
+ host = config.get("prod", "host")
|
|
|
+ domain = config.get("prod", "domain")
|
|
|
+else:
|
|
|
+ origin = config.get("dev", "origin")
|
|
|
+ host = config.get("dev", "host")
|
|
|
+ domain = config.get("dev", "domain")
|
|
|
+Origin = origin
|
|
|
+Host = host
|
|
|
+DOMAIN = domain
|
|
|
+
|
|
|
+
|
|
|
class JsonEncoder(json.JSONEncoder):
|
|
|
"""Convert numpy classes to JSON serializable objects."""
|
|
|
|
|
|
@@ -33,10 +42,11 @@ class JsonEncoder(json.JSONEncoder):
|
|
|
|
|
|
|
|
|
class GetOnlineData(object):
|
|
|
- def __init__(self):
|
|
|
+ def __init__(self, token):
|
|
|
+ self.token = "Bearer " + token
|
|
|
self.s = requests.session()
|
|
|
self.post_headers = {
|
|
|
- "Authorization": Authorization,
|
|
|
+ "Authorization": self.token,
|
|
|
"Origin": Origin,
|
|
|
"Host": Host,
|
|
|
"Content-Length": "0",
|
|
|
@@ -46,7 +56,7 @@ class GetOnlineData(object):
|
|
|
|
|
|
def refresh_headers(self):
|
|
|
self.post_headers = {
|
|
|
- "Authorization": Authorization,
|
|
|
+ "Authorization": self.token,
|
|
|
"Origin": Origin,
|
|
|
"Host": Host,
|
|
|
"Content-Length": "0",
|
|
|
@@ -77,7 +87,9 @@ class GetOnlineData(object):
|
|
|
# 查进度
|
|
|
url = "{domain}/api/ai_image/client/search_progress".format(domain=DOMAIN)
|
|
|
data = {"generate_ids": generate_ids}
|
|
|
- _s = self.s.post(url=url, headers=self.post_headers, data=json.dumps(data), timeout=60)
|
|
|
+ _s = self.s.post(
|
|
|
+ url=url, headers=self.post_headers, data=json.dumps(data), timeout=60
|
|
|
+ )
|
|
|
response_data = _s.json()
|
|
|
return response_data["data"]
|
|
|
|
|
|
@@ -85,7 +97,7 @@ class GetOnlineData(object):
|
|
|
response = requests.get(url, timeout=30)
|
|
|
pic = response.content
|
|
|
if out_path:
|
|
|
- with open(out_path, 'wb') as f:
|
|
|
+ with open(out_path, "wb") as f:
|
|
|
f.write(pic)
|
|
|
else:
|
|
|
return Image.open(BytesIO(pic))
|
|
|
@@ -93,9 +105,13 @@ class GetOnlineData(object):
|
|
|
def remove_background(self, images_url):
|
|
|
url = "{domain}/api/ai_image/client/remove_background".format(domain=DOMAIN)
|
|
|
data = {"images": images_url}
|
|
|
- _s = self.s.post(url=url, headers=self.post_headers, data=json.dumps(data), timeout=30)
|
|
|
+ _s = self.s.post(
|
|
|
+ url=url, headers=self.post_headers, data=json.dumps(data), timeout=30
|
|
|
+ )
|
|
|
response_data = _s.json()
|
|
|
- return response_data["data"]["generate_ids"], int(response_data["data"]["balance"])
|
|
|
+ return response_data["data"]["generate_ids"], int(
|
|
|
+ response_data["data"]["balance"]
|
|
|
+ )
|
|
|
|
|
|
def get_current_menu(self):
|
|
|
def get_menu(_menu_dict, _data):
|
|
|
@@ -110,6 +126,13 @@ class GetOnlineData(object):
|
|
|
url = "{domain}/api/backend/basic/get_current_menu".format(
|
|
|
domain=DOMAIN,
|
|
|
)
|
|
|
+ Headers = {
|
|
|
+ "Authorization": self.token,
|
|
|
+ "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",
|
|
|
+ "Origin": Origin,
|
|
|
+ "Host": Host,
|
|
|
+ "Content-Type": "application/json;charset=UTF-8",
|
|
|
+ }
|
|
|
_s = self.s.get(url=url, headers=Headers)
|
|
|
response_data = _s.json()
|
|
|
try:
|
|
|
@@ -127,41 +150,33 @@ class GetOnlineData(object):
|
|
|
root_path, file_name = os.path.split(file_path)
|
|
|
e = os.path.splitext(file_name)[1]
|
|
|
|
|
|
- __format = {"jpg": "JPEG",
|
|
|
- "JPG": "JPEG",
|
|
|
- "JPEG": "JPEG",
|
|
|
- "jpeg": "JPEG",
|
|
|
- "png": "PNG",
|
|
|
- "PNG": "PNG", }
|
|
|
+ __format = {
|
|
|
+ "jpg": "JPEG",
|
|
|
+ "JPG": "JPEG",
|
|
|
+ "JPEG": "JPEG",
|
|
|
+ "jpeg": "JPEG",
|
|
|
+ "png": "PNG",
|
|
|
+ "PNG": "PNG",
|
|
|
+ }
|
|
|
|
|
|
_format = __format[e[1:]]
|
|
|
if _format == "JPEG":
|
|
|
buffer = io.BytesIO()
|
|
|
im = Image.open(file_path)
|
|
|
- im.save(buffer, format='JPEG')
|
|
|
+ im.save(buffer, format="JPEG")
|
|
|
buffer.seek(0)
|
|
|
else:
|
|
|
- with open(file_path, 'rb') as file:
|
|
|
+ with open(file_path, "rb") as file:
|
|
|
buffer = io.BytesIO(file.read())
|
|
|
|
|
|
- files = [
|
|
|
- ('file',
|
|
|
- (file_path,
|
|
|
- buffer,
|
|
|
- 'image/{}'.format(_format)))
|
|
|
- ]
|
|
|
+ files = [("file", (file_path, buffer, "image/{}".format(_format)))]
|
|
|
else:
|
|
|
- files = [
|
|
|
- ('file',
|
|
|
- ("1.jpg",
|
|
|
- buffer,
|
|
|
- 'image/{}'.format("JPEG")))
|
|
|
- ]
|
|
|
+ files = [("file", ("1.jpg", buffer, "image/{}".format("JPEG")))]
|
|
|
|
|
|
url = "{domain}/api/backend/upload".format(domain=DOMAIN)
|
|
|
|
|
|
headers = {
|
|
|
- "Authorization": Authorization,
|
|
|
+ "Authorization": self.token,
|
|
|
"Origin": Origin,
|
|
|
"Host": Host,
|
|
|
}
|
|
|
@@ -174,20 +189,24 @@ class GetOnlineData(object):
|
|
|
def get_keys(self):
|
|
|
k = "pxnib99dbchtmdm"
|
|
|
s = "ub9uj5678gs4m2bnrass1t3tn6ughlk065ianosk06akagolcr2u"
|
|
|
- return (k,s)
|
|
|
+ return (k, s)
|
|
|
|
|
|
- def dispose_point(self,_type):
|
|
|
+ def dispose_point(self, _type):
|
|
|
# 扣分 sub;add为增加分数,每次操作一分
|
|
|
url = "{domain}/api/ai_image/client/dispose_point".format(domain=DOMAIN)
|
|
|
data = {"type": _type}
|
|
|
- _s = self.s.post(url=url, headers=self.post_headers,data=json.dumps(data), timeout=10)
|
|
|
+ _s = self.s.post(
|
|
|
+ url=url, headers=self.post_headers, data=json.dumps(data), timeout=10
|
|
|
+ )
|
|
|
response_data = _s.json()
|
|
|
return response_data
|
|
|
|
|
|
- def send_message(self,text):
|
|
|
+ def send_message(self, text):
|
|
|
# 发送钉钉消息
|
|
|
url = "{domain}/api/ai_image/client/send_message".format(domain=DOMAIN)
|
|
|
data = {"message": text}
|
|
|
- _s = self.s.post(url=url, headers=self.post_headers,data=json.dumps(data), timeout=10)
|
|
|
+ _s = self.s.post(
|
|
|
+ url=url, headers=self.post_headers, data=json.dumps(data), timeout=10
|
|
|
+ )
|
|
|
response_data = _s.json()
|
|
|
return response_data
|