-
Notifications
You must be signed in to change notification settings - Fork 25
/
media.py
45 lines (37 loc) · 1.56 KB
/
media.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
# filename: media.py
from basic import Basic
import urllib2
import poster.encode
from poster.streaminghttp import register_openers
import json
class Media(object):
def __init__(self):
register_openers()
#上传图片
def uplaod(self, accessToken, filePath, mediaType):
openFile = open(filePath, "rb")
param = {'media': openFile}
postData, postHeaders = poster.encode.multipart_encode(param)
postUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (accessToken, mediaType)
request = urllib2.Request(postUrl, postData, postHeaders)
urlResp = urllib2.urlopen(request)
print urlResp.read()
def get(self, accessToken, mediaId):
postUrl = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s" % (accessToken, mediaId)
urlResp = urllib2.urlopen(postUrl)
headers = urlResp.info().__dict__['headers']
if ('Content-Type: application/json\r\n' in headers) or ('Content-Type: text/plain\r\n' in headers):
jsonDict = json.loads(urlResp.read())
print jsonDict
else:
buffer = urlResp.read() # 素材的二进制
mediaFile = file("test_media.jpg", "wb")
mediaFile.write(buffer)
print "get successful"
if __name__ == '__main__':
myMedia = Media()
accessToken = Basic().get_access_token()
filePath = "D:/code/mpGuide/media/test.jpg" #请按实际填写
mediaType = "image"
myMedia.uplaod(accessToken, filePath, mediaType)