3 import diaspy
.connection
7 """This is the client class to connect to Diaspora.
9 def __init__(self
, pod
, username
, password
):
11 :param pod: The complete url of the diaspora pod to use.
13 :param username: The username used to log in.
15 :param password: The password used to log in.
18 self
.connection
= diaspy
.connection
.Connection(pod
, username
, password
)
19 self
.connection
.login()
21 self
.stream
= diaspy
.streams
.Stream(self
.connection
, 'stream.json')
23 def post(self
, text
, aspect_ids
='public', photos
=None):
24 """This function sends a post to an aspect
26 :param text: Text to post.
28 :param aspect_ids: Aspect ids to send post to.
31 :returns: diaspy.models.Post -- the Post which has been created
33 post
= self
.stream
.post(text
, aspect_ids
, photos
)
36 def post_picture(self
, filename
):
37 """This method posts a picture to D*.
39 :param filename: Path to picture file.
42 return self
.stream
.post_picture(filename
)
44 def get_activity(self
):
45 """This function returns activity stream.
46 :returns: diaspy.streams.Activity
48 activity
= diaspy
.streams
.Activity(self
.connection
, 'activity.json')
52 """This functions returns stream.
54 :returns: diaspy.streams.Stream
59 def get_notifications(self
):
60 """This functions returns a list of notifications.
62 :returns: list -- list of json formatted notifications
64 r
= self
.connection
.get('notifications.json')
66 if r
.status_code
!= 200:
67 raise Exception('wrong status code: {0}'.format(r
.status_code
))
69 notifications
= r
.json()
72 def get_mentions(self
):
73 """This functions returns a list of
74 posts the current user is being mentioned in.
76 :returns: list -- list of Post objects
78 r
= self
.connection
.get('mentions.json')
80 if r
.status_code
!= 200:
81 raise Exception('wrong status code: {0}'.format(r
.status_code
))
84 return [diaspy
.models
.Post(str(post
['id']), self
.connection
) for post
in mentions
]
86 def get_tag(self
, tag
):
87 """This functions returns a list of posts containing the tag.
88 :param tag: Name of the tag
91 :returns: list -- list of Post objects
93 r
= self
.connection
.get('tags/{0}.json'.format(tag
))
95 if r
.status_code
!= 200:
96 raise Exception('wrong status code: {0}'.format(r
.status_code
))
98 tagged_posts
= r
.json()
99 return [diaspy
.models
.Post(str(post
['id']), self
.connection
) for post
in tagged_posts
]
101 def get_mailbox(self
):
102 """This functions returns a list of messages found in the conversation.
104 :returns: list -- list of Conversation objects.
106 r
= self
.connection
.get('conversations.json')
108 if r
.status_code
!= 200:
109 raise Exception('wrong status code: {0}'.format(r
.status_code
))
112 return [diaspy
.conversations
.Conversation(str(conversation
['conversation']['id']), self
.connection
)
113 for conversation
in mailbox
]
115 def add_user_to_aspect(self
, user_id
, aspect_id
):
116 """ this function adds a user to an aspect.
118 :param user_id: User ID
120 :param aspect_id: Aspect ID
124 data
= {'authenticity_token': self
.connection
.getToken(),
125 'aspect_id': aspect_id
,
126 'person_id': user_id
}
128 r
= self
.connection
.post('aspect_memberships.json', data
=data
)
130 if r
.status_code
!= 201:
131 raise Exception('wrong status code: {0}'.format(r
.status_code
))
134 def add_aspect(self
, aspect_name
, visible
=0):
135 """ This function adds a new aspect.
137 aspects
= diaspy
.streams
.Aspects(self
.connection
)
138 aspects
.add(aspect_name
, visible
)
140 def remove_user_from_aspect(self
, user_id
, aspect_id
):
141 """ this function removes a user from an aspect.
143 :param user_id: User ID
145 :param aspect_id: Aspect ID
149 data
= {'authenticity_token': self
.connection
.getToken(),
150 'aspect_id': aspect_id
,
151 'person_id': user_id
}
153 r
= self
.connection
.delete('aspect_memberships/42.json',
156 if r
.status_code
!= 200:
157 raise Exception('wrong status code: {0}'.format(r
.status_code
))
161 def remove_aspect(self
, aspect_id
):
162 """ This function adds a new aspect.
164 data
= {'authenticity_token': self
.connection
.getToken()}
166 r
= self
.connection
.delete('aspects/{}'.format(aspect_id
),
169 if r
.status_code
!= 404:
170 raise Exception('wrong status code: {0}'.format(r
.status_code
))
172 def new_conversation(self
, contacts
, subject
, text
):
173 """Start a new conversation.
175 :param contacts: recipients ids, no guids, comma sperated.
177 :param subject: subject of the message.
179 :param text: text of the message.
182 data
= {'contact_ids': contacts
,
183 'conversation[subject]': subject
,
184 'conversation[text]': text
,
186 'authenticity_token': self
.connection
.getToken()}
188 r
= self
.connection
.post('conversations/',
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
))