3 :platform: Unix, Windows
4 :synopsis: Simple python api for diaspora
6 .. moduleauthor:: Moritz Kiefer <moritz.kiefer@gmail.com>
16 """This is the client class to connect to diaspora.
20 Before calling any other function
21 you have to call :func:`diaspy.Client.login`.
25 def __init__(self
, pod
):
26 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
28 self
._session
= requests
.Session()
31 r
= self
._session
.get(self
._pod
+ "/stream")
32 token
= self
._token
_regex
.search(r
.text
).group(1)
36 def login(self
, username
, password
):
37 """This function is used to connect to the pod and log in.
39 :param username: The username used to log in.
41 :param password: The password used to log in.
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)
50 data
= {'user[username]': self
._username
,
51 'user[password]': self
._password
,
52 'authenticity_token': token
,
55 r
= self
._session
.post(self
._pod
+ "/users/sign_in", data
=data
)
57 def post(self
, text
, aspect_id
='public'):
58 """This function sends a post to an aspect
60 :param text: Text to post.
62 :param aspect_id: Aspect id to send post to.
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
)
71 def get_user_info(self
):
72 """This function returns the current user's attributes.
74 :returns: dict -- json formatted user info.
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))
82 def like(self
, post_id
):
83 """This function likes a post
85 :param post_id: id of the post to like.
87 :returns: dict -- json formatted like object.
91 data
= {'authenticity_token': self
._get
_token
()}
93 r
= self
._session
.post(self
._pod
+ "/posts/" +
94 post_id
+ "/likes", data
=data
, headers
={'accept': 'application/json'})
97 def rmlike(self
, post_id
, like_id
):
98 """This function removes a like from a post
100 :param post_id: id of the post to remove the like from.
102 :param like_id: id of the like to remove.
107 data
= {'authenticity_token': self
._get
_token
()}
109 r
= self
._session
.delete(self
._pod
+ "/posts/" +
110 post_id
+ "/likes/" +
114 def reshare(self
, post_guid
):
115 """This function reshares a post
117 :param post_id: id of the post to resahre.
122 data
= {'root_guid': post_guid
,
123 'authenticity_token': self
._get
_token
()}
125 r
= self
._session
.post(self
._pod
+ "/reshares", data
=data
)
127 def comment(self
, post_id
, text
):
128 """This function comments on a post
130 :param post_id: id of the post to comment on.
132 :param text: text to comment.
137 data
= {'text': text
,
138 'authenticity_token': self
._get
_token
()}
140 r
= self
._session
.post(self
._pod
+ "/posts/" + post_id
+ "/comments", data
=data
)
144 def rmcomment(self
, post_id
, comment_id
):
145 """This function removes a comment from a post
147 :param post_id: id of the post to remove the like from.
149 :param like_id: id of the like to remove.
154 data
= {'authenticity_token': self
._get
_token
()}
156 r
= self
._session
.delete(self
._pod
+ "/posts/" +
157 post_id
+ "/comments/" +