From ed366d4483df1d58e9287a27de565ffffe6bf9c1 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Wed, 12 Jun 2013 07:02:40 +0200 Subject: [PATCH] `more()` method implemented in generic stream -- now one can get older posts --- diaspy/streams.py | 49 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/diaspy/streams.py b/diaspy/streams.py index 7bbc70f..9b571f0 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -49,14 +49,39 @@ class Generic(): """ return len(self._stream) - def _obtain(self): + def _obtain(self, max_time=0): """Obtains stream from pod. """ - request = self._connection.get(self._location) + params = {} + if max_time: params['max_time'] = max_time + request = self._connection.get(self._location, params=params) if request.status_code != 200: raise Exception('wrong status code: {0}'.format(request.status_code)) return [Post(str(post['id']), self._connection) for post in request.json()] + def _expand(self, new_stream): + """Appends older posts to stream. + """ + ids = [post.post_id for post in self._stream] + stream = self._stream + for post in new_stream: + if post.post_id not in ids: + stream.append(post) + ids.append(post.post_id) + self._stream = stream + + def _update(self, new_stream): + """Updates stream with new posts. + """ + ids = [post.post_id for post in self._stream] + + stream = self._stream + for i in range(len(new_stream)): + if new_stream[-i].post_id not in ids: + stream = [new_stream[-i]] + stream + ids.append(new_stream[-i].post_id) + self._stream = stream + def clear(self): """Removes all posts from stream. """ @@ -80,16 +105,7 @@ class Generic(): def update(self): """Updates stream. """ - new_stream = self._obtain() - ids = [post.post_id for post in self._stream] - - stream = self._stream - for i in range(len(new_stream)): - if new_stream[-i].post_id not in ids: - stream = [new_stream[-i]] + stream - ids.append(new_stream[-i].post_id) - - self._stream = stream + self._update( self._obtain()) def fill(self): """Fills the stream with posts. @@ -98,13 +114,14 @@ class Generic(): def more(self, max_time=0): """Tries to download more (older ones) Posts from Stream. + + :param max_time: seconds since epoch (optional, diaspy'll figure everything on its own) + :type max_time: int """ if not max_time: max_time = self.max_time - 3000000 self.max_time = max_time - params = {'max_time': self.max_time} - request = self._connection.get(self._location, params=params) - if request.status_code != 200: - raise Exception('wrong status code: {0}'.format(request.status_code)) + new_stream = self._obtain(max_time=max_time) + self._expand(new_stream) class Outer(Generic): -- 2.25.1