0d43ef4440413d70f7b062c526d16e3698850c57
8 """This is the client class to connect to diaspora.
12 def __init__(self
, pod
, username
, password
):
14 :param pod: The complete url of the diaspora pod to use.
16 :param username: The username used to log in.
18 :param password: The password used to log in.
22 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
24 self
.session
= requests
.Session()
25 self
._login
(username
, password
)
28 """This function gets a token needed for authentication in most cases
30 :returns: string -- token used to authenticate
34 r
= self
.session
.get(self
.pod
+ '/stream')
35 token
= self
._token
_regex
.search(r
.text
).group(1)
38 def _login(self
, username
, password
):
39 """This function is used to connect to the pod and log in.
41 This function shouldn't be called manually.
43 self
._username
= username
44 self
._password
= password
45 r
= self
.session
.get(self
.pod
+ '/users/sign_in')
46 token
= self
._token
_regex
.search(r
.text
).group(1)
48 data
= {'user[username]': self
._username
,
49 'user[password]': self
._password
,
50 'authenticity_token': token
,
53 r
= self
.session
.post(self
.pod
+
56 headers
={'accept': 'application/json'})
58 if r
.status_code
!= 201:
59 raise Exception(str(r
.status_code
) + ': Login failed.')
61 def post(self
, text
, aspect_id
='public'):
62 """This function sends a post to an aspect
64 :param text: Text to post.
66 :param aspect_id: Aspect id to send post to.
69 :returns: diaspy.models.Post -- the Post which has been created
72 data
= {'aspect_ids': aspect_id
,
73 'status_message[text]': text
,
74 'authenticity_token': self
.get_token()}
75 r
= self
.session
.post(self
.pod
+
78 headers
={'accept': 'application/json'})
79 if r
.status_code
!= 201:
80 raise Exception(str(r
.status_code
) + ': Post could not be posted.')
82 return diaspy
.models
.Post(str(r
.json()['id']), self
)
84 def get_user_info(self
):
85 """This function returns the current user's attributes.
87 :returns: dict -- json formatted user info.
90 r
= self
.session
.get(self
.pod
+ '/stream')
91 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
92 userdata
= json
.loads(regex
.search(r
.text
).group(1))
96 """This functions returns a list of posts found in the stream.
98 :returns: list -- list of Post objects.
102 data
= {'authenticity_token': self
.get_token()}
103 r
= self
.session
.get(self
.pod
+ "/stream.json")
105 if r
.status_code
!= 200:
106 raise Exception('wrong status code: ' + str(r
.status_code
))
113 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
117 def get_notifications(self
):
118 """This functions returns a list of notifications.
120 :returns: list -- list of json formatted notifications
125 data
= {'authenticity_token': self
.get_token()}
126 r
= self
.session
.get(self
.pod
+ "/notifications.json")
128 if r
.status_code
!= 200:
129 raise Exception('wrong status code: ' + str(r
.status_code
))
131 notifications
= r
.json()
135 def get_mentions(self
):
136 """This functions returns a list of posts the current user is being mentioned in.
138 :returns: list -- list of Post objects
143 data
= {'authenticity_token': self
.get_token()}
144 r
= self
.session
.get(self
.pod
+ "/mentions.json")
146 if r
.status_code
!= 200:
147 raise Exception('wrong status code: ' + str(r
.status_code
))
153 for post
in mentions
:
154 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))