# -*- coding: utf-8 -*- # !/usr/bin/env python import os from ..api.base import BaseWeChatAPI from ...type import RequestType from ...utils import sha256 class Media(BaseWeChatAPI): def _media_upload(self, content, filepath, filename, path): if not content: if not (filepath and os.path.exists(filepath) and os.path.isfile(filepath) and path): raise Exception('filepath is not assigned or not exists') with open(filepath, mode = 'rb') as f: content = f.read() if not filename: filename = os.path.basename(filepath) else: if not filename: raise Exception('filename is not assigned or not exists') data = { 'meta': '{"filename":"%s","sha256":"%s"}' % (filename, sha256(content)) } mimes = { '.bmp': 'image/bmp', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.avi': 'video/x-msvideo', '.wmv': 'video/x-ms-wmv', '.mpeg': 'video/mpeg', '.mp4': 'video/mp4', '.mov': 'video/quicktime', '.mkv': 'video/x-matroska', '.flv': 'video/x-flv', '.f4v': 'video/x-f4v', '.m4v': 'video/x-m4v', '.rmvb': 'application/vnd.rn-realmedia-vbr' } media_type = os.path.splitext(filename)[-1] if media_type not in mimes: raise Exception('wechatpayv3 does not support this media type.') files = [('file', (filename, content, mimes[media_type]))] return self.client.core.request( path, method = RequestType.POST, data = data, sign_data = data.get('meta'), files = files) def image_upload(self, content = None, filepath = None, filename = None): """图片上传 :param filepath: 图片文件路径 :param filename: 文件名称,未指定则从filepath参数中截取 """ return self._media_upload(content, filepath, filename, '/v3/merchant/media/upload') def video_upload(self, content = None, filepath = None, filename = None): """视频上传 :param filepath: 视频文件路径 :param filename: 文件名称,未指定则从filepath参数中截取 """ return self._media_upload(content, filepath, filename, '/v3/merchant/media/video_upload')