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 Stream(Generic
):
120 """The main stream containing the combined posts of the
121 followed users and tags and the community spotlights posts
122 if the user enabled those.
124 def _setlocation(self
):
125 self
._location
= 'stream.json'
127 def post(self
, text
, aspect_ids
='public', photos
=None):
128 """This function sends a post to an aspect
130 :param text: Text to post.
132 :param aspect_ids: Aspect ids to send post to.
133 :type aspect_ids: str
135 :returns: diaspy.models.Post -- the Post which has been created
138 data
['aspect_ids'] = aspect_ids
139 data
['status_message'] = {'text': text
}
140 if photos
: data
['photos'] = photos
141 request
= self
._connection
.post('status_messages',
142 data
=json
.dumps(data
),
143 headers
={'content-type': 'application/json',
144 'accept': 'application/json',
145 'x-csrf-token': self
._connection
.getToken()})
146 if request
.status_code
!= 201:
147 raise Exception('{0}: Post could not be posted.'.format(
148 request
.status_code
))
150 post
= Post(str(request
.json()['id']), self
._connection
)
153 def post_picture(self
, filename
):
154 """This method posts a picture to D*.
156 :param filename: Path to picture file.
159 aspects
= self
._connection
.getUserInfo()['aspects']
161 params
['photo[pending]'] = 'true'
162 params
['set_profile_image'] = ''
163 params
['qqfile'] = filename
164 for i
, aspect
in enumerate(aspects
):
165 params
['photo[aspect_ids][%d]' % (i
)] = aspect
['id']
167 data
= open(filename
, 'rb')
169 headers
= {'content-type': 'application/octet-stream',
170 'x-csrf-token': self
._connection
.getToken(),
171 'x-file-name': filename
}
172 request
= self
._connection
.post('photos', params
=params
, data
=data
, headers
=headers
)
177 class Activity(Generic
):
178 """Stream representing user's activity.
180 def _setlocation(self
):
181 self
._location
= 'activity.json'
183 def _delid(self
, id):
184 """Deletes post with given id.
187 for p
in self
._stream
:
191 if post
is not None: post
.delete()
193 def delete(self
, post
):
194 """Deletes post from users activity.
195 `post` can be either post id or Post()
196 object which will be identified and deleted.
197 After deleting post the stream will be filled.
199 :param post: post identifier
200 :type post: str, diaspy.models.Post
202 if type(post
) == str: self
._delid
(post
)
203 elif type(post
) == Post
: post
.delete()
205 raise TypeError('this method accepts only int, str or Post: {0} given')
209 class Aspects(Generic
):
210 """This stream contains the posts filtered by
211 the specified aspect IDs. You can choose the aspect IDs with
212 the parameter `aspect_ids` which value should be
213 a comma seperated list of aspect IDs.
214 If the parameter is ommitted all aspects are assumed.
215 An example call would be `aspects.json?aspect_ids=23,5,42`
217 def _setlocation(self
):
218 self
._location
= 'aspects.json'
220 def add(self
, aspect_name
, visible
=0):
221 """This function adds a new aspect.
223 data
= {'authenticity_token': self
._connection
.getToken(),
224 'aspect[name]': aspect_name
,
225 'aspect[contacts_visible]': visible
}
227 r
= self
._connection
.post('aspects', data
=data
)
228 if r
.status_code
!= 200:
229 raise Exception('wrong status code: {0}'.format(r
.status_code
))
232 class Commented(Generic
):
233 """This stream contains all posts
234 the user has made a comment on.
236 def _setlocation(self
):
237 self
._location
= 'commented.json'
240 class Liked(Generic
):
241 """This stream contains all posts the user liked.
243 def _setlocation(self
):
244 self
._location
= 'liked.json'
247 class Mentions(Generic
):
248 """This stream contains all posts
249 the user is mentioned in.
251 def _setlocation(self
):
252 self
._location
= 'mentions.json'
255 class FollowedTags(Generic
):
256 """This stream contains all posts
257 containing a tag the user is following.
259 def _setlocation(self
):
260 self
._location
= 'followed_tags.json'
262 def add(self
, tag_name
):
265 data
= {'authenticity_token': self
._connection
.getToken(),
266 'tag_name': tag_name
}
267 r
= self
._connection
.post('tags', data
=data
)
268 if r
.status_code
!= 200:
269 raise Exception('wrong status code: {0}'.format(r
.status_code
))
271 def create(self
, tag_name
):
274 :param tag_name: tag name
277 data
= {'tag_name':tag_name
,
278 'authenticity_token':self
._connection
.getToken(),
280 request
= self
._connection
.post('followed_tags', data
=data
, headers
=headers
)
282 if request
.status_code
!= 200:
283 raise Exception('wrong error code: {0}'.format(request
.status_code
))