7 """This is the client class to connect to diaspora.
11 def __init__(self
, pod
, username
, password
):
13 :param pod: The complete url of the diaspora pod to use.
15 :param username: The username used to log in.
17 :param password: The password used to log in.
21 self
._token
_regex
= re
.compile(r
'content="(.*?)"\s+name="csrf-token')
23 self
.session
= requests
.Session()
24 self
._login
(username
, password
)
27 """This function gets a token needed for authentication in most cases
29 :returns: string -- token used to authenticate
33 r
= self
.session
.get(self
.pod
+ '/stream')
34 token
= self
._token
_regex
.search(r
.text
).group(1)
37 def _login(self
, username
, password
):
38 """This function is used to connect to the pod and log in.
40 This function shouldn't be called manually.
42 self
._username
= username
43 self
._password
= password
44 r
= self
.session
.get(self
.pod
+ '/users/sign_in')
45 token
= self
._token
_regex
.search(r
.text
).group(1)
47 data
= {'user[username]': self
._username
,
48 'user[password]': self
._password
,
49 'authenticity_token': token
,
52 r
= self
.session
.post(self
.pod
+
55 headers
={'accept': 'application/json'})
57 if r
.status_code
!= 201:
58 raise Exception(str(r
.status_code
) + ': Login failed.')
60 def post(self
, text
, aspect_id
='public', photos
=None):
61 """This function sends a post to an aspect
63 :param text: Text to post.
65 :param aspect_id: Aspect id to send post to.
68 :returns: diaspy.models.Post -- the Post which has been created
71 data
= {'aspect_ids': aspect_id
,
72 'status_message': {'text': text
}}
75 data
['photos'] = photos
76 r
= self
.session
.post(self
.pod
+
78 data
=json
.dumps(data
),
79 headers
={'content-type': 'application/json',
80 'accept': 'application/json',
81 'x-csrf-token': self
.get_token()})
82 if r
.status_code
!= 201:
83 raise Exception(str(r
.status_code
) + ': Post could not be posted.')
85 return diaspy
.models
.Post(str(r
.json()['id']), self
)
87 def get_user_info(self
):
88 """This function returns the current user's attributes.
90 :returns: dict -- json formatted user info.
93 r
= self
.session
.get(self
.pod
+ '/bookmarklet')
94 regex
= re
.compile(r
'window.current_user_attributes = ({.*})')
95 userdata
= json
.loads(regex
.search(r
.text
).group(1))
98 def post_picture(self
, filename
):
99 aspects
= self
.get_user_info()['aspects']
101 params
['photo[pending]'] = 'true'
102 params
['set_profile_image'] = ''
103 params
['qqfile'] = filename
104 for i
, aspect
in enumerate(aspects
):
105 params
['photo[aspect_ids][%d]' % (i
)] = aspect
['id']
107 data
= open(filename
, 'rb')
109 headers
= {'content-type': 'application/octet-stream',
110 'x-csrf-token': self
.get_token(),
111 'x-file-name': filename
}
113 r
= self
.session
.post(self
.pod
+ '/photos', params
=params
, data
=data
, headers
=headers
)
119 def get_stream(self
):
120 """This functions returns a list of posts found in the stream.
122 :returns: list -- list of Post objects.
126 data
= {'authenticity_token': self
.get_token()}
127 r
= self
.session
.get(self
.pod
+ "/stream.json")
129 if r
.status_code
!= 200:
130 raise Exception('wrong status code: ' + str(r
.status_code
))
137 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))
141 def get_notifications(self
):
142 """This functions returns a list of notifications.
144 :returns: list -- list of json formatted notifications
149 data
= {'authenticity_token': self
.get_token()}
150 r
= self
.session
.get(self
.pod
+ "/notifications.json")
152 if r
.status_code
!= 200:
153 raise Exception('wrong status code: ' + str(r
.status_code
))
155 notifications
= r
.json()
159 def get_mentions(self
):
160 """This functions returns a list of posts the current user is being mentioned in.
162 :returns: list -- list of Post objects
167 data
= {'authenticity_token': self
.get_token()}
168 r
= self
.session
.get(self
.pod
+ "/mentions.json")
170 if r
.status_code
!= 200:
171 raise Exception('wrong status code: ' + str(r
.status_code
))
177 for post
in mentions
:
178 posts
.append(diaspy
.models
.Post(str(post
['id']), self
))