Skip to content

Commit

Permalink
Add sm.ms to photostore
Browse files Browse the repository at this point in the history
  • Loading branch information
zhsj committed Oct 10, 2018
1 parent a1790d7 commit ba7a5bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions fishroom/photostore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion fishroom/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)

Expand Down

0 comments on commit ba7a5bc

Please # to comment.