微信公众号图片上传处理
#weixin_wechat/upload_media.py
"""
新增临时素材
"""
from werobot import WeRoBot
import urllib
import requests,os
robot = WeRoBot()
robot.config["APP_ID"] = "xxx"
robot.config["APP_SECRET"] = "xxx"
client = robot.client
token = client.grant_token()
print(token)
def get_img_media_id(img_url,img_file_name):
"""
* 上传临时素菜
* 1、临时素材media_id是可复用的。
* 2、媒体文件在微信后台保存时间为3天,即3天后media_id失效。
* 3、上传临时素材的格式、大小限制与公众平台官网一致。
"""
resource = urllib.request.urlopen(img_url)
f_name = img_file_name
with open(f_name, 'wb') as f:
f.write(resource.read())
#media_json = client.upload_media("image", open(r"./img_media.jpg", "rb")) ## 临时素材
media_json = client.upload_permanent_media("image", open(r"./img_media.jpg", "rb")) ##永久素材
media_id = media_json['media_id']
media_url = media_json['url']
print('微信素材id:',media_id)
return media_id,media_url,media_json
# img_url = "http://p3.img.cctvpic.com/photoAlbum/vms/standard/img/2021/2/22/VIDE1VIFQU4vOR3lBM1R6Sae210222.png"
# img_url = "https://mmbiz.qpic.cn/mmbiz_png/MAQicZAy5oRAicGuDBv1rDXcoEVopGlJcFpc6joTH7W9NwCoHJwShtE26N6NFIu2sEAjyibNkRD4OHeCzaogYz2zg/0?wx_fmt=png"
# img_file_name = 'img_media.jpg'
# print('*'*30)
# media_id,media_url,media_json = get_img_media_id(img_url,img_file_name)
# print(media_id)
# print(media_url)
# print(media_json)