-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluevia_helpers.py
More file actions
199 lines (162 loc) · 7.44 KB
/
bluevia_helpers.py
File metadata and controls
199 lines (162 loc) · 7.44 KB
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
#
# The MIT license
#
# Copyright (C) 2011 by Bernhard Walter ( @bernhard42 )
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
# Version 01.08.2011
#
# # # # # # # # # # # # # # # #
# Simple XML to JSON Class
# # # # # # # # # # # # # # # #
from xml.dom.minidom import parseString
def _parseAdResponse(xmlString):
"""
Parses the XML respponse of a BlueVia Adsvertising requests and constructs a JSON object as result
Internal method
"""
dom = parseString(xmlString)
arId = dom.getElementsByTagName ("NS1:adResponse")[0].attributes.getNamedItem("id").value
arVersion = dom.getElementsByTagName ("NS1:adResponse")[0].attributes.getNamedItem("version").value
adId = dom.getElementsByTagName ("NS1:ad")[0].attributes.getNamedItem("id").value
adPlace = dom.getElementsByTagName ("NS1:ad")[0].attributes.getNamedItem("ad_placement").value
adCampaign = dom.getElementsByTagName ("NS1:ad")[0].attributes.getNamedItem("campaign").value
adFlight = dom.getElementsByTagName ("NS1:ad")[0].attributes.getNamedItem("flight").value
adPresent = dom.getElementsByTagName ("NS1:resource")[0].attributes.getNamedItem("ad_presentation").value
ce = dom.getElementsByTagName ("NS1:creative_element")[0]
ceType = ce.attributes.items()[0]
ceAttrs = [(n.attributes.items(), n.childNodes[0].nodeValue) for n in ce.childNodes if n.nodeName == "NS1:attribute"]
ceInter = dom.getElementsByTagName ("NS1:interaction")[0]
ciType = ceInter.attributes.items()[0]
ciAttrs = [(n.attributes.items(), n.childNodes[0].nodeValue) for n in ceInter.childNodes if n.nodeName == "NS1:attribute"]
creativeElement = {
ceType[0]:ceType[1],
"attributes":[{x[0][0][0]:x[0][0][1],"value":x[1]} for x in ceAttrs],
"interaction":{ciType[0]:ciType[1],
"attributes":[{x[0][0][0]:x[0][0][1],"value":x[1]} for x in ciAttrs]}
}
fullAd = {"adResponse": { \
"id":arId, \
"version":arVersion, \
"ad": { \
"id":adId, \
"ad_place_ment":adPlace, \
"campaign":adCampaign, \
"flight":adFlight, \
"resource":{ \
"ad_representation":adPresent, \
"creative_element": creativeElement}}}}
return fullAd
# # # # # # # # # # # # # # # #
# Simple Multipart POST body
# # # # # # # # # # # # # # # #
import random, string, sys, mimetypes, base64, email, os
def _random_string (length):
return ''.join (random.choice (string.letters) for ii in range (length + 1))
def _encodeMultipart (root, messages = None, files = None):
"""
Encodes messages and files in Multipart MIME format fitting for BlueVia Send MMS API
Internal method
"""
# to comply with blueVia format, some adaptation to email.mime.multipart results would have to be applied
# so just do it manually
boundary1 = _random_string (32)
boundary2 = _random_string (32)
def get_content_type (filename):
return mimetypes.guess_type (filename)[0] or 'application/octet-stream'
def encode_message (num, message, boundary):
return ('--' + boundary,
'Content-Disposition: attachment; name="message%d.txt"' % num,
'Content-Type: text/plain',
'Content-Transfer-Encoding: 8bit;charset=UTF-8',
'', str(message))
def encode_file (field_name, boundary):
filename = files [field_name]
return ('\r\n--' + boundary,
'Content-Disposition: attachment; filename="%s"' % filename,
'Content-Type: %s' % get_content_type(filename),
'Content-Transfer-Encoding: base64',
'', base64.encodestring(open (filename, 'rb').read ()))
lines = ['\r\n--' + boundary1,
'Content-Disposition: form-data; name="root-fields"',
'Content-Type: application/json;charset=UTF-8',
'Content-Transfer-Encoding: 8bit',
'', str(root)]
if messages or files:
att = ('\r\n--' + boundary1,
'Content-Disposition: form-data; name="attachments"',
'Content-Type: multipart/mixed; boundary=%s' % boundary2,
'')
lines.extend(att)
if messages:
i=0
for msg in messages:
lines.extend (encode_message(i, msg, boundary2))
i += 1
if files:
for name in files:
lines.extend (encode_file (name, boundary2))
lines.extend (('--%s--' % boundary2, ''))
lines.extend (('--%s--' % boundary1, ''))
body = '\r\n'.join (lines)
headers = {'content-type': 'multipart/form-data; boundary=' + boundary1,
'content-length': str (len (body))}
return body, headers
def _decodeMultipart(msgId, body):
"""
Decodes the BlueVia Multipart MIME body from retrieving MMS attachemnts into a folder with message and file objects
Internal method
"""
common_ext = { 'image/jpg': '.jpg',
'image/jpeg': '.jpg',
'image/png': '.png',
'application/json': '.json',
'application/xml': '.xml',
'text/plain': '.txt',
'text/html': '.html'
}
result = []
directory = msgId
os.mkdir(directory)
# add the multipart header, unfortunately missing in BlueVia response
boundary = body[:200].split("\r\n")[1][2:]
fullbody = 'Content-Type: multipart/mixed; boundary="%s"\r\n%s\r\n"' % (boundary, body)
msg = email.message_from_string(fullbody)
counter = 1
for part in msg.walk():
# multipart/* are just containers
if part.get_content_maintype() == 'multipart':
continue
filename = part.get_filename()
ext = common_ext.get(part.get_content_type())
if not ext:
ext = mimetypes.guess_extension(part.get_content_type())
if not ext:
ext = '.bin'
if filename:
if not os.path.splitext(filename)[1]:
filename += ext
else:
filename = 'part-%03d%s' % (counter, ext)
counter += 1
fp = open(os.path.join(directory, filename), 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
print "%d files saved to %s" % (counter, msgId)
return (counter, msgId)