get_notifications() function
[diaspy.git] / diaspy / client.py
1 import requests
2 import re
3 import json
4 import diaspy.models
5
6
7 class Client:
8 """This is the client class to connect to diaspora.
9
10 """
11
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 """
22 self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
23 self.pod = pod
24 self.session = requests.Session()
25 self._login(username, password)
26
27 def get_token(self):
28 """This function gets a token needed for authentication in most cases
29
30 :returns: string -- token used to authenticate
31
32 """
33
34 r = self.session.get(self.pod + '/stream')
35 token = self._token_regex.search(r.text).group(1)
36 return token
37
38 def _login(self, username, password):
39 """This function is used to connect to the pod and log in.
40 .. note::
41 This function shouldn't be called manually.
42 """
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)
47
48 data = {'user[username]': self._username,
49 'user[password]': self._password,
50 'authenticity_token': token,
51 'commit': ''}
52
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.')
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()}
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()
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 """
88 r = self.session.get(self.pod + '/stream')
89 regex = re.compile(r'window.current_user_attributes = ({.*})')
90 userdata = json.loads(regex.search(r.text).group(1))
91 return userdata
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
114
115 def get_notifications(self):
116 """This functions returns a list of notifications.
117
118 :returns: list -- list of json formatted notifications
119
120 """
121
122
123 data = {'authenticity_token': self.get_token()}
124 r = self.session.get(self.pod + "/notifications.json")
125
126 if r.status_code != 200:
127 raise Exception('wrong status code: ' + str(r.status_code))
128
129 notifications = r.json()
130 return notifications