Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
update image base64
Browse files Browse the repository at this point in the history
  • Loading branch information
ZegWe committed Sep 1, 2021
1 parent 40d94e6 commit a1f268b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
65 changes: 22 additions & 43 deletions model/message_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,10 @@ def __init__(self, url: str, qid: int, to: int):
self.url: str = url
self.qid: int = qid
self.to: int = to
self._data: dict = {}
self._image_data: dict = {}

def _get_data(self, message: str) -> dict:
tmp = self._data
tmp["Content"] = message
return tmp

def _get_image_data(self, url: str) -> dict:
tmp = self._image_data
tmp["PicUrl"] = url
return tmp

self._data : dict = {
"ToUserUid": self.to,
}

@classmethod
def get_last_time(cls) -> datetime.datetime:
Expand All @@ -41,12 +32,24 @@ def set_last_time(cls, time: datetime.datetime):
cls.__last_time = time

def send(self, message: str):
data = self._get_data(message)
self.send_data(data)
tmp = self._data.copy()
tmp["SendMsgType"] = "TextMsg"
tmp["Content"] = message
self.send_data(tmp)

def send_image_by_url(self, url: str, msg: str = ""):
tmp = self._data.copy()
tmp["SendMsgType"] = "PicMsg"
tmp["PicUrl"] = url
tmp["Content"] = msg
self.send_data(tmp)

def send_image(self, url: str):
data = self._get_image_data(url)
self.send_data(data)
def send_image_by_base64(self, base64_data: str, msg: str = ""):
tmp = self._data.copy()
tmp["SendMsgType"] = "PicMsg"
tmp["PicBase64Buf"] = base64_data
tmp["Content"] = msg
self.send_data(tmp)

def send_data(self, data: dict):
delta = datetime.timedelta(seconds=1.1)
Expand All @@ -70,33 +73,9 @@ def send_data(self, data: dict):
class GroupSender(MsgSender):
def __init__(self, url: str, qid: int, to: int):
super().__init__(url, qid, to)
self._data = {
"ToUserUid": self.to,
"SendToType": 2,
"SendMsgType": "TextMsg",
"Content": ""
}

self._image_data = {
"ToUserUid": self.to,
"SendToType": 2,
"SendMsgType": "PicMsg",
"PicUrl": ""
}
self._data["SendToType"] = 2

class FriendSender(MsgSender):
def __init__(self, url: str, qid: int, to: int):
super().__init__(url, qid, to)
self._data = {
"ToUserUid": self.to,
"SendToType": 1,
"SendMsgType": "TextMsg",
"Content": ""
}

self._image_data = {
"ToUserUid": self.to,
"SendToType": 1,
"SendMsgType": "PicMsg",
"PicUrl": ""
}
self._data["SendToType"] = 1
1 change: 0 additions & 1 deletion plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from .dota2fortune import Fortune
from .dota2mdi import Mdi
from .dota2watcher import Dota2Watcher
from model.logger import logger

plugin_list: list = [
Fortune,
Expand Down
8 changes: 5 additions & 3 deletions plugins/dota2mdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from model.command import Command
from model.message_sender import GroupSender
from model.plugin import Plugin

import requests, base64

class Mdi(Plugin):
__name = "DOTA2战绩图"
Expand All @@ -13,8 +13,10 @@ def __init__(self, group_id: int, sender: GroupSender):

def get_image(self, match_id: int, FromUserId: int) -> None:
url = Config.mdi_url+"/match?id="+str(match_id)
self.sender.send('图片正在生成中...')
self.sender.send_image(url)
self.sender.send('战绩图正在生成中...')
r = requests.get(url)
data = base64.b64encode(r.content)
self.sender.send_image_by_base64(str(data, encoding="utf-8"), '战绩图生成成功!')

@classmethod
def get_name(cls):
Expand Down

0 comments on commit a1f268b

Please sign in to comment.