Add function to read stream data
authorMoritz Kiefer <moritz.kiefer@gmail.com>
Fri, 1 Feb 2013 20:07:22 +0000 (21:07 +0100)
committerMoritz Kiefer <moritz.kiefer@gmail.com>
Fri, 1 Feb 2013 20:07:22 +0000 (21:07 +0100)
diaspy/client.py
diaspy/models.py

index 13782a8905e2463aeca128ae0425bb037b9cbf3e..1807c201688ea462d32f8deac765c75e483a96ed 100644 (file)
@@ -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
index 3302c76e9c198e96d19c99dfa2f510713040909e..6e29f73061c8429a63e5268ef164020b7721e7a8 100644 (file)
@@ -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))