-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoResourceUpload.py
More file actions
57 lines (49 loc) · 1.6 KB
/
DoResourceUpload.py
File metadata and controls
57 lines (49 loc) · 1.6 KB
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
# @Author: AlanStar
# @Contact: alan233@vip.qq.com
# @License: MIT
# Copyright (c) 2022-2022
import json
from requests import Request, Session
from urllib3 import encode_multipart_formdata
import BotInfo
def pic_upload(filePath, fileName):
URL = "https://botopen.imdodo.com/api/v1/resource/picture/upload"
# 重新指定请求头, 此处不使用 PublicHeaders.py
file_data = {
"file": (fileName, open(filePath, "rb").read())
}
# 编码 multipart-data
encode_data = encode_multipart_formdata(file_data)
data = encode_data[0]
# 重构请求头
public_headers = {
"Content-Type": encode_data[1],
"Authorization": BotInfo.combine_Authorization()
}
# 调试用
# print(public_headers)
# print(filePath)
# 生成请求体, 通过 session 请求,
s = Session()
req = Request("POST", url=URL, headers=public_headers, data=data)
# 预计算请求头中的 Content-Length
prepped = req.prepare()
# 调试用
# print(prepped.headers)
# 使用 session 发送请求
file_message = s.send(prepped)
# 接受返回信息并转为 json
file_message = file_message.text
file_message = json.loads(file_message)
# 数据解析
print(file_message)
status = file_message["status"]
message = file_message["message"]
# 返回 json
return file_message
if __name__ == "__main__":
filePath = "C:\\Users\\AlanStar\\Desktop\\B站头像.jpg"
# fileSize = 369017
# fileName 可以指定, 起什么名字都可以
fileName = "pic.jpg"
pic_upload(filePath=filePath, fileName=fileName)