2 from diaspy
.models
import Post
4 """Docstrings for this module are taken from:
5 https://gist.github.com/MrZYX/01c93096c30dc44caf71
7 Documentation for D* JSON API taken from:
8 http://pad.spored.de/ro/r.qWmvhSZg7rk4OQam
12 """Object representing generic stream. Used in Tag(),
13 Stream(), Activity() etc.
15 def __init__(self
, connection
, location
=''):
17 :param connection: Connection() object
18 :type connection: diaspy.connection.Connection
19 :param location: location of json
22 self
._connection
= connection
24 if location
: self
._location
= location
28 def __contains__(self
, post
):
29 """Returns True if stream contains given post.
31 if type(post
) is not Post
:
32 raise TypeError('stream can contain only posts: checked for {0}'.format(type(post
)))
33 return post
in self
._stream
36 """Provides iterable interface for stream.
38 return iter(self
._stream
)
40 def __getitem__(self
, n
):
41 """Returns n-th item in Stream.
43 return self
._stream
[n
]
46 """Returns length of the Stream.
48 return len(self
._stream
)
50 def _setlocation(self
):
51 """Sets location of the stream.
52 Location defaults to 'stream.json'
54 NOTICE: inheriting objects should override this method
55 and set their own value to the `location`.
56 However, it is possible to override default location by
57 passing the desired one to the constructor.
60 def _setlocation(self):
61 self._location = 'foo.json'
64 :param location: url of the stream
69 self
._location
= 'stream.json'
72 """Obtains stream from pod.
74 request
= self
._connection
.get(self
._location
)
75 if request
.status_code
!= 200:
76 raise Exception('wrong status code: {0}'.format(request
.status_code
))
77 return [Post(str(post
['id']), self
._connection
) for post
in request
.json()]
80 """Removes all posts from stream.
85 """Removes all unexistent posts from stream.
88 for post
in self
._stream
:
96 if not deleted
: stream
.append(post
)
102 new_stream
= self
._obtain
()
103 ids
= [post
.post_id
for post
in self
._stream
]
105 stream
= self
._stream
106 for i
in range(len(new_stream
)):
107 if new_stream
[-i
].post_id
not in ids
:
108 stream
= [new_stream
[-i
]] + stream
109 ids
.append(new_stream
[-i
].post_id
)
111 self
._stream
= stream
114 """Fills the stream with posts.
116 self
._stream
= self
._obtain
()
119 class Outer(Generic
):
120 """Object used by diaspy.models.User to represent
121 stream of other user.
124 """Obtains stream of other user.
126 request
= self
._connection
.get(self
._location
)
127 if request
.status_code
!= 200:
128 raise Exception('wrong status code: {0}'.format(request
.status_code
))
129 return [Post(str(post
['id']), self
._connection
) for post
in request
.json()]
132 class Stream(Generic
):
133 """The main stream containing the combined posts of the
134 followed users and tags and the community spotlights posts
135 if the user enabled those.
137 def _setlocation(self
):
138 self
._location
= 'stream.json'
140 def post(self
, text
='', aspect_ids
='public', photos
=None, photo
=''):
141 """This function sends a post to an aspect.
142 If both `photo` and `photos` are specified `photos` takes precedence.
144 :param text: Text to post.
146 :param aspect_ids: Aspect ids to send post to.
147 :type aspect_ids: str
148 :param photo: filename of photo to post
150 :param photos: id of photo to post (obtained from _photoupload())
153 :returns: diaspy.models.Post -- the Post which has been created
156 data
['aspect_ids'] = aspect_ids
157 data
['status_message'] = {'text': text
}
158 if photo
: data
['photos'] = self
._photoupload
(photo
)
159 if photos
: data
['photos'] = photos
161 request
= self
._connection
.post('status_messages',
162 data
=json
.dumps(data
),
163 headers
={'content-type': 'application/json',
164 'accept': 'application/json',
165 'x-csrf-token': self
._connection
.get_token()})
166 if request
.status_code
!= 201:
167 raise Exception('{0}: Post could not be posted.'.format(request
.status_code
))
169 post
= Post(str(request
.json()['id']), self
._connection
)
172 def _photoupload(self
, filename
):
173 """Uploads picture to the pod.
175 :param filename: path to picture file
178 :returns: id of the photo being uploaded
180 data
= open(filename
, 'rb')
185 params
['photo[pending]'] = 'true'
186 params
['set_profile_image'] = ''
187 params
['qqfile'] = filename
188 aspects
= self
._connection
.getUserInfo()['aspects']
189 for i
, aspect
in enumerate(aspects
):
190 params
['photo[aspect_ids][{0}]'.format(i
)] = aspect
['id']
192 headers
= {'content-type': 'application/octet-stream',
193 'x-csrf-token': self
._connection
.get_token(),
194 'x-file-name': filename
}
196 request
= self
._connection
.post('photos', data
=image
, params
=params
, headers
=headers
)
197 if request
.status_code
!= 200:
198 raise Exception('wrong error code: {0}'.format(request
.status_code
))
199 return request
.json()['data']['photo']['id']
202 class Activity(Generic
):
203 """Stream representing user's activity.
205 def _setlocation(self
):
206 self
._location
= 'activity.json'
208 def _delid(self
, id):
209 """Deletes post with given id.
212 for p
in self
._stream
:
216 if post
is not None: post
.delete()
218 def delete(self
, post
):
219 """Deletes post from users activity.
220 `post` can be either post id or Post()
221 object which will be identified and deleted.
222 After deleting post the stream will be filled.
224 :param post: post identifier
225 :type post: str, diaspy.models.Post
227 if type(post
) == str: self
._delid
(post
)
228 elif type(post
) == Post
: post
.delete()
230 raise TypeError('this method accepts only int, str or Post: {0} given')
234 class Aspects(Generic
):
235 """This stream contains the posts filtered by
236 the specified aspect IDs. You can choose the aspect IDs with
237 the parameter `aspect_ids` which value should be
238 a comma seperated list of aspect IDs.
239 If the parameter is ommitted all aspects are assumed.
240 An example call would be `aspects.json?aspect_ids=23,5,42`
242 def _setlocation(self
):
243 self
._location
= 'aspects.json'
245 def add(self
, aspect_name
, visible
=0):
246 """This function adds a new aspect.
248 data
= {'authenticity_token': self
._connection
.get_token(),
249 'aspect[name]': aspect_name
,
250 'aspect[contacts_visible]': visible
}
252 r
= self
._connection
.post('aspects', data
=data
)
253 if r
.status_code
!= 200:
254 raise Exception('wrong status code: {0}'.format(r
.status_code
))
256 def remove(self
, aspect_id
):
257 """This method removes an aspect.
259 data
= {'authenticity_token': self
.connection
.get_token()}
260 r
= self
.connection
.delete('aspects/{}'.format(aspect_id
),
262 if r
.status_code
!= 404:
263 raise Exception('wrong status code: {0}'.format(r
.status_code
))
266 class Commented(Generic
):
267 """This stream contains all posts
268 the user has made a comment on.
270 def _setlocation(self
):
271 self
._location
= 'commented.json'
274 class Liked(Generic
):
275 """This stream contains all posts the user liked.
277 def _setlocation(self
):
278 self
._location
= 'liked.json'
281 class Mentions(Generic
):
282 """This stream contains all posts
283 the user is mentioned in.
285 def _setlocation(self
):
286 self
._location
= 'mentions.json'
289 class FollowedTags(Generic
):
290 """This stream contains all posts
291 containing tags the user is following.
293 def _setlocation(self
):
294 self
._location
= 'followed_tags.json'
296 def remove(self
, tag_id
):
297 """Stop following a tag.
299 :param tag_id: tag id
302 data
= {'authenticity_token':self
._connection
.get_token()}
303 request
= self
._connection
.delete('tag_followings/{0}'.format(tag_id
), data
=data
)
304 if request
.status_code
!= 404:
305 raise Exception('wrong status code: {0}'.format(request
.status_code
))
307 def add(self
, tag_name
):
309 Error code 403 is accepted because pods respod with it when request
310 is sent to follow a tag that a user already follows.
312 :param tag_name: tag name
314 :returns: int (response code)
316 data
= {'name':tag_name
,
317 'authenticity_token':self
._connection
.get_token(),
319 headers
={'content-type': 'application/json',
320 'x-csrf-token': self
._connection
.get_token(),
321 'accept': 'application/json'}
323 request
= self
._connection
.post('tag_followings', data
=json
.dumps(data
), headers
=headers
)
325 if request
.status_code
not in [201, 403]:
326 raise Exception('wrong error code: {0}'.format(request
.status_code
))
327 return request
.status_code