Commit | Line | Data |
---|---|---|
a993a4b6 MK |
1 | import requests |
2 | import re | |
3 | import json | |
95d2d310 | 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 | ||
95d2d310 MK |
69 | :returns: diaspy.models.Post -- the Post which has been created |
70 | ||
a993a4b6 MK |
71 | """ |
72 | data = {'aspect_ids': aspect_id, | |
73 | 'status_message[text]': text, | |
74 | 'authenticity_token': self.get_token()} | |
a7661afd MK |
75 | r = self.session.post(self.pod + |
76 | "/status_messages", | |
77 | data=data, | |
78 | headers={'accept': 'application/json'}) | |
79 | if r.status_code != 201: | |
80 | raise Exception(str(r.status_code) + ': Post could not be posted.') | |
81 | ||
95d2d310 | 82 | return diaspy.models.Post(str(r.json()['id']), self) |
a993a4b6 MK |
83 | |
84 | def get_user_info(self): | |
85 | """This function returns the current user's attributes. | |
86 | ||
87 | :returns: dict -- json formatted user info. | |
88 | ||
89 | """ | |
a7661afd | 90 | r = self.session.get(self.pod + '/stream') |
a993a4b6 MK |
91 | regex = re.compile(r'window.current_user_attributes = ({.*})') |
92 | userdata = json.loads(regex.search(r.text).group(1)) | |
93 | return userdata | |
b356c9f9 MK |
94 | |
95 | def get_stream(self): | |
96 | """This functions returns a list of posts found in the stream. | |
97 | ||
98 | :returns: list -- list of Post objects. | |
99 | ||
100 | """ | |
101 | ||
102 | data = {'authenticity_token': self.get_token()} | |
103 | r = self.session.get(self.pod + "/stream.json") | |
104 | ||
105 | if r.status_code != 200: | |
106 | raise Exception('wrong status code: ' + str(r.status_code)) | |
107 | ||
108 | stream = r.json() | |
109 | ||
110 | posts = [] | |
111 | ||
112 | for post in stream: | |
113 | posts.append(diaspy.models.Post(str(post['id']), self)) | |
114 | ||
115 | return posts | |
33f21ecf MK |
116 | |
117 | def get_notifications(self): | |
118 | """This functions returns a list of notifications. | |
119 | ||
120 | :returns: list -- list of json formatted notifications | |
121 | ||
122 | """ | |
123 | ||
124 | ||
125 | data = {'authenticity_token': self.get_token()} | |
126 | r = self.session.get(self.pod + "/notifications.json") | |
127 | ||
128 | if r.status_code != 200: | |
129 | raise Exception('wrong status code: ' + str(r.status_code)) | |
130 | ||
131 | notifications = r.json() | |
132 | return notifications | |
3d3dff8f MK |
133 | |
134 | ||
135 | def get_mentions(self): | |
136 | """This functions returns a list of posts the current user is being mentioned in. | |
137 | ||
138 | :returns: list -- list of Post objects | |
139 | ||
140 | """ | |
141 | ||
142 | ||
143 | data = {'authenticity_token': self.get_token()} | |
144 | r = self.session.get(self.pod + "/mentions.json") | |
145 | ||
146 | if r.status_code != 200: | |
147 | raise Exception('wrong status code: ' + str(r.status_code)) | |
148 | ||
149 | mentions = r.json() | |
150 | ||
151 | posts = [] | |
152 | ||
153 | for post in mentions: | |
154 | posts.append(diaspy.models.Post(str(post['id']), self)) | |
155 | ||
156 | return posts |