Commit | Line | Data |
---|---|---|
a993a4b6 MK |
1 | import requests |
2 | import re | |
3 | import json | |
b356c9f9 | 4 | import diaspy.models |
a993a4b6 MK |
5 | |
6 | ||
7 | class Client: | |
8 | """This is the client class to connect to diaspora. | |
9 | ||
a993a4b6 MK |
10 | """ |
11 | ||
a7661afd MK |
12 | def __init__(self, pod, username, password): |
13 | """ | |
14 | :param pod: The complete url of the diaspora pod to use. | |
15 | :type pod: str | |
16 | :param username: The username used to log in. | |
17 | :type username: str | |
18 | :param password: The password used to log in. | |
19 | :type password: str | |
20 | ||
21 | """ | |
a993a4b6 MK |
22 | self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') |
23 | self.pod = pod | |
24 | self.session = requests.Session() | |
a7661afd | 25 | self._login(username, password) |
a993a4b6 MK |
26 | |
27 | def get_token(self): | |
ae221396 MK |
28 | """This function gets a token needed for authentication in most cases |
29 | ||
30 | :returns: string -- token used to authenticate | |
31 | ||
32 | """ | |
33 | ||
a7661afd | 34 | r = self.session.get(self.pod + '/stream') |
a993a4b6 MK |
35 | token = self._token_regex.search(r.text).group(1) |
36 | return token | |
37 | ||
a7661afd | 38 | def _login(self, username, password): |
a993a4b6 | 39 | """This function is used to connect to the pod and log in. |
a7661afd MK |
40 | .. note:: |
41 | This function shouldn't be called manually. | |
a993a4b6 MK |
42 | """ |
43 | self._username = username | |
44 | self._password = password | |
a7661afd | 45 | r = self.session.get(self.pod + '/users/sign_in') |
a993a4b6 MK |
46 | token = self._token_regex.search(r.text).group(1) |
47 | ||
48 | data = {'user[username]': self._username, | |
49 | 'user[password]': self._password, | |
50 | 'authenticity_token': token, | |
51 | 'commit': ''} | |
52 | ||
a7661afd MK |
53 | r = self.session.post(self.pod + |
54 | '/users/sign_in', | |
55 | data=data, | |
56 | headers={'accept': 'application/json'}) | |
57 | ||
58 | if r.status_code != 201: | |
59 | raise Exception(str(r.status_code) + ': Login failed.') | |
a993a4b6 MK |
60 | |
61 | def post(self, text, aspect_id='public'): | |
62 | """This function sends a post to an aspect | |
63 | ||
64 | :param text: Text to post. | |
65 | :type text: str | |
66 | :param aspect_id: Aspect id to send post to. | |
67 | :type aspect_id: str | |
68 | ||
69 | """ | |
70 | data = {'aspect_ids': aspect_id, | |
71 | 'status_message[text]': text, | |
72 | 'authenticity_token': self.get_token()} | |
a7661afd MK |
73 | r = self.session.post(self.pod + |
74 | "/status_messages", | |
75 | data=data, | |
76 | headers={'accept': 'application/json'}) | |
77 | if r.status_code != 201: | |
78 | raise Exception(str(r.status_code) + ': Post could not be posted.') | |
79 | ||
80 | return r.json() | |
a993a4b6 MK |
81 | |
82 | def get_user_info(self): | |
83 | """This function returns the current user's attributes. | |
84 | ||
85 | :returns: dict -- json formatted user info. | |
86 | ||
87 | """ | |
a7661afd | 88 | r = self.session.get(self.pod + '/stream') |
a993a4b6 MK |
89 | regex = re.compile(r'window.current_user_attributes = ({.*})') |
90 | userdata = json.loads(regex.search(r.text).group(1)) | |
91 | return userdata | |
b356c9f9 MK |
92 | |
93 | def get_stream(self): | |
94 | """This functions returns a list of posts found in the stream. | |
95 | ||
96 | :returns: list -- list of Post objects. | |
97 | ||
98 | """ | |
99 | ||
100 | data = {'authenticity_token': self.get_token()} | |
101 | r = self.session.get(self.pod + "/stream.json") | |
102 | ||
103 | if r.status_code != 200: | |
104 | raise Exception('wrong status code: ' + str(r.status_code)) | |
105 | ||
106 | stream = r.json() | |
107 | ||
108 | posts = [] | |
109 | ||
110 | for post in stream: | |
111 | posts.append(diaspy.models.Post(str(post['id']), self)) | |
112 | ||
113 | return posts |