Further work being done on streams; all basic streams implemented
[diaspy.git] / diaspy / client.py
1 import diaspy.models
2 import diaspy.streams
3 import diaspy.connection
4
5
6 class Client:
7 """This is the client class to connect to Diaspora.
8 """
9 def __init__(self, pod, username, password):
10 """
11 :param pod: The complete url of the diaspora pod to use.
12 :type pod: str
13 :param username: The username used to log in.
14 :type username: str
15 :param password: The password used to log in.
16 :type password: str
17 """
18 self.connection = diaspy.connection.Connection(pod, username, password)
19 self.connection.login()
20 self.pod = pod
21 self.stream = diaspy.streams.Stream(self.connection, 'stream.json')
22
23 def post(self, text, aspect_ids='public', photos=None):
24 """This function sends a post to an aspect
25
26 :param text: Text to post.
27 :type text: str
28 :param aspect_ids: Aspect ids to send post to.
29 :type aspect_ids: str
30
31 :returns: diaspy.models.Post -- the Post which has been created
32 """
33 post = self.stream.post(text, aspect_ids, photos)
34 return post
35
36 def post_picture(self, filename):
37 """This method posts a picture to D*.
38
39 :param filename: Path to picture file.
40 :type filename: str
41 """
42 return self.stream.post_picture(filename)
43
44 def get_activity(self):
45 """This function returns activity stream.
46 :returns: diaspy.streams.Activity
47 """
48 activity = diaspy.streams.Activity(self.connection, 'activity.json')
49 return activity
50
51 def get_stream(self):
52 """This functions returns stream.
53
54 :returns: diaspy.streams.Stream
55 """
56 self.stream.update()
57 return self.stream
58
59 def get_notifications(self):
60 """This functions returns a list of notifications.
61
62 :returns: list -- list of json formatted notifications
63 """
64 r = self.connection.get('notifications.json')
65
66 if r.status_code != 200:
67 raise Exception('wrong status code: {0}'.format(r.status_code))
68
69 notifications = r.json()
70 return notifications
71
72 def get_mentions(self):
73 """This functions returns a list of
74 posts the current user is being mentioned in.
75
76 :returns: list -- list of Post objects
77 """
78 r = self.connection.get('mentions.json')
79
80 if r.status_code != 200:
81 raise Exception('wrong status code: {0}'.format(r.status_code))
82
83 mentions = r.json()
84 return [diaspy.models.Post(str(post['id']), self.connection) for post in mentions]
85
86 def get_tag(self, tag):
87 """This functions returns a list of posts containing the tag.
88 :param tag: Name of the tag
89 :type tag: str
90
91 :returns: list -- list of Post objects
92 """
93 r = self.connection.get('tags/{0}.json'.format(tag))
94
95 if r.status_code != 200:
96 raise Exception('wrong status code: {0}'.format(r.status_code))
97
98 tagged_posts = r.json()
99 return [diaspy.models.Post(str(post['id']), self.connection) for post in tagged_posts]
100
101 def get_mailbox(self):
102 """This functions returns a list of messages found in the conversation.
103
104 :returns: list -- list of Conversation objects.
105 """
106 r = self.connection.get('conversations.json')
107
108 if r.status_code != 200:
109 raise Exception('wrong status code: {0}'.format(r.status_code))
110
111 mailbox = r.json()
112 return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
113 for conversation in mailbox]
114
115 def add_user_to_aspect(self, user_id, aspect_id):
116 """ this function adds a user to an aspect.
117
118 :param user_id: User ID
119 :type user_id: str
120 :param aspect_id: Aspect ID
121 :type aspect_id: str
122
123 """
124 data = {'authenticity_token': self.connection.getToken(),
125 'aspect_id': aspect_id,
126 'person_id': user_id}
127
128 r = self.connection.post('aspect_memberships.json', data=data)
129
130 if r.status_code != 201:
131 raise Exception('wrong status code: {0}'.format(r.status_code))
132 return r.json()
133
134 def add_aspect(self, aspect_name, visible=0):
135 """ This function adds a new aspect.
136 """
137 aspects = diaspy.streams.Aspects(self.connection)
138 aspects.add(aspect_name, visible)
139
140 def remove_user_from_aspect(self, user_id, aspect_id):
141 """ this function removes a user from an aspect.
142
143 :param user_id: User ID
144 :type user_id: str
145 :param aspect_id: Aspect ID
146 :type aspect_id: str
147
148 """
149 data = {'authenticity_token': self.connection.getToken(),
150 'aspect_id': aspect_id,
151 'person_id': user_id}
152
153 r = self.connection.delete('aspect_memberships/42.json',
154 data=data)
155
156 if r.status_code != 200:
157 raise Exception('wrong status code: {0}'.format(r.status_code))
158
159 return r.json()
160
161 def remove_aspect(self, aspect_id):
162 """ This function adds a new aspect.
163 """
164 data = {'authenticity_token': self.connection.getToken()}
165
166 r = self.connection.delete('aspects/{}'.format(aspect_id),
167 data=data)
168
169 if r.status_code != 404:
170 raise Exception('wrong status code: {0}'.format(r.status_code))
171
172 def new_conversation(self, contacts, subject, text):
173 """Start a new conversation.
174
175 :param contacts: recipients ids, no guids, comma sperated.
176 :type contacts: str
177 :param subject: subject of the message.
178 :type subject: str
179 :param text: text of the message.
180 :type text: str
181 """
182 data = {'contact_ids': contacts,
183 'conversation[subject]': subject,
184 'conversation[text]': text,
185 'utf8': '✓',
186 'authenticity_token': self.connection.getToken()}
187
188 r = self.connection.post('conversations/',
189 data=data,
190 headers={'accept': 'application/json'})
191 if r.status_code != 200:
192 raise Exception('{0}: Conversation could not be started.'
193 .format(r.status_code))
194 return r.json()