Skip to content

Commit 1340f6f

Browse files
committed
添加遇见图床的支持
1 parent 7b7e32c commit 1340f6f

File tree

7 files changed

+70
-14
lines changed

7 files changed

+70
-14
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pip install --upgrade markdown-img-icexmoon
8585

8686
可以切换图床服务,以备某个图床不可用或者访问不稳定。
8787

88-
目前支持的图床有[**sm.ms**](https://sm.ms/)、阿里、[**如优**](https://img.rruu.net/)[**Vim-CN**](https://img.vim-cn.com/)
88+
目前支持的图床有[**sm.ms**](https://sm.ms/)、阿里、[**如优**](https://img.rruu.net/)[**Vim-CN**](https://img.vim-cn.com/)[**遇见**](https://www.hualigs.cn/)
8989

9090
1. 执行`python -m markdown_img -i ali`
9191

@@ -163,4 +163,10 @@ python -m markdown_img -m refresh
163163

164164
在生成的图片索引中加入换行以区分图片。
165165

166-
加入的引用模块检测,安装时如果缺少相应模块会自动安装。
166+
加入引用模块检测,安装时如果缺少相应模块会自动安装。
167+
168+
### 0.1.8
169+
170+
加入遇见图床支持。
171+
172+
阿里图床的API调用替换为遇见图床。

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[metadata]
22
# replace with your username:
33
name = markdown-img-icexmoon
4-
version = 0.1.7
4+
version = 0.1.8
55
author = icexmoon
66
author_email = [email protected]
77
description = A program for find and upload images in markdown file and will replace them.

src/markdown_img/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Config():
77
PARAM_SMMS_TOKEN = 'smms_token'
88
PARAM_RRUU_TOKEN = 'rruu_token'
99
PARAM_IMG_SERVICE = 'img_service'
10+
PARAM_YUJIAN_TOKEN = 'yujian_token'
11+
IMG_SERVICE_YUJIAN = 'yujian'
1012
IMG_SERVICE_SMMS = 'smms'
1113
IMG_SERVICE_RRUU = 'rruu'
1214
IMG_SERVICE_ALI = 'ali'
@@ -82,6 +84,12 @@ def getRruuToken(self):
8284
raise UserException(UserException.CODE_NO_RRUU_TOKEN)
8385
return token
8486

87+
def getYujianToken(self):
88+
token = self.getConfigParam(Config.PARAM_YUJIAN_TOKEN)
89+
if token == '':
90+
raise UserException(UserException.CODE_NO_YUJIAN_TOKEN)
91+
return token
92+
8593
def getSmmsToken(self):
8694
token = self.getConfigParam(Config.PARAM_SMMS_TOKEN)
8795
if token == '':

src/markdown_img/help.info

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
ali 阿里图床,访问速度快(非正常提供服务,稳定性不保证)。
99
rruu 如优图床。
1010
vimcn Vim-CN图床,访问速度偏慢。
11+
yujian 遇见图床。
1112
-c --change_token 替换图床访问令牌
1213
rruu 如优图床。
1314
smms sm.ms图床。
15+
yujian 遇见图床。
1416
-s --scan 扫描当前目录的图片并生成网络图床索引

src/markdown_img/main.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,30 @@ def dealUserException(self, userExp: UserException):
114114
sysConfig.writeMainConfig()
115115
print("访问令牌已保存,请重新运行程序")
116116
elif userExp.getErrorCode() == UserException.CODE_UPLOAD_ERROR:
117-
currentImgService = sysConfig.getConfigParam(Config.PARAM_IMG_SERVICE)
118-
print("上传图片到"+str(currentImgService)+"失败,请检查日志文件", sysConfig.getErrorLogFilePath())
117+
currentImgService = sysConfig.getConfigParam(
118+
Config.PARAM_IMG_SERVICE)
119+
print("上传图片到"+str(currentImgService)+"失败,请检查日志文件",
120+
sysConfig.getErrorLogFilePath())
119121
elif userExp.getErrorCode() == UserException.CODE_TIMEOUT:
120122
print(userExp.getErrorMsg())
123+
elif userExp.getErrorCode() == UserException.CODE_NO_YUJIAN_TOKEN:
124+
token = input("缺少你的遇见图床访问令牌,请输入:")
125+
sysConfig.setConfigParam(Config.PARAM_YUJIAN_TOKEN, token)
126+
sysConfig.writeMainConfig()
127+
print("访问令牌已保存,请重新运行程序")
121128
else:
122129
print("未定义错误,请联系开发者")
123130
exit()
124131

125-
def main(self, refresh = False):
132+
def main(self, refresh=False):
126133
# 检索当前目录中的markdown文件
127134
for dir in os.listdir():
128135
if os.path.isfile(dir):
129136
if self.isOrigMdFile(dir):
130137
# 如果副本存在,刷新模式下且原md文件更新过的,删除副本,重新生成,否则不处理
131138
copyFilePath = self.getCopyFilePath(dir)
132139
if os.path.exists(copyFilePath):
133-
if refresh and TimeHelper.compareTwoFilsLastModifyTime(dir,copyFilePath) > 0:
140+
if refresh and TimeHelper.compareTwoFilsLastModifyTime(dir, copyFilePath) > 0:
134141
os.remove(copyFilePath)
135142
else:
136143
continue
@@ -189,7 +196,7 @@ def recoveryImgsInMarkdown(self, copyFilePath: 'copied markdown file path', orig
189196
return False
190197

191198
def changeImgService(self, selectedService):
192-
supportedService = {'smms', 'ali', 'rruu', 'vimcn'}
199+
supportedService = {'smms', 'ali', 'rruu', 'vimcn', 'yujian'}
193200
if selectedService not in supportedService:
194201
print('不支持的图床服务', selectedService)
195202
return False
@@ -203,6 +210,9 @@ def changeImgService(self, selectedService):
203210
elif selectedService == 'vimcn':
204211
sysConfig.setConfigParam(
205212
Config.PARAM_IMG_SERVICE, Config.IMG_SERVICE_VIMCN)
213+
elif selectedService == 'yujian':
214+
sysConfig.setConfigParam(
215+
Config.PARAM_IMG_SERVICE, Config.IMG_SERVICE_YUJIAN)
206216
else:
207217
sysConfig.setConfigParam(
208218
Config.PARAM_IMG_SERVICE, Config.IMG_SERVICE_SMMS)
@@ -211,7 +221,7 @@ def changeImgService(self, selectedService):
211221
return True
212222

213223
def changeToken(self, imgService):
214-
tokenImgServices = {'rruu', 'smms'}
224+
tokenImgServices = {'rruu', 'smms', 'yujian'}
215225
if imgService not in tokenImgServices:
216226
print('不是合法的图床', imgService)
217227
return False
@@ -221,6 +231,8 @@ def changeToken(self, imgService):
221231
sysConfig.setConfigParam(Config.PARAM_RRUU_TOKEN, token)
222232
elif imgService == 'smms':
223233
sysConfig.setConfigParam(Config.PARAM_SMMS_TOKEN, token)
234+
elif imgService == 'yujian':
235+
sysConfig.setConfigParam(Config.PARAM_YUJIAN_TOKEN, token)
224236
else:
225237
pass
226238
sysConfig.writeMainConfig()

src/markdown_img/smms_img.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ def uploadToSmms(self, path: str) -> str:
2222
return res['images']
2323
else:
2424
pass
25-
# logOpen = open(file='upload.log', mode='a')
26-
# print(res, file=logOpen)
27-
# logOpen.close()
2825
self.sysConfig.writeErrorLog(str(res))
2926
return False
3027

@@ -64,18 +61,48 @@ def uploadToRruu(self, imgPath):
6461
return urls['ali']
6562
return urls['rruu']
6663

64+
def uploadToYujian(self, imgPath):
65+
'''上传到遇见图床和阿里图床'''
66+
imgOpen = open(imgPath, 'rb')
67+
files = {'image': imgOpen}
68+
apiType = 'ali'
69+
token = self.sysConfig.getYujianToken()
70+
r = requests.post('https://www.hualigs.cn/api/upload',
71+
data={'apiType': apiType, 'privateStorage': '', 'token': token}, files=files)
72+
imgOpen.close()
73+
try:
74+
respJson = r.json()
75+
except json.decoder.JSONDecodeError as e:
76+
self.sysConfig.writeErrorLog("接口解析错误:"+str(e)+"\n返回信息:"+r.text)
77+
raise UserException(UserException.CODE_UPLOAD_ERROR)
78+
except Exception as e:
79+
self.sysConfig.writeErrorLog("未知的接口调用错误:"+str(e))
80+
raise UserException(UserException.CODE_UPLOAD_ERROR)
81+
urls = {}
82+
if str(respJson['code']).strip() == '200' and str(respJson['msg']).strip() == 'success':
83+
urls['yujian'] = respJson['data']['url']['distribute']
84+
urls['ali'] = respJson['data']['url']['ali']
85+
else:
86+
return False
87+
if self.sysConfig.getConfigParam(Config.PARAM_IMG_SERVICE) == Config.IMG_SERVICE_ALI:
88+
return urls['ali']
89+
return urls['yujian']
90+
6791
def multiUploadImage(self, images: list, results: dict):
6892
'''批量上传图片'''
6993
if len(images) <= 10:
7094
for localImg in images:
71-
imgService = self.sysConfig.getConfigParam(Config.PARAM_IMG_SERVICE)
95+
imgService = self.sysConfig.getConfigParam(
96+
Config.PARAM_IMG_SERVICE)
7297
webImage = ''
7398
if imgService == Config.IMG_SERVICE_ALI:
74-
webImage = self.uploadToRruu(localImg)
99+
webImage = self.uploadToYujian(localImg)
75100
elif imgService == Config.IMG_SERVICE_RRUU:
76101
webImage = self.uploadToRruu(localImg)
77102
elif imgService == Config.IMG_SERVICE_VIMCN:
78103
webImage = self.uploadToVimCn(localImg)
104+
elif imgService == Config.IMG_SERVICE_YUJIAN:
105+
webImage = self.uploadToYujian(localImg)
79106
else:
80107
webImage = self.uploadToSmms(localImg)
81108
if webImage == False:

src/markdown_img/user_exception.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class UserException (Exception):
55
CODE_TIMEOUT = 3
66
CODE_NO_SMMS_TOKEN = 4
77
CODE_NO_RRUU_TOKEN = 5
8+
CODE_NO_YUJIAN_TOKEN = 6
89

910
def __init__(self, errorCode: int, errorMsg: str = ""):
1011
self.errorCode = errorCode

0 commit comments

Comments
 (0)