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