Add functions to like and remove likes on a post.
[diaspy.git] / diaspy.py
CommitLineData
c84d4a8e 1"""
8ee6afa4 2.. module:: diaspy
c84d4a8e
MK
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
55447814 12import json
c84d4a8e 13
52280949 14
c84d4a8e
MK
15class Client:
16 """This is the client class to connect to diaspora.
17
18 .. note::
19
52280949
MK
20 Before calling any other function
21 you have to call :func:`diaspy.Client.login`.
c84d4a8e
MK
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 login(self, username, password):
60ca2baa 31 """This function is used to connect to the pod and log in.
52280949 32
60ca2baa 33 :param username: The username used to log in.
dff719cc 34 :type username: str
60ca2baa 35 :param password: The password used to log in.
dff719cc 36 :type password: str
52280949 37
60ca2baa 38 """
c84d4a8e
MK
39 self._username = username
40 self._password = password
41 r = self._session.get(self._pod + "/users/sign_in")
42 token = self._token_regex.search(r.text).group(1)
43
44 data = {'user[username]': self._username,
45 'user[password]': self._password,
46 'authenticity_token': token,
52280949 47 'commit': ''}
c84d4a8e
MK
48
49 r = self._session.post(self._pod + "/users/sign_in", data=data)
50
51 def post(self, text, aspect_id='public'):
60ca2baa 52 """This function sends a post to an aspect
52280949 53
60ca2baa
MK
54 :param text: Text to post.
55 :type text: str
56 :param aspect_id: Aspect id to send post to.
dff719cc 57 :type aspect_id: str
52280949 58
60ca2baa 59 """
c84d4a8e
MK
60 r = self._session.get(self._pod + "/stream")
61 token = self._token_regex.search(r.text).group(1)
62 data = {'aspect_ids': aspect_id,
63 'status_message[text]': text,
64 'authenticity_token': token}
65 r = self._session.post(self._pod + "/status_messages", data=data)
55447814 66
52280949
MK
67 def get_user_info(self):
68 """This function returns the current user's attributes.
69
70 :returns: dict -- json formatted user info.
55447814
MK
71
72 """
73 r = self._session.get(self._pod + "/stream")
74 regex = re.compile(r'window.current_user_attributes = ({.*})')
75 userdata = json.loads(regex.search(r.text).group(1))
76 return userdata
52280949
MK
77
78 def like(self, post_id):
79 """This function likes a post
80
81 :param post_id: id of the post to like.
82 :type post_id: str
83 :returns: dict -- json formatted like object.
84
85 """
86 r = self._session.get(self._pod + "/stream")
87 token = self._token_regex.search(r.text).group(1)
88
89 data = {'authenticity_token': token}
90
91 r = self._session.post(self._pod + "/posts/" +
92 post_id + "/likes", data=data, headers={'accept': 'application/json'})
93 return r.json()
94
95 def rmlike(self, post_id, like_id):
96 """This function removes a like from a post
97
98 :param post_id: id of the post to remove the like from.
99 :type post_id: str
100 :param like_id: id of the like to remove.
101 :type like_id: str
102
103 """
104 r = self._session.get(self._pod + "/stream")
105 token = self._token_regex.search(r.text).group(1)
106
107 data = {'authenticity_token': token}
108
109 r = self._session.delete(self._pod + "/posts/" +
110 post_id + "/likes/" +
111 like_id,
112 data=data)