Skip to content

Commit

Permalink
🐞 fix: apis 模型处理的一个bug
Browse files Browse the repository at this point in the history
  • Loading branch information
WhaleFell committed May 8, 2022
1 parent a34163a commit 3ec03ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
23 changes: 16 additions & 7 deletions flask_app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from datetime import datetime
from . import ModelView
import json
from typing import Union,Optional
from typing import Union, Optional
from pydantic import BaseModel

default_header = {
"User-Agent": "Mozilla/5.0 (Linux; U; Android 10; zh-cn; Mi 10 Build/QKQ1.191117.002) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/79.0.3945.147 Mobile Safari/537.36 XiaoMi/MiuiBrowser/13.5.40"
}


class ApisModelVies(ModelView):
create_template = 'api_edit.html'
edit_template = 'api_edit.html'
Expand All @@ -22,11 +23,12 @@ class ApisModelVies(ModelView):
# 可以导出 csv
can_export = True


class Apis(db.Model):
id = db.Column(db.Integer, primary_key=True) # 主键
desc = db.Column(db.String(20), default="Default") # 描述
url = db.Column(db.String(9999), unique=True, nullable=False) # 链接
method = db.Column(db.Enum("GET","POST"), nullable=False) # 请求方法
method = db.Column(db.Enum("GET", "POST"), nullable=False) # 请求方法
header = db.Column(db.String(9999)) # 请求头
data = db.Column(db.String(9999)) # 请求数据
add_time = db.Column(db.DateTime(), default=datetime.now) # 添加时间
Expand All @@ -42,11 +44,14 @@ class API(BaseModel):
def replace_data(self, content: Union[str, dict], phone) -> str:
# 统一转换成 str 再替换.
content = str(content).replace("[phone]", phone).replace(
"[timestamp]", self.timestamp_new()).replace("'",'"')
"[timestamp]", self.timestamp_new()).replace("'", '"')
# 尝试 json 化
try:
# json.loads(content)
# print("json成功",content)
return json.loads(content)
except:
# print("json失败",content)
return content

def timestamp_new(self) -> str:
Expand All @@ -59,14 +64,18 @@ def handle_API(self, phone=None):
:return: API basemodel
"""
# 仅仅当传入 phone 参数时添加 Referer
# fix: 这段代码很有问题.......
if phone:
# 进入的 header 是个字符串
if self.header == "":
self.header = {}
self.header['Referer'] = self.url # 增加 Referer
else:
self.header = self.replace_data(self.header, phone)


self.header = self.replace_data(self.header, phone)
if not self.header.get('Referer'):
self.header['Referer'] = self.url # 增加 Referer

self.data = self.replace_data(self.data, phone)
self.url = self.replace_data(self.url, phone)
# print(self)
return self

2 changes: 1 addition & 1 deletion smsboom.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def req(api: Union[API, str], phone: tuple):
resp = client.get(url=api, headers=default_header)
logger.info(f"GETAPI接口-{resp.text[:30]}")
except httpx.HTTPError as why:
logger.error(f"{why.request.url}请求失败{why}")
logger.error(f"请求失败{why}")


@click.command()
Expand Down
12 changes: 8 additions & 4 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ def handle_API(self, phone: str=None):
:param API: one API basemodel
:return: API basemodel
"""
# 如果传入的 header 是字符串,就转为字典.
# 仅仅当传入 phone 参数时添加 Referer
# fix: 这段代码很有问题.......
if phone:
# 进入的 header 是个字符串
if self.header == "":
self.header = {}
self.header['Referer'] = self.url # 增加 Referer
else:
self.header = self.replace_data(self.header, phone)


self.header = self.replace_data(self.header, phone)
if not self.header.get('Referer'):
self.header['Referer'] = self.url # 增加 Referer

self.data = self.replace_data(self.data, phone)
self.url = self.replace_data(self.url, phone)
# print(self)
return self

0 comments on commit 3ec03ba

Please sign in to comment.