From ba7a5bc32a192d7598c085118a4a999141fac295 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Wed, 10 Oct 2018 22:13:30 +0800 Subject: [PATCH] Add sm.ms to photostore --- fishroom/photostore.py | 33 +++++++++++++++++++++++++++++++++ fishroom/telegram.py | 4 +++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/fishroom/photostore.py b/fishroom/photostore.py index df1f582..72e001a 100644 --- a/fishroom/photostore.py +++ b/fishroom/photostore.py @@ -88,6 +88,39 @@ def upload_image(self, filename=None, filedata=None, **kwargs) -> str: return r.text.strip() +class SMMS(BasePhotoStore): + + url = "https://sm.ms/api/upload" + + def __init__(self, **kwargs): + pass + + def upload_image(self, filename=None, filedata=None, **kwargs) -> str: + if filedata is None: + with open(filename, 'rb') as f: + filedata = f.read() + files = {"smfile": filedata} + + try: + r = requests.post(self.url, files=files, timeout=5) + except requests.exceptions.Timeout: + logger.error("Timeout uploading to SMMS") + return None + except: + logger.exception("Unknown errror uploading to SMMS") + return None + + try: + ret = json.loads(r.text) + except: + return None + if ret.get('code', None) != 'success': + logger.error("Error: SMMS returned error") + return None + + return ret.get('data', {}).get('url', None) + + if __name__ == "__main__": import sys imgur = Imgur(sys.argv[1]) diff --git a/fishroom/telegram.py b/fishroom/telegram.py index 6727f79..d970385 100644 --- a/fishroom/telegram.py +++ b/fishroom/telegram.py @@ -703,7 +703,7 @@ def Fishroom2TelegramThread(tg: Telegram, bus: MessageBus): def init(): from .db import get_redis from .filestore import get_qiniu - from .photostore import Imgur, VimCN + from .photostore import Imgur, VimCN, SMMS redis_client = get_redis() def photo_store_init(): @@ -713,6 +713,8 @@ def photo_store_init(): return Imgur(**options) elif provider == "vim-cn": return VimCN() + elif provider == "sm.ms": + return SMMS() elif provider == "qiniu": return get_qiniu(redis_client, config)