Add post class
authorMoritz Kiefer <moritz.kiefer@gmail.com>
Thu, 17 Jan 2013 21:39:36 +0000 (22:39 +0100)
committerMoritz Kiefer <moritz.kiefer@gmail.com>
Thu, 17 Jan 2013 21:42:04 +0000 (22:42 +0100)
.gitignore
diaspy/client.py [new file with mode: 0644]
diaspy/models.py [new file with mode: 0644]
docs/source/client.rst [new file with mode: 0644]
docs/source/models.rst [new file with mode: 0644]
docs/source/modules.rst [new file with mode: 0644]

index 1ebd6c0eaf28fc40c2a19230c441a84c77042464..c5edd0715e067b5b198f583fb1930014c08f1d2a 100644 (file)
@@ -1,6 +1,6 @@
 *~
 *.swp
 docs/build/*
-__pycache__/*
+*__pycache__/*
 .env
 
diff --git a/diaspy/client.py b/diaspy/client.py
new file mode 100644 (file)
index 0000000..c21a6b4
--- /dev/null
@@ -0,0 +1,71 @@
+import requests
+import re
+import json
+
+
+class Client:
+    """This is the client class to connect to diaspora.
+
+    .. note::
+
+       Before calling any other function
+       you have to call :func:`diaspy.Client.login`.
+
+    """
+
+    def __init__(self, pod):
+        self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
+        self.pod = pod
+        self.session = requests.Session()
+
+    def get_token(self):
+        r = self.session.get(self.pod + "/stream")
+        token = self._token_regex.search(r.text).group(1)
+        return token
+
+
+    def login(self, username, password):
+        """This function is used to connect to the pod and log in.
+
+        :param username: The username used to log in.
+        :type username: str
+        :param password: The password used to log in.
+        :type password: str
+
+        """
+        self._username = username
+        self._password = password
+        r = self.session.get(self.pod + "/users/sign_in")
+        token = self._token_regex.search(r.text).group(1)
+
+        data = {'user[username]': self._username,
+                'user[password]': self._password,
+                'authenticity_token': token,
+                'commit': ''}
+
+        r = self.session.post(self.pod + "/users/sign_in", data=data)
+
+    def post(self, text, aspect_id='public'):
+        """This function sends a post to an aspect
+
+        :param text: Text to post.
+        :type text: str
+        :param aspect_id: Aspect id to send post to.
+        :type aspect_id: str
+
+        """
+        data = {'aspect_ids': aspect_id,
+                'status_message[text]': text,
+                'authenticity_token': self.get_token()}
+        r = self.session.post(self.pod + "/status_messages", data=data)
+
+    def get_user_info(self):
+        """This function returns the current user's attributes.
+
+        :returns: dict -- json formatted user info.
+
+        """
+        r = self.session.get(self.pod + "/stream")
+        regex = re.compile(r'window.current_user_attributes = ({.*})')
+        userdata = json.loads(regex.search(r.text).group(1))
+        return userdata
diff --git a/diaspy/models.py b/diaspy/models.py
new file mode 100644 (file)
index 0000000..ea22b45
--- /dev/null
@@ -0,0 +1,89 @@
+import requests
+class Post:
+
+    def __init__(self, post_id, client):
+        self._client = client
+        r = self._client.session.get(self._client.pod + '/posts/' + post_id + '.json')
+        if r.status_code == 200:
+            self.data = r.json()
+        else:
+            raise Exception('wrong status code: ' + str(r.status_code))
+
+    def like(self):
+        """This function likes a post
+
+        :returns: dict -- json formatted like object.
+
+        """
+
+        data = {'authenticity_token': self._client.get_token()}
+
+        r = self._client.session.post(self._client.pod +
+                                      "/posts/" +
+                                      str(self.data['id']) +
+                                      "/likes",
+                                      data=data,
+                                      headers={'accept': 'application/json'})
+        return r.json()
+
+    def rmlike(self):
+        """This function removes a like from a post
+
+        """
+
+        data = {'authenticity_token': self._client.get_token()}
+
+        r = self._client.session.delete(self._client.pod + '/posts/' + 
+                                        str(self.data['id']) +
+                                        '/likes/' +
+                                        str(self.data['interactions']['likes'][0]['id']),
+                                        data=data)
+
+    def reshare(self):
+        """This function reshares a post
+
+        """
+
+        data = {'root_guid': self.data['guid'],
+                'authenticity_token': self._client.get_token()}
+
+        r = self._client.session.post(self._client.pod +
+                                      "/reshares",
+                                      data=data)
+
+        return r.json()
+
+    def comment(self, text):
+        """This function comments on a post
+
+        :param post_id: id of the post to comment on.
+        :type post_id: str
+        :param text: text to comment.
+        :type text: str
+
+        """
+
+        data = {'text': text,
+                'authenticity_token': self._client.get_token()}
+
+        r = self._client.session.post(self._client.pod + '/posts/' + str(self.data['id']) + '/comments', data=data)
+
+        return r.json()
+    
+    def rmcomment(self, comment_id):
+        """This function removes a comment from a post
+
+        :param post_id: id of the post to remove the like from.
+        :type post_id: str
+        :param like_id: id of the like to remove.
+        :type like_id: str
+
+        """
+
+        data = {'authenticity_token': self._client.get_token()}
+
+        r = self._client.session.delete(self._client.pod + '/posts/' + 
+                                str(self.data['id']) +
+                                '/comments/' +
+                                comment_id,
+                                data=data)
diff --git a/docs/source/client.rst b/docs/source/client.rst
new file mode 100644 (file)
index 0000000..acb4873
--- /dev/null
@@ -0,0 +1,7 @@
+client Module
+=============
+
+.. automodule:: client
+    :members:
+    :undoc-members:
+    :show-inheritance:
diff --git a/docs/source/models.rst b/docs/source/models.rst
new file mode 100644 (file)
index 0000000..b37ad40
--- /dev/null
@@ -0,0 +1,7 @@
+models Module
+=============
+
+.. automodule:: models
+    :members:
+    :undoc-members:
+    :show-inheritance:
diff --git a/docs/source/modules.rst b/docs/source/modules.rst
new file mode 100644 (file)
index 0000000..7a1b7a3
--- /dev/null
@@ -0,0 +1,8 @@
+diaspy
+======
+
+.. toctree::
+   :maxdepth: 4
+
+   client
+   models