Completed
Pull Request — develop (#105)
by
unknown
01:10
created

LocationMessage.location()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
# -*- coding: utf-8 -*-
2
3
import six
4
from entries import *
5
6
MESSAGE_TYPES = {}
7
8
9
class MessageMetaClass(type):
10
    def __new__(mcs, name, bases, attrs):
11
        return type.__new__(mcs, name, bases, attrs)
12
13
    def __init__(cls, name, bases, attrs):
14
        if '__type__' in attrs:
15
            if isinstance(attrs['__type__'], list):
16
                for _type in attrs['__type__']:
17
                    MESSAGE_TYPES[_type] = cls
18
            else:
19
                MESSAGE_TYPES[attrs['__type__']] = cls
20
        type.__init__(cls, name, bases, attrs)
21
22
23
@six.add_metaclass(MessageMetaClass)
24
class WeChatMessage(object):
25
    id = IntEntry('MsgId', 0)
26
    target = StringEntry('ToUserName')
27
    source = StringEntry('FromUserName')
28
    time = IntEntry('CreateTime', 0)
29
30
    def __init__(self, message):
31
        self.__dict__.update(message)
32
33
34
class TextMessage(WeChatMessage):
35
    __type__ = 'text'
36
    content = StringEntry('Content')
37
38
39
class ImageMessage(WeChatMessage):
40
    __type__ = 'image'
41
    img = StringEntry('PicUrl')
42
43
44
class LocationMessage(WeChatMessage):
45
    __type__ = 'location'
46
    location_x = FloatEntry('Location_X')
47
    location_y = FloatEntry('Location_Y')
48
    label = StringEntry('Label')
49
    scale = IntEntry('Scale')
50
51
    @property
52
    def location(self):
53
        return self.location_x, self.location_y
54
55
56
class LinkMessage(WeChatMessage):
57
    __type__ = 'link'
58
    title = StringEntry('Title')
59
    description = StringEntry('Description')
60
    url = StringEntry('Url')
61
62
63
class EventMessage(WeChatMessage):
64
    __type__ = ['event']
65
66
    def __init__(self, message):
67
        message.pop("type")
68
        self.type = message.pop('Event')
69
        self.type = str(self.type).lower()
70
        if self.type == "click":
71
            self.__class__.key = StringEntry('EventKey')
72
        elif self.type == "location":
73
            self.__class__.latitude = FloatEntry('Latitude')
74
            self.__class__.longitude = FloatEntry('Longitude')
75
            self.__class__.precision = FloatEntry('Precision')
76
        super(EventMessage, self).__init__(message)
77
78
79
class VoiceMessage(WeChatMessage):
80
    __type__ = 'voice'
81
    media_id = StringEntry('MediaId')
82
    format = StringEntry('Format')
83
    recognition = StringEntry('Recognition')
84
85
86
class VideoMessage(WeChatMessage):
87
    __type__ = ['video', 'shortvideo']
88
    media_id = StringEntry('MediaId')
89
    thumb_media_id = StringEntry('ThumbMediaId')
90
91
92
class UnknownMessage(WeChatMessage):
93
    __type__ = 'unknown'
94