1010def upload_picture (picture_bed_api_url , picture_bed_api_token , picture_path ):
1111 print (picture_bed_api_url , picture_bed_api_token , picture_path )
1212 if not os .path .exists (picture_path ): # 检测是否存在图片
13- print (" 图片文件路径不存在" )
14- return False , " 图片文件路径不存在"
13+ print (' 图片文件路径不存在' )
14+ return False , ' 图片文件路径不存在'
1515 else :
16- print (" 开始获取图床类型" )
16+ print (' 开始获取图床类型' )
1717 # 去除URL的中的' '、' '和'\n',针对用户错误输入的优化
1818 picture_bed_api_url = picture_bed_api_url .replace (' ' , '' )
1919 picture_bed_api_url = picture_bed_api_url .replace (' ' , '' )
2020 picture_bed_api_url = picture_bed_api_url .replace ('\n ' , '' )
2121 get_picture_bed_type_success , picture_bed_type = get_picture_bed_type (picture_bed_api_url )
2222 if get_picture_bed_type_success :
23- print (f" 获取到图床的类型:{ picture_bed_type } " )
24- if picture_bed_type == " lsky-pro" :
23+ print (f' 获取到图床的类型:{ picture_bed_type } ' )
24+ if picture_bed_type == ' lsky-pro' :
2525 return lsky_pro_picture_bed (picture_bed_api_url , picture_bed_api_token , picture_path )
26- elif picture_bed_type == " bohe" :
26+ elif picture_bed_type == ' bohe' :
2727 return bohe_picture_bed (picture_bed_api_url , picture_bed_api_token , picture_path )
28- elif picture_bed_type == " chevereto" :
28+ elif picture_bed_type == ' chevereto' :
2929 return chevereto_picture_bed (picture_bed_api_url , picture_bed_api_token , picture_path )
30- elif picture_bed_type == " freeimage" :
30+ elif picture_bed_type == ' freeimage' :
3131 return freeimage_picture_bed (picture_bed_api_url , picture_bed_api_token , picture_path )
32- elif picture_bed_type == " imgbb" :
32+ elif picture_bed_type == ' imgbb' :
3333 return imgbb_picture_bed (picture_bed_api_url , picture_bed_api_token , picture_path )
34- elif picture_bed_type == " pixhost" :
35- return pixhost_picture_bed (picture_bed_api_url , picture_bed_api_token , picture_path )
34+ elif picture_bed_type == ' pixhost' :
35+ return pixhost_picture_bed (picture_bed_api_url , picture_path )
3636 else :
37- return False , " 你错误更改了图床配置文件?冒号前面的类型是不能随便改的!如果需要支持更多新类型的图床请提Issues,前提是图床支持API上传!"
37+ return False , ' 你错误更改了图床配置文件?冒号前面的类型是不能随便改的!如果需要支持更多新类型的图床请提Issues,前提是图床支持API上传!'
3838 else :
3939 return False , picture_bed_type
4040
@@ -43,48 +43,48 @@ def upload_picture(picture_bed_api_url, picture_bed_api_token, picture_path):
4343def lsky_pro_picture_bed (api_url , api_token , frame_path ):
4444 print ('接受到上传兰空图床请求' )
4545 url = api_url
46- files = {'file' : (frame_path , open (frame_path , 'rb' ), " image/png" )}
46+ files = {'file' : (frame_path , open (frame_path , 'rb' ), ' image/png' )}
4747 headers = {'Authorization' : api_token , 'Accept' : 'json' }
4848 data = {}
4949 print ('值已经获取' )
5050
5151 try :
5252 # 发送POST请求
53- print (" 开始发送上传图床的请求" )
53+ print (' 开始发送上传图床的请求' )
5454 res = requests .post (url , headers = headers , data = data , files = files )
55- print (" 已成功发送上传图床的请求" )
55+ print (' 已成功发送上传图床的请求' )
5656 except requests .RequestException as e :
57- print (" 请求过程中出现错误:" , e )
58- return False , " 请求过程中出现错误:" + str (e )
57+ print (f' 请求过程中出现错误:{ str ( e ) } ' )
58+ return False , f' 请求过程中出现错误:{ str (e )} '
5959
6060 try :
6161 data = json .loads (res .text )
6262 # 提取所需的URL
63- image_url = data [" data" ][ " links" ][ " bbcode" ]
64- print (image_url )
65- return True , image_url
63+ image_bbs_url = data [' data' ][ ' links' ][ ' bbcode' ]
64+ print (image_bbs_url )
65+ return True , image_bbs_url
6666 except KeyError as e :
67- print (False , " 图床响应结果缺少所需的值:" + str (e ))
68- return False , " 图床响应结果缺少所需的值:" + str (e ) + str (res )
67+ print (f' 图床响应结果缺少所需的值:{ str (e )} , { str ( res ) } ' )
68+ return False , f' 图床响应结果缺少所需的值:{ str (e )} , { str (res )} '
6969 except json .JSONDecodeError as e :
70- print (False , " 处理返回的JSON过程中出现错误:" + str (e ))
71- return False , " 处理返回的JSON过程中出现错误:" + str (e ) + str (res )
70+ print (f' 处理返回的JSON过程中出现错误:{ str (e )} , { str ( res ) } ' )
71+ return False , f' 处理返回的JSON过程中出现错误:{ str (e )} , { str (res )} '
7272
7373
7474# 薄荷图床
7575def bohe_picture_bed (api_url , api_token , frame_path ):
76- print (" 开始上传薄荷图床" )
76+ print (' 开始上传薄荷图床' )
7777 url = api_url
78- files = {'uploadedFile' : (frame_path , open (frame_path , 'rb' ), " image/png" )}
78+ files = {'uploadedFile' : (frame_path , open (frame_path , 'rb' ), ' image/png' )}
7979 data = {'api_token' : api_token , 'image_compress' : 0 , 'image_compress_level' : 80 }
8080
8181 try :
8282 # 发送POST请求
8383 res = requests .post (url , data = data , files = files )
84- print (" 已成功发送上传图床的请求" )
84+ print (' 已成功发送上传图床的请求' )
8585 except requests .RequestException as e :
86- print (" 请求过程中出现错误:" , e )
87- return False , " 请求过程中出现错误:" + str (e )
86+ print (f' 请求过程中出现错误:{ str ( e ) } ' )
87+ return False , f' 请求过程中出现错误:{ str (e )} '
8888
8989 # 关闭文件流,避免资源泄露
9090 files ['uploadedFile' ][1 ].close ()
@@ -93,73 +93,73 @@ def bohe_picture_bed(api_url, api_token, frame_path):
9393 try :
9494 api_response = json .loads (res .text )
9595 except json .JSONDecodeError :
96- print (" 响应不是有效的JSON格式" )
97- return False , " 响应不是有效的JSON格式"
96+ print (' 响应不是有效的JSON格式' )
97+ return False , ' 响应不是有效的JSON格式'
9898
9999 # 打印提取的url
100- status_code = api_response .get (" statusCode" , "" )
101- result_data = api_response .get (" resultData" , "" )
102-
103- if status_code == " 200" :
104- bbs_url = str (api_response .get (" bbsurl" , "" ))
105- return True , bbs_url
106- elif status_code == "" :
107- return False , " 未接受到响应"
100+ status_code = api_response .get (' statusCode' , '' )
101+ result_data = api_response .get (' resultData' , '' )
102+
103+ if status_code == ' 200' :
104+ image_bbs_url = str (api_response .get (' bbsurl' , '' ))
105+ return True , image_bbs_url
106+ elif status_code == '' :
107+ return False , ' 未接受到响应'
108108 else :
109- return False , f" API响应出错了,错误码:{ status_code } ,错误提示:{ result_data } "
109+ return False , f' API响应出错了,错误码:{ status_code } ,错误提示:{ result_data } '
110110
111111
112112# chevereto图床
113113def chevereto_picture_bed (api_url , api_token , frame_path ):
114114 print ('接受到上传chevereto图床请求' )
115115 url = api_url
116- data = {'expiration' : 'PT5M' , 'X-API-Key' : api_token , " key" : api_token }
117- files = {'source' : (frame_path , open (frame_path , 'rb' ), " image/png" )}
116+ data = {'expiration' : 'PT5M' , 'X-API-Key' : api_token , ' key' : api_token }
117+ files = {'source' : (frame_path , open (frame_path , 'rb' ), ' image/png' )}
118118 print ('值已经获取' )
119119
120120 try :
121121 # 发送POST请求
122- print (" 开始发送上传图床的请求" )
122+ print (' 开始发送上传图床的请求' )
123123 res = requests .post (url , data = data , files = files )
124- print (" 已成功发送上传图床的请求" )
124+ print (' 已成功发送上传图床的请求' )
125125 except requests .RequestException as e :
126- print (" 请求过程中出现错误:" , e )
127- return False , " 请求过程中出现错误:" + str (e )
126+ print (f' 请求过程中出现错误:{ str ( e ) } ' )
127+ return False , f' 请求过程中出现错误:{ str (e )} '
128128
129129 try :
130130 print (res .text )
131131 data = json .loads (res .text )
132132 # 提取所需的URL
133- image_url = data [" image" ][ " url" ]
133+ image_url = data [' image' ][ ' url' ]
134134 print (image_url )
135- return True , '[img]' + image_url + ' [/img]'
135+ return True , f '[img]{ image_url } [/img]'
136136 except KeyError as e :
137- print (False , " 图床响应结果缺少所需的值:" + str (e ))
138- return False , " 图床响应结果缺少所需的值:" + str (e ) + str (res )
137+ print (f' 图床响应结果缺少所需的值:{ str (e )} , { str ( res ) } ' )
138+ return False , f' 图床响应结果缺少所需的值:{ str (e )} , { str (res )} '
139139 except json .JSONDecodeError as e :
140- print (False , " 处理返回的JSON过程中出现错误:" + str (e ))
141- return False , " 处理返回的JSON过程中出现错误:" + str (e ) + str (res )
140+ print (f' 处理返回的JSON过程中出现错误:{ str (e )} , { str ( res ) } ' )
141+ return False , f' 处理返回的JSON过程中出现错误:{ str (e )} , { str (res )} '
142142
143143
144144# freeimage图床
145145def freeimage_picture_bed (api_url , api_token , frame_path ):
146146 print ('接受到上传freeimage图床请求' )
147147 url = api_url
148148 data = {'key' : api_token , 'format' : 'txt' }
149- files = {'source' : (frame_path , open (frame_path , 'rb' ), " image/png" )}
149+ files = {'source' : (frame_path , open (frame_path , 'rb' ), ' image/png' )}
150150 print ('值已经获取' )
151151
152152 try :
153153 # 发送POST请求
154- print (" 开始发送上传图床的请求" )
154+ print (' 开始发送上传图床的请求' )
155155 res = requests .post (url , data = data , files = files )
156- print (" 已成功发送上传图床的请求" )
156+ print (' 已成功发送上传图床的请求' )
157157 except requests .RequestException as e :
158- print (" 请求过程中出现错误:" , e )
159- return False , " 请求过程中出现错误:" + str (e )
158+ print (' 请求过程中出现错误:' , e )
159+ return False , ' 请求过程中出现错误:' + str (e )
160160
161161 print (res .text )
162- if res .text [:4 ] == " http" :
162+ if res .text [:4 ] == ' http' :
163163 bbs_url = '[img]' + res .text + '[/img]'
164164 print (bbs_url )
165165 return True , bbs_url
@@ -172,61 +172,61 @@ def imgbb_picture_bed(api_url, api_token, frame_path):
172172 print ('接受到上传imgbb图床请求' )
173173 url = api_url
174174 data = {'expiration' : '600' , 'key' : api_token }
175- files = {'image' : (frame_path , open (frame_path , 'rb' ), " image/png" )}
175+ files = {'image' : (frame_path , open (frame_path , 'rb' ), ' image/png' )}
176176 print ('值已经获取' )
177177
178178 try :
179179 # 发送POST请求
180- print (" 开始发送上传图床的请求" )
180+ print (' 开始发送上传图床的请求' )
181181 res = requests .post (url , data = data , files = files )
182- print (" 已成功发送上传图床的请求" )
182+ print (' 已成功发送上传图床的请求' )
183183 except requests .RequestException as e :
184- print (" 请求过程中出现错误:" , e )
185- return False , " 请求过程中出现错误:" + str (e )
184+ print (' 请求过程中出现错误:' , e )
185+ return False , ' 请求过程中出现错误:' + str (e )
186186
187187 try :
188188 data = json .loads (res .text )
189189 # 提取所需的URL
190- image_url = data [" data" ][ " image" ][ " url" ]
190+ image_url = data [' data' ][ ' image' ][ ' url' ]
191191 print (image_url )
192- return True , '[img]' + image_url + ' [/img]'
192+ return True , f '[img]{ image_url } [/img]'
193193 except KeyError as e :
194- print (False , " 图床响应结果缺少所需的值:" + str (e ))
195- return False , " 图床响应结果缺少所需的值:" + str (e ) + str (res )
194+ print (f' 图床响应结果缺少所需的值:{ str (e )} , { str ( res ) } ' )
195+ return False , f' 图床响应结果缺少所需的值:{ str (e )} , { str (res )} '
196196 except json .JSONDecodeError as e :
197- print (False , " 处理返回的JSON过程中出现错误:" + str (e ))
198- return False , " 处理返回的JSON过程中出现错误:" + str (e ) + str (res )
197+ print (f' 处理返回的JSON过程中出现错误:{ str (e )} , { str ( res ) } ' )
198+ return False , f' 处理返回的JSON过程中出现错误:{ str (e )} , { str (res )} '
199199
200200
201201# pixhost图床
202- def pixhost_picture_bed (api_url , api_token , frame_path ):
202+ def pixhost_picture_bed (api_url , frame_path ):
203203 print ('接受到上传pixhost图床请求' )
204204 url = api_url
205- files = {'img' : (frame_path , open (frame_path , 'rb' ), " image/jpeg" )}
205+ files = {'img' : (frame_path , open (frame_path , 'rb' ), ' image/jpeg' )}
206206 data = {'content_type' : 0 , 'max_th_size' : 420 }
207207 headers = {'Accept' : 'application/json' }
208208 print ('值已经获取' )
209209
210210 try :
211211 # 发送POST请求
212- print (" 开始发送上传图床的请求" )
212+ print (' 开始发送上传图床的请求' )
213213 res = requests .post (url , headers = headers , data = data , files = files )
214- print (" 已成功发送上传图床的请求" )
214+ print (' 已成功发送上传图床的请求' )
215215 except requests .RequestException as e :
216- print (" 请求过程中出现错误:" , e )
217- return False , " 请求过程中出现错误:" + str (e )
216+ print (' 请求过程中出现错误:' , e )
217+ return False , ' 请求过程中出现错误:' + str (e )
218218
219219 try :
220220 data = json .loads (res .text )
221221 # 提取所需的URL
222- image_url = data [" th_url" ]
223- image_url = image_url .replace (" //t" , " //img" )
224- image_url = image_url .replace (" /thumbs/" , " /images/" )
222+ image_url = data [' th_url' ]
223+ image_url = image_url .replace (' //t' , ' //img' )
224+ image_url = image_url .replace (' /thumbs/' , ' /images/' )
225225 print (image_url )
226- return True , '[img]' + image_url + ' [/img]'
226+ return True , f '[img]{ image_url } [/img]'
227227 except KeyError as e :
228- print (False , " 图床响应结果缺少所需的值:" + str (e ))
229- return False , " 图床响应结果缺少所需的值:" + str (e ) + str (res )
228+ print (f' 图床响应结果缺少所需的值:{ str (e )} , { str ( res ) } ' )
229+ return False , f' 图床响应结果缺少所需的值:{ str (e )} , { str (res )} '
230230 except json .JSONDecodeError as e :
231- print (False , " 处理返回的JSON过程中出现错误:" + str (e ))
232- return False , " 处理返回的JSON过程中出现错误:" + str (e ) + str (res )
231+ print (f' 处理返回的JSON过程中出现错误:{ str (e )} , { str ( res ) } ' )
232+ return False , f' 处理返回的JSON过程中出现错误:{ str (e )} , { str (res )} '
0 commit comments