Add function to add and remove comments
[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
0ae51069
MK
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
c84d4a8e 36 def login(self, username, password):
60ca2baa 37 """This function is used to connect to the pod and log in.
52280949 38
60ca2baa 39 :param username: The username used to log in.
dff719cc 40 :type username: str
60ca2baa 41 :param password: The password used to log in.
dff719cc 42 :type password: str
52280949 43
60ca2baa 44 """
c84d4a8e
MK
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,
52280949 53 'commit': ''}
c84d4a8e
MK
54
55 r = self._session.post(self._pod + "/users/sign_in", data=data)
56
57 def post(self, text, aspect_id='public'):
60ca2baa 58 """This function sends a post to an aspect
52280949 59
60ca2baa
MK
60 :param text: Text to post.
61 :type text: str
62 :param aspect_id: Aspect id to send post to.
dff719cc 63 :type aspect_id: str
52280949 64
60ca2baa 65 """
c84d4a8e
MK
66 data = {'aspect_ids': aspect_id,
67 'status_message[text]': text,
0ae51069 68 'authenticity_token': self._get_token()}
c84d4a8e 69 r = self._session.post(self._pod + "/status_messages", data=data)
55447814 70
52280949
MK
71 def get_user_info(self):
72 """This function returns the current user's attributes.
73
74 :returns: dict -- json formatted user info.
55447814
MK
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
52280949
MK
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 """
52280949 90
0ae51069 91 data = {'authenticity_token': self._get_token()}
52280949
MK
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 """
52280949 106
0ae51069 107 data = {'authenticity_token': self._get_token()}
52280949
MK
108
109 r = self._session.delete(self._pod + "/posts/" +
110 post_id + "/likes/" +
111 like_id,
112 data=data)
0ae51069
MK
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)
2ab410e1
MK
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)