material.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # encoding: utf-8
  2. from __future__ import absolute_import, unicode_literals
  3. import requests
  4. from wechatpy.client.api.base import BaseWeChatAPI
  5. class WeChatMaterial(BaseWeChatAPI):
  6. def add_articles(self, articles):
  7. """
  8. 新增永久图文素材
  9. 详情请参考
  10. https://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
  11. :param articles: 图文素材数组
  12. :return: 返回的 JSON 数据包
  13. """
  14. articles_data = []
  15. for article in articles:
  16. articles_data.append({
  17. 'thumb_media_id': article['thumb_media_id'],
  18. 'title': article['title'],
  19. 'content': article['content'],
  20. 'author': article.get('author', ''),
  21. 'content_source_url': article.get('content_source_url', ''),
  22. 'digest': article.get('digest', ''),
  23. 'show_cover_pic': article.get('show_cover_pic', 0)
  24. })
  25. return self._post(
  26. 'material/add_mpnews',
  27. data={
  28. "mpnews": {
  29. "articles": articles_data
  30. }
  31. }
  32. )
  33. def add(self, agent_id, media_type, media_file):
  34. """
  35. 新增其它类型永久素材
  36. 详情请参考
  37. https://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
  38. :param agent_id: 企业应用的id
  39. :param media_type: 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)普通文件(file)
  40. :param media_file: 要上传的文件,一个 File-object
  41. :return: 返回的 JSON 数据包
  42. """
  43. params = {
  44. 'agentid': agent_id,
  45. 'type': media_type,
  46. }
  47. return self._post(
  48. url='material/add_material',
  49. params=params,
  50. files={
  51. 'media': media_file
  52. }
  53. )
  54. def get_url(self, agent_id, media_id):
  55. """
  56. 获取永久素材下载地址
  57. 详情请参考
  58. https://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
  59. :param agent_id: 企业应用的id
  60. :param media_id: 媒体文件 ID
  61. :return: 临时素材下载地址
  62. """
  63. parts = (
  64. 'https://qyapi.weixin.qq.com/cgi-bin/material/get',
  65. '?access_token=',
  66. self.access_token,
  67. '&media_id=',
  68. media_id,
  69. '&agentid=',
  70. agent_id,
  71. )
  72. return ''.join(parts)
  73. def get(self, agent_id, media_id):
  74. """
  75. 获取永久素材
  76. 详情请参考
  77. https://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
  78. :param agent_id: 企业应用的id
  79. :param media_id: 媒体文件 ID
  80. :return: requests 的 Response 实例
  81. """
  82. return requests.get(self.get_url(agent_id, media_id))
  83. def get_articles(self, agent_id, media_id):
  84. """
  85. 获取永久素材:图文消息素材
  86. 详情请参考
  87. https://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
  88. :param agent_id: 企业应用的id
  89. :param media_id: 媒体文件 ID
  90. :return: 返回的 JSON 数据包
  91. """
  92. return self._get(
  93. 'material/get',
  94. params={
  95. 'agentid': agent_id,
  96. 'media_id': media_id,
  97. }
  98. )
  99. def delete(self, agent_id, media_id):
  100. """
  101. 删除永久素材
  102. 详情请参考
  103. https://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%A0%E9%99%A4%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90
  104. :param agent_id: 企业应用的id
  105. :param media_id: 媒体文件 ID
  106. :return: 返回的 JSON 数据包
  107. """
  108. return self._get(
  109. 'material/del',
  110. params={
  111. 'agentid': agent_id,
  112. 'media_id': media_id,
  113. }
  114. )
  115. def update_articles(self, agent_id, media_id, articles):
  116. """
  117. 修改永久图文素材
  118. 详情请参考
  119. https://qydev.weixin.qq.com/wiki/index.php?title=%E4%BF%AE%E6%94%B9%E6%B0%B8%E4%B9%85%E5%9B%BE%E6%96%87%E7%B4%A0%E6%9D%90
  120. :param media_id: 要修改的图文消息的 id
  121. :param index: 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为 0
  122. :param articles: 图文素材数组
  123. :return: 返回的 JSON 数据包
  124. """
  125. articles_data = []
  126. for article in articles:
  127. articles_data.append({
  128. 'thumb_media_id': article['thumb_media_id'],
  129. 'title': article['title'],
  130. 'content': article['content'],
  131. 'author': article.get('author', ''),
  132. 'content_source_url': article.get('content_source_url', ''),
  133. 'digest': article.get('digest', ''),
  134. 'show_cover_pic': article.get('show_cover_pic', 0)
  135. })
  136. return self._post(
  137. 'material/update_news',
  138. data={
  139. 'agentid': agent_id,
  140. 'media_id': media_id,
  141. 'articles': articles_data
  142. }
  143. )
  144. def get_count(self, agent_id):
  145. """
  146. 获取素材总数
  147. 详情请参考
  148. https://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E6%80%BB%E6%95%B0
  149. :param agent_id: 企业应用的id
  150. :return: 返回的 JSON 数据包
  151. """
  152. return self._get(
  153. 'material/get_count',
  154. params={
  155. 'agent_id': agent_id,
  156. }
  157. )
  158. def batchget(self, agent_id, media_type, offset=0, count=20):
  159. """
  160. 批量获取永久素材列表
  161. 详情请参考
  162. https://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E5%88%97%E8%A1%A8
  163. :param agent_id: 企业应用的id
  164. :param media_type: 媒体文件类型,分别有图文(mpnews)、图片(image)、
  165. 语音(voice)、视频(video)和文件(file)
  166. :param offset: 从全部素材的该偏移位置开始返回,0 表示从第一个素材返回
  167. :param count: 返回素材的数量,取值在1到20之间
  168. :return: 返回的 JSON 数据包
  169. """
  170. return self._post(
  171. 'material/batchget',
  172. data={
  173. 'agent_id': agent_id,
  174. 'type': media_type,
  175. 'offset': offset,
  176. 'count': count
  177. }
  178. )