注意:因为是国外的服务,有时候不是很稳定,上传和删除速度都有点慢
- 登陆SM.MS官网注册账号,进入Dashboard
- 生成并复制Secret Token
- sm.ms的API文档写的很简洁,直接参考API发送request请求即可
- 我封装了几个满足我需求的指令,可以参考下python的写法
upload()
: 提供图片路径即可直接上传,返回图片url,格式例如:https://s2.loli.net/2022/03/24/rvgPMndtU3QzZ8w.pngdeleteHistory()
: 删除之前所有的存储(不知道是网络的原因还是什么,这样一个一个get请求删会很慢)- 因为国外的服务有时候不很稳定,这里设置了5s的超时时间,记得在主函数里捕捉下
requests.exceptions.RequestException
异常
class SMMS(object):
def __init__(self):
self.headers = {'Authorization': '[Secret Token]'}
def isDiskFull(self):
url = 'https://sm.ms/api/v2/profile'
res = requests.post(url, headers=self.headers, timeout=5).json()
if res["disk_usage_raw"] > 0.9 * res["disk_limit_raw"]:
return True
def upload(self, filepath):
files = {'smfile': open(filepath, 'rb')}
url = 'https://sm.ms/api/v2/upload'
res = requests.post(url, files=files, headers=self.headers, timeout=5).json()
# print(json.dumps(res, indent=4))
return res['data']['url']
def getHistory(self):
url = 'https://sm.ms/api/v2/upload_history'
res = requests.get(url, headers=self.headers, timeout=5).json()
return res
def deleteHistory(self):
history = self.getHistory()
lastTime = time.time()
for item in history["data"]:
if item["created_at"] < lastTime:
url = 'https://sm.ms/api/v2/delete/{}'.format(item["hash"])
res = requests.get(url, headers=self.headers, timeout=5).json()
print("History deleted!")