-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.py
More file actions
executable file
·159 lines (127 loc) · 5.36 KB
/
Copy pathpayload.py
File metadata and controls
executable file
·159 lines (127 loc) · 5.36 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
import xml_objects
import base64
import StringIO
import gzip
from elementtree.ElementTree import *
class Payload(object):
"""Gnip Payload container class
A Payload represents the payload information in a Gnip Activity.
"""
def __init__(self, title=None, body=None, media_urls=None, raw=None):
"""Initialize the class.
@type title string
@param title Activity title
@type body string
@param body Activity body
@type media_urls List of URLs
@param media_urls MediaURLs associated with the activity
@type raw raw data of a payload that will be compressed and encoded
@param raw Raw text of activity
"""
self.title = title
self.body = body
self.media_urls = media_urls
self.write_raw(raw)
def read_raw(self):
"""Return the decoded and uncompressed raw value from a payload
@return string
"""
if self.__raw is None:
return None
else:
return self.__decompress_with_gzip(self.__decode(self.__raw))
def write_raw(self, raw):
"""Set the raw for the payload.
@type raw string
@param raw string will be compressed and encoded.
"""
if raw is None:
self.__raw = None
else:
self.__raw = self.__encode(self.__compress_with_gzip(raw))
def from_xml_node(self, payload_xml_node):
""" Populates payload from a payload xml node
@type payload_xml_node Element
@param payload_xml_node an Element representing the Payload for an Activity.
"""
if payload_xml_node is not None:
title_node = payload_xml_node.find("title")
if title_node is not None:
self.title = title_node.text
else:
self.title = None
body_node = payload_xml_node.find("body")
if body_node is not None:
self.body = body_node.text
else:
self.body = None
media_url_nodes = payload_xml_node.findall("mediaURL")
if media_url_nodes is not None:
self.media_urls = []
for media_url_node in media_url_nodes:
media_url = xml_objects.MediaURL(value=media_url_node.text,
height=media_url_node.get("height"),
width=media_url_node.get("width"),
duration=media_url_node.get("duration"),
type=media_url_node.get("type"),
mimetype=media_url_node.get("mimeType")
)
self.media_urls.append(media_url)
else:
self.media_urls = None
raw_node = payload_xml_node.find("raw")
self.__raw = raw_node.text
def to_xml_node(self):
""" Return a XML representation of this object
@return string containing XML representation of the Payload
Returns a XML representation of this object.
"""
payload_node = Element("payload")
if self.title is not None:
title_node = Element("title")
title_node.text = self.title
payload_node.append(title_node)
if self.body is not None:
body_node = Element("body")
body_node.text = self.body
payload_node.append(body_node)
if self.media_urls is not None and len(self.media_urls) > 0:
for media_url in self.media_urls:
media_url_node = Element("mediaURL")
media_url_node.text = media_url.value
if media_url.height is not None:
media_url_node.set("height", media_url.height)
if media_url.width is not None:
media_url_node.set("width", media_url.width)
if media_url.duration is not None:
media_url_node.set("duration", media_url.duration)
if media_url.type is not None:
media_url_node.set("type", media_url.type)
if media_url.mimetype is not None:
media_url_node.set("mimeType", media_url.mimetype)
payload_node.append(media_url_node)
if self.__raw is not None:
raw_node = Element("raw")
raw_node.text = self.__raw
payload_node.append(raw_node)
return payload_node
def __encode(self, string):
return base64.b64encode(string)
def __decode(self, string):
return base64.b64decode(string)
def __compress_with_gzip(self, string):
zbuf = StringIO.StringIO()
zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
zfile.write(string)
zfile.close()
return zbuf.getvalue()
def __decompress_with_gzip(self, compresseddata):
zbuf = StringIO.StringIO(compresseddata)
zfile = gzip.GzipFile(fileobj=zbuf)
return zfile.read()
def __str__(self):
return "[" + str(self.title) + \
", " + str(self.body) + \
", " + str(self.media_urls) + \
", " + str(self.read_raw()) + \
"]"