wxmp.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #-*-coding:utf-8-*-
  2. import requests
  3. import json
  4. import os
  5. import sys
  6. reload(sys)
  7. sys.setdefaultencoding( "utf-8" )
  8. class WXMP(object):
  9. def __init__(self,appid=None,secret=None):
  10. """
  11. """
  12. self.appid = appid if appid else "wx84a42c807ed07198"
  13. self.secret = secret if secret else "e2de849dfef826b1854c0b18407a6be2"
  14. self.access_token = None
  15. def get_access_token(self):
  16. """获取access_token
  17. """
  18. url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" \
  19. % (self.appid,self.secret)
  20. res = requests.get(url)
  21. if res.status_code == 200:
  22. access_token = res.json().get("access_token")
  23. self.access_token = access_token
  24. return access_token
  25. return None
  26. def upload_media(self,imgpath):
  27. """
  28. """
  29. access_token = self.get_access_token()
  30. url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (access_token,"image")
  31. files = {"media":(imgpath,open(imgpath,"rb"),"image/png")}
  32. res = requests.post(url,files=files)
  33. if res.status_code == 200:
  34. return res.json().get("media_id")
  35. return None
  36. def upload_material_media(self,imgpath):
  37. """
  38. """
  39. access_token = self.get_access_token()
  40. url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s" % (access_token,"image")
  41. files = {"media":(imgpath,open(imgpath,"rb"),"image/png")}
  42. res = requests.post(url,files=files)
  43. if res.status_code == 200:
  44. return res.json().get("media_id")
  45. return None
  46. def upload_material_media_from_url(self,url):
  47. """
  48. """
  49. if not self.access_token:
  50. access_token = self.get_access_token()
  51. else:
  52. access_token = self.access_token
  53. print access_token
  54. imgname = os.path.split(url)[-1]
  55. imgobj = requests.get(url).content
  56. #print imgobj
  57. url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=%s" % (access_token,"image")
  58. files = {"media":(imgname,imgobj,"image/png")}
  59. res = requests.post(url,files=files)
  60. print res.json()
  61. if res.status_code == 200:
  62. return res.json().get("media_id")
  63. return None
  64. def upload_media_news(self,articles):
  65. """
  66. """
  67. if not self.access_token:
  68. access_token = self.get_access_token()
  69. else:
  70. access_token = self.access_token
  71. headers = {"Content-Type":"application/json;charset=utf-8"}
  72. url = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s" % (access_token)
  73. res = requests.post(url,data=json.dumps({"articles":articles},ensure_ascii=False).encode("utf-8"),headers=headers)
  74. print res.json(),3333333333333
  75. if res.status_code == 200:
  76. return res.json().get("media_id")
  77. return None
  78. def add_material_news(self,articles):
  79. """
  80. """
  81. headers = {"Content-Type":"application/json;charset=utf-8"}
  82. access_token = self.get_access_token()
  83. url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s" % (access_token)
  84. res = requests.post(url,data=json.dumps({"articles":articles},ensure_ascii=False).encode("utf-8"),headers=headers)
  85. print res.json()
  86. if res.status_code == 200:
  87. news_media_id = res.json().get("media_id")
  88. if not news_media_id:
  89. print res.json().get("errcode")
  90. if res.json().get("errcode") == 40007:
  91. return None,u"您上传的图片格式微信同步发送无法识别通过!"
  92. return None,res.json().get("errmsg")
  93. return news_media_id,"success"
  94. return None,None
  95. def add_news_curl(self,title,media_id,authod,digest,content):
  96. """
  97. """
  98. headers = {"Content-Type":"application/json;charset=utf-8"}
  99. access_token = self.get_access_token()
  100. url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=%s" % (access_token)
  101. cmd = u'''
  102. 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
  103. ''' % (title,media_id,authod,digest,content,url)
  104. res = os.popen(cmd)
  105. def send_news_toall(self,media_id):
  106. """推送图文消息给所有微信用户
  107. """
  108. headers = {"Content-Type":"application/json;charset=utf-8"}
  109. access_token = self.get_access_token()
  110. url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s" % (access_token)
  111. data = {
  112. "filter":{"is_to_all":True},
  113. "mpnews":{"media_id":media_id},
  114. "msgtype":"mpnews",
  115. "send_ignore_reprint":1
  116. }
  117. res = requests.post(url,data=json.dumps(data,ensure_ascii=False).encode("utf-8"),headers=headers)
  118. print res.json()
  119. if res.status_code == 200:
  120. news_media_id = res.json().get("msg_data_id")
  121. if not news_media_id:
  122. if res.json().get("errcode") == 40113:
  123. return None,u"您上传的图片格式微信同步发送无法识别通过!"
  124. if res.json().get("errcode") == 45028:
  125. return None,u"您今日的微信同步发送限额已超限制!"
  126. return None,res.json().get("errmsg")
  127. return news_media_id,"success"
  128. return None,None
  129. def get_news_analyse_day(self,begin_date,end_date):
  130. """
  131. """
  132. headers = {"Content-Type":"application/json;charset=utf-8"}
  133. access_token = self.get_access_token()
  134. url = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token=%s" % (access_token)
  135. #url = "https://api.weixin.qq.com/datacube/getarticletotal?access_token=%s" % (access_token)
  136. data = {
  137. "begin_date":begin_date,
  138. "end_date":end_date
  139. }
  140. #res = requests.post(url,data=json.dumps(data,ensure_ascii=False).encode("utf-8"),headers=headers)
  141. res = requests.post(url,data=json.dumps(data),headers=headers)
  142. if res.json().get("list"):
  143. return True,res.json().get("list")
  144. return False,res.json()
  145. if __name__ == "__main__":
  146. wxmp = WXMP()
  147. imgpath = "/tmp/testmedia.png"
  148. imgpath = "http://tederenoss.oss-cn-beijing.aliyuncs.com/zky/upload/1631503610565.jpeg"
  149. #media_id = wxmp.upload_material_media_from_url(imgpath)
  150. #print media_id
  151. #media_id = wxmp.upload_material_media(imgpath)
  152. #articles = [{
  153. # "title":u"测试图文消息",
  154. # "thumb_media_id":media_id,
  155. # "authod":u"四川环宇星创科技有限公司",
  156. # "digest":u"测试图文消息",
  157. # "show_cover_pic":1,
  158. # "content":u"测试发送图文消息"
  159. #},{
  160. # "title":u"测试图文消息111111测试图文消息111111测试图文消息111111测试图文消息111111",
  161. # "thumb_media_id":media_id,
  162. # "authod":u"四川环宇星创科技有限公司",
  163. # "digest":u"测试图文消息",
  164. # "show_cover_pic":1,
  165. # "content":u"测试发送图文消息333"
  166. #}]
  167. #news_media_id = wxmp.add_material_news(articles)
  168. #wxmp.send_news_toall(news_media_id)
  169. begin_date,end_date = "2021-10-12","2021-10-12"
  170. import pprint
  171. pprint.pprint(wxmp.get_news_analyse_day(begin_date,end_date))