1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19



from fastapi import APIRouter, File, UploadFile, Form
from Crawel.fb_cont import get_content
from Schemas.Unicorn import UniMoblie

router = APIRouter()


@router.post("/photo")
async def post_photo(mysize: str, mycolor: str, file: UploadFile = File(...)):
content = await file.read()
with open(file.filename, "wb") as f:
f.write(content)
with open(file.filename, 'rb') as f:
image = f.read()
result = get_content(mysize, mycolor, file.filename, image)
return result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

import json
import base64
import requests
from lxml import etree
from collections import OrderedDict
from urllib3 import encode_multipart_formdata

session = requests.Session()

header = {
# "Cookie": "hidden=value; tmp=100; PHPSESSID=tvlpo8a6mac9ilq3ef97rp0loq; hidden=value; tmp=100; Hm_lvt_a1258fdb51aadcdf62e886e13f8b9252=1640680201,1640708551; Hm_lpvt_a1258fdb51aadcdf62e886e13f8b9252=1640708724",
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundarygcfhLR9P0fj0CgyD",
"Origin": "http://zhaopianhuandise.com",
"Referer": "http://zhaopianhuandise.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"

}

home_url = "http://zhaopianhuandise.com"
upload_url = "http://zhaopianhuandise.com/index/shangcuan.html"
content_url = "http://zhaopianhuandise.com/index/faceapi.html"


def base64_img():
with open("1a.jpg", 'rb') as f:
image = f.read()
return image
# image_base64 = str(base64.b64encode(image), encoding='utf-8')
# return "data:image/jpeg;base64," + image_base64


def get_token():
req = session.get(home_url, headers=header)
html = etree.HTML(req.text)
upload_token = html.xpath('//input[@id="upload_token"]/@value')[0]
return upload_token


def post_upload(name, img_content):
params = OrderedDict(
[("upload_token", (get_token())),
("image_name", (name)),
("image_file", (name, img_content)),
]
)
m = encode_multipart_formdata(params, boundary='----WebKitFormBoundarygcfhLR9P0fj0CgyD')
req = session.post(upload_url, headers=header, data=m[0])
try:
return req.json()
except:
return {
"code": 401,
"msg": "图片类型错误,默认jpg"
}


def get_content(mysize, mycolor, name, img_content):
'''
mysize:1(一寸(295*413,jpg格式))
mysize:2(小一寸(260*378,jpg格式))
mysize:3(大一寸(390*567,jpg格式))
mysize:4(二寸(413*579,jpg格式))
mysize:5(小二寸(413*531,jpg格式))
mysize:6(大二寸(413*626,jpg格式))
mysize:21(五寸(1051*1500,jpg格式))
:return:
'''
data = post_upload(name, img_content)
if data["code"] == 401:
return {
"code": 401,
"msg": "照片类型有误,默认jpg"
}
params = {
"isupload": "1",
"picpath": data["data"]["picPath"],
"mysize": mysize,
"code": data["data"]["code"],
"mycolor": mycolor
}
req = session.get(content_url, headers=header, params=params)
photo_one = home_url + req.json()["data"]["pic_one"]
photo_all = home_url + req.json()["data"]["pic_all"]
return {
"code": 200,
"photo_one": photo_one,
"photo_all": photo_all
}

# get_content("2", "blue")