documentation fixes
[diaspy.git] / diaspy.py
1 """
2 .. module:: diaspy
3 :platform: Unix, Windows
4 :synopsis: Simple python api for diaspora
5
6 .. moduleauthor:: Moritz Kiefer <moritz.kiefer@gmail.com>
7
8
9 """
10 import requests
11 import re
12 import json
13
14
15 class Client:
16 """This is the client class to connect to diaspora.
17
18 .. note::
19
20 Before calling any other function
21 you have to call :func:`diaspy.Client.login`.
22
23 """
24
25 def __init__(self, pod):
26 self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
27 self._pod = pod
28 self._session = requests.Session()
29
30 def _get_token(self):
31 r = self._session.get(self._pod + "/stream")
32 token = self._token_regex.search(r.text).group(1)
33 return token
34
35
36 def login(self, username, password):
37 """This function is used to connect to the pod and log in.
38
39 :param username: The username used to log in.
40 :type username: str
41 :param password: The password used to log in.
42 :type password: str
43
44 """
45 self._username = username
46 self._password = password
47 r = self._session.get(self._pod + "/users/sign_in")
48 token = self._token_regex.search(r.text).group(1)
49
50 data = {'user[username]': self._username,
51 'user[password]': self._password,
52 'authenticity_token': token,
53 'commit': ''}
54
55 r = self._session.post(self._pod + "/users/sign_in", data=data)
56
57 def post(self, text, aspect_id='public'):
58 """This function sends a post to an aspect
59
60 :param text: Text to post.
61 :type text: str
62 :param aspect_id: Aspect id to send post to.
63 :type aspect_id: str
64
65 """
66 data = {'aspect_ids': aspect_id,
67 'status_message[text]': text,
68 'authenticity_token': self._get_token()}
69 r = self._session.post(self._pod + "/status_messages", data=data)
70
71 def get_user_info(self):
72 """This function returns the current user's attributes.
73
74 :returns: dict -- json formatted user info.
75
76 """
77 r = self._session.get(self._pod + "/stream")
78 regex = re.compile(r'window.current_user_attributes = ({.*})')
79 userdata = json.loads(regex.search(r.text).group(1))
80 return userdata
81
82 def like(self, post_id):
83 """This function likes a post
84
85 :param post_id: id of the post to like.
86 :type post_id: str
87 :returns: dict -- json formatted like object.
88
89 """
90
91 data = {'authenticity_token': self._get_token()}
92
93 r = self._session.post(self._pod + "/posts/" +
94 post_id + "/likes", data=data, headers={'accept': 'application/json'})
95 return r.json()
96
97 def rmlike(self, post_id, like_id):
98 """This function removes a like from a post
99
100 :param post_id: id of the post to remove the like from.
101 :type post_id: str
102 :param like_id: id of the like to remove.
103 :type like_id: str
104
105 """
106
107 data = {'authenticity_token': self._get_token()}
108
109 r = self._session.delete(self._pod + "/posts/" +
110 post_id + "/likes/" +
111 like_id,
112 data=data)
113
114 def reshare(self, post_guid):
115 """This function reshares a post
116
117 :param post_id: id of the post to resahre.
118 :type post_id: str
119
120 """
121
122 data = {'root_guid': post_guid,
123 'authenticity_token': self._get_token()}
124
125 r = self._session.post(self._pod + "/reshares", data=data)
126
127 def comment(self, post_id, text):
128 """This function comments on a post
129
130 :param post_id: id of the post to comment on.
131 :type post_id: str
132 :param text: text to comment.
133 :type text: str
134
135 """
136
137 data = {'text': text,
138 'authenticity_token': self._get_token()}
139
140 r = self._session.post(self._pod + "/posts/" + post_id + "/comments", data=data)
141
142 return r.json()
143
144 def rmcomment(self, post_id, comment_id):
145 """This function removes a comment from a post
146
147 :param post_id: id of the post to remove the like from.
148 :type post_id: str
149 :param like_id: id of the like to remove.
150 :type like_id: str
151
152 """
153
154 data = {'authenticity_token': self._get_token()}
155
156 r = self._session.delete(self._pod + "/posts/" +
157 post_id + "/comments/" +
158 comment_id,
159 data=data)