#-*-coding:utf-8-*- import requests import json import os import sys reload(sys) sys.setdefaultencoding( "utf-8" ) class WXMP(object): def __init__(self,appid=None,secret=None): """ """ self.appid = appid if appid else "wx84a42c807ed07198" self.secret = secret if secret else "e2de849dfef826b1854c0b18407a6be2" self.access_token = None def get_access_token(self): """获取access_token """ url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" \ % (self.appid,self.secret) res = requests.get(url) if res.status_code == 200: access_token = res.json().get("access_token") self.access_token = access_token return access_token return None def upload_media(self,imgpath): """ """ access_token = self.get_access_token() url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (access_token,"image") files = {"media":(imgpath,open(imgpath,"rb"),"image/png")} res = requests.post(url,files=files) if res.status_code == 200: return res.json().get("media_id") return None def upload_material_media(self,imgpath): """ """ access_token = self.get_access_token() url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s" % (access_token,"image") files = {"media":(imgpath,open(imgpath,"rb"),"image/png")} res = requests.post(url,files=files) if res.status_code == 200: return res.json().get("media_id") return None def upload_material_media_from_url(self,url): """ """ if not self.access_token: access_token = self.get_access_token() else: access_token = self.access_token print access_token imgname = os.path.split(url)[-1] imgobj = requests.get(url).content #print imgobj url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s" % (access_token,"image") files = {"media":(imgname,imgobj,"image/png")} res = requests.post(url,files=files) print res.json() if res.status_code == 200: return res.json().get("media_id") return None def upload_media_news(self,articles): """ """ if not self.access_token: access_token = self.get_access_token() else: access_token = self.access_token headers = {"Content-Type":"application/json;charset=utf-8"} url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s" % (access_token) res = requests.post(url,data=json.dumps({"articles":articles},ensure_ascii=False).encode("utf-8"),headers=headers) print res.json(),3333333333333 if res.status_code == 200: return res.json().get("media_id") return None def add_material_news(self,articles): """ """ headers = {"Content-Type":"application/json;charset=utf-8"} access_token = self.get_access_token() url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s" % (access_token) res = requests.post(url,data=json.dumps({"articles":articles},ensure_ascii=False).encode("utf-8"),headers=headers) print res.json() if res.status_code == 200: news_media_id = res.json().get("media_id") if not news_media_id: print res.json().get("errcode") if res.json().get("errcode") == 40007: return None,u"您上传的图片格式微信同步发送无法识别通过!" return None,res.json().get("errmsg") return news_media_id,"success" return None,None def add_news_curl(self,title,media_id,authod,digest,content): """ """ headers = {"Content-Type":"application/json;charset=utf-8"} access_token = self.get_access_token() url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s" % (access_token) cmd = u''' curl -H "Content-Type:application/json" -X POST --data '{"articles":[{"title":"%s","thumb_media_id":"%s","authod":"%s","digest":"%s","show_cover_pic":1,"content":"%s"}]}' %s ''' % (title,media_id,authod,digest,content,url) res = os.popen(cmd) def send_news_toall(self,media_id): """推送图文消息给所有微信用户 """ headers = {"Content-Type":"application/json;charset=utf-8"} access_token = self.get_access_token() url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s" % (access_token) data = { "filter":{"is_to_all":True}, "mpnews":{"media_id":media_id}, "msgtype":"mpnews", "send_ignore_reprint":1 } res = requests.post(url,data=json.dumps(data,ensure_ascii=False).encode("utf-8"),headers=headers) print res.json() if res.status_code == 200: news_media_id = res.json().get("msg_data_id") if not news_media_id: if res.json().get("errcode") == 40113: return None,u"您上传的图片格式微信同步发送无法识别通过!" if res.json().get("errcode") == 45028: return None,u"您今日的微信同步发送限额已超限制!" return None,res.json().get("errmsg") return news_media_id,"success" return None,None def get_news_analyse_day(self,begin_date,end_date): """ """ headers = {"Content-Type":"application/json;charset=utf-8"} access_token = self.get_access_token() url = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token=%s" % (access_token) #url = "https://api.weixin.qq.com/datacube/getarticletotal?access_token=%s" % (access_token) data = { "begin_date":begin_date, "end_date":end_date } #res = requests.post(url,data=json.dumps(data,ensure_ascii=False).encode("utf-8"),headers=headers) res = requests.post(url,data=json.dumps(data),headers=headers) if res.json().get("list"): return True,res.json().get("list") return False,res.json() if __name__ == "__main__": wxmp = WXMP() imgpath = "/tmp/testmedia.png" imgpath = "http://tederenoss.oss-cn-beijing.aliyuncs.com/zky/upload/1631503610565.jpeg" #media_id = wxmp.upload_material_media_from_url(imgpath) #print media_id #media_id = wxmp.upload_material_media(imgpath) #articles = [{ # "title":u"测试图文消息", # "thumb_media_id":media_id, # "authod":u"四川环宇星创科技有限公司", # "digest":u"测试图文消息", # "show_cover_pic":1, # "content":u"测试发送图文消息" #},{ # "title":u"测试图文消息111111测试图文消息111111测试图文消息111111测试图文消息111111", # "thumb_media_id":media_id, # "authod":u"四川环宇星创科技有限公司", # "digest":u"测试图文消息", # "show_cover_pic":1, # "content":u"测试发送图文消息333" #}] #news_media_id = wxmp.add_material_news(articles) #wxmp.send_news_toall(news_media_id) begin_date,end_date = "2021-10-12","2021-10-12" import pprint pprint.pprint(wxmp.get_news_analyse_day(begin_date,end_date))