wechatpy.client.api.media.WeChatMedia.get_url()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 1
cts 2
cp 0.5
crap 1.125
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3
4 10
from wechatpy.client.api.base import BaseWeChatAPI
5
6
7 10
class WeChatMedia(BaseWeChatAPI):
8
    """ 素材管理
9
10
    https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html
11
    """
12
13 10
    def upload(self, media_type, media_file):
14
        """
15
        新增临时素材
16
        详情请参考
17
        https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html
18
19
        :param media_type: 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
20
        :param media_file: 要上传的文件,一个 File-object
21
22
        :return: 返回的 JSON 数据包
23
        """
24 10
        return self._post(url='media/upload', params={'type': media_type}, files={'media': media_file})
25
26 10
    def download(self, media_id):
27
        """
28
        获取临时素材
29
        详情请参考
30
        https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_temporary_materials.html
31
32
        :param media_id: 媒体文件 ID
33
34
        :return: requests 的 Response 实例
35
        """
36
        return self._get('media/get', params={'media_id': media_id})
37
38 10
    def get_url(self, media_id):
39
        """
40
        获取临时素材下载地址
41
42
        :param media_id: 媒体文件 ID
43
        :return: 临时素材下载地址
44
        """
45
        return 'https://api.weixin.qq.com/cgi-bin/media/get' \
46
               '?access_token={}&media_id={}'.format(self.access_token, media_id)
47
48 10
    def upload_video(self, media_id, title, description):
49
        """
50
        群发视频消息时获取视频 media_id
51
        详情请参考
52
        http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html
53
54
        :param media_id: 需通过基础支持中的上传下载多媒体文件 :func:`upload` 来得到
55
        :param title: 视频标题
56
        :param description: 视频描述
57
58
        :return: 返回的 JSON 数据包
59
        """
60
        return self._post(
61
            url='media/uploadvideo',
62
            data={
63
                'media_id': media_id,
64
                'title': title,
65
                'description': description
66
            }
67
        )
68
69 10 View Code Duplication
    def upload_articles(self, articles):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
70
        """
71
        上传图文消息素材
72
        详情请参考
73
        http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html
74
75
        :param articles: 图文消息数组
76
        :return: 返回的 JSON 数据包
77
        """
78
        articles_data = []
79
        for article in articles:
80
            articles_data.append({
81
                'thumb_media_id': article['thumb_media_id'],
82
                'title': article['title'],
83
                'content': article['content'],
84
                'author': article.get('author', ''),
85
                'content_source_url': article.get('content_source_url', ''),
86
                'digest': article.get('digest', ''),
87
                'show_cover_pic': article.get('show_cover_pic', 0)
88
            })
89
        return self._post(
90
            'media/uploadnews',
91
            data={
92
                'articles': articles_data
93
            }
94
        )
95
96 10
    def upload_image(self, media_file):
97
        """
98
        上传群发消息内的图片
99
        详情请参考
100
        https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
101
102
        :param media_file: 要上传的文件,一个 File-object
103
        :return: 上传成功时返回图片 URL
104
        """
105 10
        res = self._post(
106
            url='media/uploadimg',
107
            files={
108
                'media': media_file
109
            },
110
            result_processor=lambda x: x['url']
111
        )
112 10
        return res
113
114
    upload_mass_image = upload_image
115