material.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. from library.wechatpy.utils import json
  4. from library.wechatpy.client.api.base import BaseWeChatAPI
  5. class WeChatMaterial(BaseWeChatAPI):
  6. def add_articles(self, articles):
  7. """
  8. 新增永久图文素材
  9. 详情请参考
  10. https://mp.weixin.qq.com/wiki?id=mp1494572718_WzHIY
  11. :param articles: 图文素材数组
  12. :type articles: list[dict]
  13. :return: 返回的 JSON 数据包
  14. """
  15. articles_data = []
  16. for article in articles:
  17. articles_data.append({
  18. 'thumb_media_id': article['thumb_media_id'],
  19. 'title': article['title'],
  20. 'content': article['content'],
  21. 'author': article.get('author', ''),
  22. 'content_source_url': article.get('content_source_url', ''),
  23. 'digest': article.get('digest', ''),
  24. 'show_cover_pic': article.get('show_cover_pic', 0),
  25. 'need_open_comment': int(article.get('need_open_comment', False)),
  26. 'only_fans_can_comment': int(article.get('only_fans_can_comment', False)),
  27. })
  28. return self._post(
  29. 'material/add_news',
  30. data={
  31. 'articles': articles_data
  32. }
  33. )
  34. def add(self, media_type, media_file, title=None, introduction=None):
  35. """
  36. 新增其它类型永久素材
  37. 详情请参考
  38. http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html
  39. :param media_type: 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
  40. :param media_file: 要上传的文件,一个 File-object
  41. :param title: 视频素材标题,仅上传视频素材时需要
  42. :param introduction: 视频素材简介,仅上传视频素材时需要
  43. :return: 返回的 JSON 数据包
  44. """
  45. params = {
  46. 'access_token': self.access_token,
  47. 'type': media_type
  48. }
  49. if media_type == 'video':
  50. assert title, 'Video title must be set'
  51. assert introduction, 'Video introduction must be set'
  52. description = {
  53. 'title': title,
  54. 'introduction': introduction
  55. }
  56. params['description'] = json.dumps(description)
  57. return self._post(
  58. 'material/add_material',
  59. params=params,
  60. files={
  61. 'media': media_file
  62. }
  63. )
  64. def get(self, media_id):
  65. """
  66. 获取永久素材
  67. 详情请参考
  68. http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html
  69. :param media_id: 素材的 media_id
  70. :return: 图文素材返回图文列表,其它类型为素材的内容
  71. """
  72. def _processor(res):
  73. if isinstance(res, dict):
  74. if 'news_item' in res:
  75. # 图文素材
  76. return res['news_item']
  77. return res
  78. res = self._post(
  79. 'material/get_material',
  80. data={
  81. 'media_id': media_id
  82. },
  83. result_processor=_processor
  84. )
  85. return res
  86. def delete(self, media_id):
  87. """
  88. 删除永久素材
  89. 详情请参考
  90. http://mp.weixin.qq.com/wiki/5/e66f61c303db51a6c0f90f46b15af5f5.html
  91. :param media_id: 素材的 media_id
  92. :return: 返回的 JSON 数据包
  93. """
  94. return self._post(
  95. 'material/del_material',
  96. data={
  97. 'media_id': media_id
  98. }
  99. )
  100. def update_article(self, media_id, index, article):
  101. """
  102. 修改永久图文素材
  103. 详情请参考
  104. https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1444738732
  105. :param media_id: 要修改的图文消息的 id
  106. :param index: 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为 0
  107. :param article: 图文素材
  108. :return: 返回的 JSON 数据包
  109. """
  110. article_data = {
  111. 'thumb_media_id': article['thumb_media_id'],
  112. 'title': article['title'],
  113. 'content': article['content'],
  114. 'author': article.get('author', ''),
  115. 'content_source_url': article.get('content_source_url', ''),
  116. 'digest': article.get('digest', ''),
  117. 'show_cover_pic': article.get('show_cover_pic', 0)
  118. }
  119. return self._post(
  120. 'material/update_news',
  121. data={
  122. 'media_id': media_id,
  123. 'index': index,
  124. 'articles': article_data
  125. }
  126. )
  127. def update_articles(self, media_id, index, articles):
  128. """
  129. 修改永久图文素材
  130. 详情请参考
  131. http://mp.weixin.qq.com/wiki/4/19a59cba020d506e767360ca1be29450.html
  132. :param media_id: 要修改的图文消息的 id
  133. :param index: 要更新的文章在图文消息中的位置(多图文消息时,此字段才有意义),第一篇为 0
  134. :param articles: 图文素材数组
  135. :return: 返回的 JSON 数据包
  136. """
  137. return self.update_article(media_id, index, articles[index])
  138. def batchget(self, media_type, offset=0, count=20):
  139. """
  140. 批量获取永久素材列表
  141. 详情请参考
  142. http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html
  143. :param media_type: 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(news)
  144. :param offset: 从全部素材的该偏移位置开始返回,0 表示从第一个素材返回
  145. :param count: 返回素材的数量,取值在1到20之间
  146. :return: 返回的 JSON 数据包
  147. """
  148. return self._post(
  149. 'material/batchget_material',
  150. data={
  151. 'type': media_type,
  152. 'offset': offset,
  153. 'count': count
  154. }
  155. )
  156. def get_count(self):
  157. """
  158. 获取素材总数
  159. 详情请参考
  160. http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html
  161. :return: 返回的 JSON 数据包
  162. """
  163. return self._get('material/get_materialcount')
  164. def open_comment(self, msg_data_id, index=1):
  165. """
  166. 打开已群发文章评论
  167. https://mp.weixin.qq.com/wiki?id=mp1494572718_WzHIY
  168. """
  169. return self._post(
  170. 'comment/open',
  171. data={
  172. 'msg_data_id': msg_data_id,
  173. 'index': index,
  174. })
  175. def close_comment(self, msg_data_id, index=1):
  176. """
  177. 关闭已群发文章评论
  178. """
  179. return self._post(
  180. 'comment/close',
  181. data={
  182. 'msg_data_id': msg_data_id,
  183. 'index': index,
  184. })
  185. def list_comment(self, msg_data_id, index=1, begin=0, count=50, type=0):
  186. """
  187. 查看指定文章的评论数据
  188. """
  189. return self._post(
  190. 'comment/list',
  191. data={
  192. 'msg_data_id': msg_data_id,
  193. 'index': index,
  194. 'begin': begin,
  195. 'count': count,
  196. 'type': type
  197. })
  198. def markelect_comment(self, msg_data_id, index, user_comment_id):
  199. """
  200. 将评论标记精选
  201. """
  202. return self._post(
  203. 'comment/markelect',
  204. data={
  205. 'msg_data_id': msg_data_id,
  206. 'index': index,
  207. 'user_comment_id': user_comment_id,
  208. })
  209. def unmarkelect_comment(self, msg_data_id, index, user_comment_id):
  210. """
  211. 将评论取消精选
  212. """
  213. return self._post(
  214. 'comment/unmarkelect',
  215. data={
  216. 'msg_data_id': msg_data_id,
  217. 'index': index,
  218. 'user_comment_id': user_comment_id,
  219. })
  220. def delete_comment(self, msg_data_id, index, user_comment_id):
  221. """
  222. 删除评论
  223. """
  224. return self._post(
  225. 'comment/delete',
  226. data={
  227. 'msg_data_id': msg_data_id,
  228. 'index': index,
  229. 'user_comment_id': user_comment_id,
  230. })
  231. def add_reply_comment(self, msg_data_id, index, user_comment_id, content):
  232. """
  233. 回复评论
  234. """
  235. return self._post(
  236. 'comment/reply/add',
  237. data={
  238. 'msg_data_id': msg_data_id,
  239. 'index': index,
  240. 'user_comment_id': user_comment_id,
  241. 'content': content
  242. })
  243. def delete_reply_comment(self, msg_data_id, index, user_comment_id):
  244. """
  245. 删除回复
  246. """
  247. return self._post(
  248. 'comment/reply/delete',
  249. data={
  250. 'msg_data_id': msg_data_id,
  251. 'index': index,
  252. 'user_comment_id': user_comment_id,
  253. })