def save_file(self, msg, msg_type):
path = os.path.join("storage", self.channel_id)
if not os.path.exists(path):
os.makedirs(path)
filename = "%s_%s_%s" % (msg_type, msg['NewMsgId'], int(time.time()))
fullpath = os.path.join(path, filename)
msg['Text'](fullpath)
mime = magic.from_file(fullpath, mime=True)
if isinstance(mime, bytes):
mime = mime.decode()
guess_ext = mimetypes.guess_extension(mime) or ".unknown"
if guess_ext == ".unknown":
self.logger.warning("File %s with mime %s has no matching extensions.", fullpath, mime)
ext = ".jpeg" if mime == "image/jpeg" else guess_ext
os.rename(fullpath, "%s%s" % (fullpath, ext))
fullpath = "%s%s" % (fullpath, ext)
self.logger.info("File saved from WeChat\nFull path: %s\nMIME: %s", fullpath, mime)
return fullpath, mime
评论列表
文章目录