-
Notifications
You must be signed in to change notification settings - Fork 1
/
receiveIFTTT.py
230 lines (210 loc) · 8.56 KB
/
receiveIFTTT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
from google.appengine.api import memcache
import logging
import json
import userinfo_utility
import send2Line
import utility
import uuid
import urllib
import re
def receiveExec_Text(self):
logging.debug(self.request.headers)
logging.debug(self.request.body)
if (self.request.headers['Content-Type'] == 'text/plain'):
jsonstr = self.request.body
strArr = jsonstr.split("\\\\")
to = strArr[0].strip()
text = "\\\\".join(strArr[1:]);
text = text.lstrip('<.').rstrip('>').strip()
else:
jsonstr = self.request.body
logging.debug(jsonstr)
jsonstr = jsonstr.replace('\r', '')
jsonstr = jsonstr.replace('\n', '\\n')
logging.debug(jsonstr)
jsonobj = json.loads(jsonstr)
logging.debug(jsonobj)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Received!')
to = str(jsonobj[u"to"])
text = jsonobj[u"text"].lstrip('<.').rstrip('>').strip()
if not userinfo_utility.isExistUserData(to):
return
send2Line.sendText(to, text)
def receiveExec_Image(self):
if (self.request.headers['Content-Type'] == 'text/plain'):
jsonstr = self.request.body
else:
jsonstr = self.request.body
logging.debug(jsonstr)
jsonobj = json.loads(jsonstr)
logging.debug(jsonobj)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Received!')
originalContentUrl = jsonobj[u"originalContentUrl"]
picture_key = utility.getHash(originalContentUrl)
encodeURL = urllib.quote(originalContentUrl)
originalContentUrl_encode = self.request.host_url + "/originals/" + picture_key + "/" + encodeURL
previewImageUrl = ""
if jsonobj.has_key(u"previewImageUrl"):
previewImageUrl = jsonobj[u"previewImageUrl"]
if previewImageUrl == "":
previewImageUrl = self.request.host_url + "/thumbnail/" + picture_key + "/" + encodeURL
if not userinfo_utility.isExistUserData(str(jsonobj[u"to"])):
return
send2Line.sendImage(str(jsonobj[u"to"]), originalContentUrl_encode, previewImageUrl)
def receiveExec_Video(self):
if (self.request.headers['Content-Type'] == 'text/plain'):
jsonstr = self.request.body
else:
jsonstr = self.request.body
logging.debug(jsonstr)
jsonobj = json.loads(jsonstr)
logging.debug(jsonobj)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Received!')
previewImageUrl = ""
if jsonobj.has_key(u"previewImageUrl"):
previewImageUrl = jsonobj[u"previewImageUrl"]
if previewImageUrl == "":
previewImageUrl = "https://ifttt-line.appspot.com/images/preview_image.jpg?" + str(uuid.uuid4())
if not userinfo_utility.isExistUserData(str(jsonobj[u"to"])):
return
send2Line.sendVideo(str(jsonobj[u"to"]), jsonobj[u"originalContentUrl"], previewImageUrl)
def receiveExec_Audio(self):
if (self.request.headers['Content-Type'] == 'text/plain'):
jsonstr = self.request.body
else:
jsonstr = self.request.body
logging.debug(jsonstr)
jsonobj = json.loads(jsonstr)
logging.debug(jsonobj)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Received!')
audioLen = 180000
if jsonobj.has_key(u"AUDLEN") and isinstance(jsonobj.has_key(u"AUDLEN"), int):
audioLen = jsonobj[u"AUDLEN"]
if audioLen == "":
audioLen = 180000
if not userinfo_utility.isExistUserData(str(jsonobj[u"to"])):
return
send2Line.sendAudio(str(jsonobj[u"to"]), jsonobj[u"originalContentUrl"], audioLen)
def receiveExec_Location(self):
logging.debug(self.request.headers)
if (self.request.headers['Content-Type'] == 'text/plain'):
jsonstr = self.request.body
else:
jsonstr = self.request.body
logging.debug(jsonstr)
jsonobj = json.loads(jsonstr)
logging.debug(jsonobj)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Received!')
latitude = 0;
longitude = 0;
title = "";
address = "";
if jsonobj.has_key("LocationMapUrl"):
url = jsonobj["LocationMapUrl"]
logging.debug(url)
url = url.split(" ")[0]
url = utility.getRealURL(url)
logging.debug(url)
r = re.compile("center=(-?[0-9]+\.?[0-9]*),(-?[0-9]+\.?[0-9]*)")
m = r.search(url)
if m != None:
latitude = m.group(1)
longitude = m.group(2)
r = re.compile("ll=(-?[0-9]+\.?[0-9]*),(-?[0-9]+\.?[0-9]*)")
m = r.search(url)
if m != None:
latitude = m.group(1)
longitude = m.group(2)
r = re.compile("q=(-?[0-9]+\.?[0-9]*),(-?[0-9]+\.?[0-9]*)")
m = r.search(url)
if m != None:
latitude = m.group(1)
longitude = m.group(2)
r = re.compile("\/maps\/search\/(-?[0-9]+\.?[0-9]*),(-?[0-9]+\.?[0-9]*)")
m = r.search(url)
if m != None:
latitude = m.group(1)
longitude = m.group(2)
if jsonobj.has_key("latitude"):
latitude = jsonobj["latitude"];
if jsonobj.has_key("longitude"):
longitude = jsonobj["longitude"];
if jsonobj.has_key("title"):
title = jsonobj["title"];
if jsonobj.has_key("address"):
address = jsonobj["address"];
if title == "":
title_json = utility.getAddress(latitude, longitude)
if len(title_json) > 0:
title = title_json[0]["formatted_address"]
title = title.encode('utf_8')
if title == "":
title = "位置情報"
if address == "":
address = "位置情報"
if not userinfo_utility.isExistUserData(str(jsonobj[u"to"])):
return
send2Line.sendLocation(str(jsonobj[u"to"]), address, title, latitude, longitude)
def receiveExec_Sticker(self):
if (self.request.headers['Content-Type'] == 'text/plain'):
jsonstr = self.request.body
else:
jsonstr = self.request.body
logging.debug(jsonstr)
jsonobj = json.loads(jsonstr)
logging.debug(jsonobj)
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Received!')
STKID = 0
STKPKGID = 0
STKVER = 0
if jsonobj.has_key("ContentUrl"):
url = jsonobj["ContentUrl"]
url = utility.getRealURL(url)
r = re.compile("\/products\/0\/0\/([0-9]+)\/([0-9]+)\/android\/stickers\/([0-9]+).png")
m = r.search(url)
if m != None:
STKVER = m.group(1)
STKPKGID = m.group(2)
STKID = m.group(3)
if jsonobj.has_key("STKVER"):
STKVER = jsonobj[u"STKVER"]
if jsonobj.has_key("STKPKGID"):
STKPKGID = jsonobj[u"STKPKGID"]
if jsonobj.has_key("STKID"):
STKID = jsonobj[u"STKID"]
if not userinfo_utility.isExistUserData(str(jsonobj[u"to"])):
return
send2Line.sendSticker(str(jsonobj[u"to"]), STKID, STKPKGID, STKVER)
# def receiveExec_MultiMessage(self):
# if(self.request.headers['Content-Type']=='text/plain'):
# jsonstr = self.request.body
# else:
# jsonstr = self.request.body
# logging.debug(jsonstr)
# jsonobj = json.loads(jsonstr)
# logging.debug(jsonobj)
# self.response.headers['Content-Type'] = 'text/plain'
# self.response.out.write('Received!')
# if not userinfo_utility.isExistUserData(str(jsonobj[u"to"])):
# return
# send2Line.sendMultiMessage( [str(jsonobj[u"to"])],jsonobj[u"messages"] )
# def receiveExec_RichMessage(self):
# if(self.request.headers['Content-Type']=='text/plain'):
# jsonstr = self.request.body
# else:
# jsonstr = self.request.body
# logging.debug(jsonstr)
# jsonobj = json.loads(jsonstr)
# logging.debug(jsonobj)
# self.response.headers['Content-Type'] = 'text/plain'
# self.response.out.write('Received!')
# if not userinfo_utility.isExistUserData(str(jsonobj[u"to"])):
# return
# send2Line.sendRichMessage( [str(jsonobj[u"to"])],jsonobj[u"MARKUP_JSON"] )