Further improvements to the documentation
[diaspy.git] / diaspy.py
CommitLineData
c84d4a8e
MK
1"""
2.. module:: diaspora_api.diaspy
3 :platform: Unix, Windows
4 :synopsis: Simple python api for diaspora
5
6.. moduleauthor:: Moritz Kiefer <moritz.kiefer@gmail.com>
7
8
9"""
10import requests
11import re
12
13class Client:
14 """This is the client class to connect to diaspora.
15
16 .. note::
17
18 Before calling any other function you have to call :func:`diaspy.diaspy.Client.login`.
19
20 """
21
22 def __init__(self, pod):
23 self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
24 self._pod = pod
25 self._session = requests.Session()
26
27 def login(self, username, password):
60ca2baa
MK
28 """This function is used to connect to the pod and log in.
29
30 :param username: The username used to log in.
dff719cc 31 :type username: str
60ca2baa 32 :param password: The password used to log in.
dff719cc 33 :type password: str
60ca2baa
MK
34
35 """
c84d4a8e
MK
36 self._username = username
37 self._password = password
38 r = self._session.get(self._pod + "/users/sign_in")
39 token = self._token_regex.search(r.text).group(1)
40
41 data = {'user[username]': self._username,
42 'user[password]': self._password,
43 'authenticity_token': token,
44 'commit':''}
45
46 r = self._session.post(self._pod + "/users/sign_in", data=data)
47
48 def post(self, text, aspect_id='public'):
60ca2baa
MK
49 """This function sends a post to an aspect
50
51 :param text: Text to post.
52 :type text: str
53 :param aspect_id: Aspect id to send post to.
dff719cc 54 :type aspect_id: str
60ca2baa
MK
55
56 """
c84d4a8e
MK
57 r = self._session.get(self._pod + "/stream")
58 token = self._token_regex.search(r.text).group(1)
59 data = {'aspect_ids': aspect_id,
60 'status_message[text]': text,
61 'authenticity_token': token}
62 r = self._session.post(self._pod + "/status_messages", data=data)