本文整理汇总了Python中wechat_sdk.WechatBasic.download_media方法的典型用法代码示例。如果您正苦于以下问题:Python WechatBasic.download_media方法的具体用法?Python WechatBasic.download_media怎么用?Python WechatBasic.download_media使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wechat_sdk.WechatBasic
的用法示例。
在下文中一共展示了WechatBasic.download_media方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wechattest
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import download_media [as 别名]
def wechattest(request):
signature = request.GET.get('signature')
timestamp = request.GET.get('timestamp')
nonce = request.GET.get('nonce')
wechat_instance = WechatBasic(conf = conf)
if not wechat_instance.check_signature(signature = signature, timestamp = timestamp, nonce = nonce) :
return HttpResponseBadRequest('Verify Failed')
else :
if request.method == 'GET' :
response = request.GET.get('echostr', 'err')
else :
try:
wechat_instance.parse_data(request.body)
message = wechat_instance.get_message()
wechat_user_openid = wechat_instance.message.source
# res = wechat_instance.create_qrcode({
# "expire_seconds": "QR_LIMIT_SCENE",
# "action_name": "QR_SCENE",
# "action_info": {
# "scene": {
# "scene_id": 123
# }
# }
# })
# response = wechat_instance.show_qrcode(res['ticket'])
# with open('tmpfile', 'wb') as fd:
# for chunk in response.iter_content(1024):
# fd.write(chunk)
if isinstance(message, ImageMessage) :
try:
link = WechatQRCode.objects.get(wechat_openid = wechat_user_openid)
link.qrcode_url = message.picurl
link.save()
response = wechat_instance.download_media(message.media_id)
# import pdb; pdb.set_trace()
# filename = os.path.join(os.path.pardir,'/static/img/' + link.account.account_name + '.jpeg')
with open('ILink/static/img/' + link.account.account_name + '.jpeg', 'wb') as fd :
for chunk in response.iter_content(1024) :
fd.write(chunk)
reply_text = '二维码上传成功'
except Exception, e:
reply_text = '请先绑定帐号\n\n绑定ILink帐号,请使用 - 为分隔符输入帐号密码\n上传个人微信二维码,请直接发送图片'
elif isinstance(message, TextMessage) :
content = message.content
if content.find("-") > -1 :
# User varification
account, passwd = content.split("-")
# Encode account password
md5 = hashlib.md5()
md5.update(passwd.encode('utf-8'))
passwd = md5.hexdigest()
# Account validation
try:
account_for_validation = Account.objects.get(_account_name = account)
except Exception, e:
reply_text = '绑定ILink帐号,请使用 - 为分隔符输入帐号密码\n上传个人微信二维码,请直接发送图片'
# import pdb; pdb.set_trace()
if account_for_validation :
if account_for_validation.account_passwd == passwd :
try:
link = WechatQRCode.objects.get(wechat_openid = wechat_user_openid)
link.account = account_for_validation
reply_text = '重新绑定ILink帐号成功'
except Exception, e:
newlink = WechatQRCode.create(account = account_for_validation, openid=wechat_user_openid)
newlink.save()
reply_text = '绑定ILink帐号成功'
else :
reply_text = '绑定ILink帐号验证失败'
else :
示例2: __init__
# 需要导入模块: from wechat_sdk import WechatBasic [as 别名]
# 或者: from wechat_sdk.WechatBasic import download_media [as 别名]
#.........这里部分代码省略.........
# create a handler for write the log to file.
fh = logging.FileHandler(self.log_path)
fh.setLevel(self.log_level)
# create a handler for print the log info on console.
ch = logging.StreamHandler()
ch.setLevel(self.log_level)
# set the log format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to logger
self.logger.addHandler(fh)
self.logger.addHandler(ch)
self.logger.info('init over.')
####### web test ######
@bottle.route('/')
def index_get():
try:
# get the post data
self.logger.debug('handle a GET request: /, ')
# e.g : /?signature=04d39d841082682dc7623945528d8086cc9ece97&echostr=8242236714827861439×tamp=1440564411&nonce=2061393952
# get the data
self.logger.debug('handle the request data: %s' %(bottle.request.query_string))
#self.logger.debug('handle the request signature:%s' %(bottle.request.query.signature))
#self.logger.debug('handle the request echostr:%s' %(bottle.request.query.echostr))
#self.logger.debug('handle the request timestamp:%s' %(bottle.request.query.timestamp))
#self.logger.debug('handle the request nonce:%s' %(bottle.request.query.nonce))
return bottle.request.query.echostr
except Exception,ex:
return "%s" %(ex)
return "Hello, this is myWeixinServer."
@bottle.route('/', method="POST")
def index_post():
try:
response = ''
self.logger.debug('handle a POST request: /, ')
self.logger.debug('handle the request data: %s' %(bottle.request.query_string))
post_data = bottle.request.body.getvalue()
self.logger.debug('handle the request post data: %s' %(post_data))
echostr = bottle.request.query.echostr
signature = bottle.request.query.signature
timestamp = bottle.request.query.timestamp
nonce = bottle.request.query.nonce
if self.wechat.check_signature(signature=signature, timestamp=timestamp, nonce=nonce):
self.logger.debug('check_signature ok.')
self.wechat.parse_data(post_data)
message = self.wechat.get_message()
if message.type == 'text':
if message.content == 'wechat':
response = self.wechat.response_text(u'^_^')
elif u'天气' in message.content:
city = u'北京'
data = self.database.get_weather_data(city)
self.logger.debug('get the weather response:{0}'.format(data))
response = self.wechat.response_text(data)
else:
response = self.wechat.response_text(u'文字')
elif message.type == 'image':
response = self.wechat.response_text(u'图片')
elif message.type == 'video':
self.logger.debug('message.media_id:%s' %(message.media_id))
response = self.wechat.download_media(message.media_id)
self.logger.debug('message.media_id:%s over' %(message.media_id))
#response = self.wechat.response_text(u'视频')
elif message.type == 'voice':
response = self.wechat.response_text(u'音频')
elif message.type == 'location':
response = self.wechat.response_text(u'位置')
else:
response = self.wechat.response_text(u'未知')
return response
except Exception,ex:
self.logger.debug('error:%s' %(ex))
return "%s" %(ex)