From: Moritz Kiefer Date: Fri, 1 Feb 2013 20:07:22 +0000 (+0100) Subject: Add function to read stream data X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b356c9f9d586bbe9c3c749072508bade856c50d2;p=diaspy.git Add function to read stream data --- diff --git a/diaspy/client.py b/diaspy/client.py index 13782a8..1807c20 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -1,6 +1,7 @@ import requests import re import json +import diaspy.models class Client: @@ -88,3 +89,25 @@ class Client: regex = re.compile(r'window.current_user_attributes = ({.*})') userdata = json.loads(regex.search(r.text).group(1)) return userdata + + def get_stream(self): + """This functions returns a list of posts found in the stream. + + :returns: list -- list of Post objects. + + """ + + data = {'authenticity_token': self.get_token()} + r = self.session.get(self.pod + "/stream.json") + + if r.status_code != 200: + raise Exception('wrong status code: ' + str(r.status_code)) + + stream = r.json() + + posts = [] + + for post in stream: + posts.append(diaspy.models.Post(str(post['id']), self)) + + return posts diff --git a/diaspy/models.py b/diaspy/models.py index 3302c76..6e29f73 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -31,7 +31,7 @@ class Post: self.post_id + '.json') if r.status_code == 200: - self.data = r.json() + return r.json() else: raise Exception('wrong status code: ' + str(r.status_code))