3 import diaspy
.connection
7 """This is the client class used to interact with Diaspora.
8 It can be used as a reference implementation of client using diaspy.
10 def __init__(self
, pod
, username
='', password
=''):
12 `pod` can also be a diaspy.connection.Connection type and
13 Client() will detect it. When giving a connection there is no need
14 to pass username and password.
16 :param pod: The complete url of the diaspora pod to use
17 (or Connection() object).
19 :param username: The username used to log in.
21 :param password: The password used to log in.
24 if type(pod
) == diaspy
.connection
.Connection
:
27 self
.connection
= diaspy
.connection
.Connection(pod
, username
, password
)
28 self
.connection
.login()
29 self
.stream
= diaspy
.streams
.Stream(self
.connection
, 'stream.json')
31 def post(self
, text
, aspect_ids
='public', photos
=None, photo
=''):
32 """This function sends a post to an aspect
34 :param text: text to post
36 :param aspect_ids: Aspect ids to send post to.
38 :param photo: path to picture file
40 :returns: diaspy.models.Post -- the Post which has been created
42 post
= self
.stream
.post(text
, aspect_ids
, photos
, photo
)
45 def get_activity(self
):
46 """This function returns activity stream.
48 :returns: diaspy.streams.Activity
50 activity
= diaspy
.streams
.Activity(self
.connection
, 'activity.json')
54 """This functions returns stream.
56 :returns: diaspy.streams.Stream
61 def get_aspects(self
):
62 """Returns aspects stream.
64 :returns: diaspy.streams.Aspects
66 return diaspy
.streams
.Aspects(self
.connection
)
68 def get_mentions(self
):
69 """Returns /mentions stream.
71 :returns: diaspy.streams.Mentions
73 return diaspy
.streams
.Mentions(self
.connection
)
75 def get_followed_tags(self
):
76 """Returns followed tags stream.
78 :returns: diaspy.streams.FollowedTags
80 return diaspy
.streams
.FollowedTags(self
.connection
)
82 def get_tag(self
, tag
):
83 """This functions returns a list of posts containing the tag.
84 :param tag: Name of the tag
87 :returns: diaspy.streams.Generic -- stream containg posts with given tag
89 return diaspy
.streams
.Generic(self
.connection
, location
='tags/{0}.json'.format(tag
))
91 def get_notifications(self
):
92 """This functions returns a list of notifications.
94 :returns: list -- list of json formatted notifications
96 r
= self
.connection
.get('notifications.json')
98 if r
.status_code
!= 200:
99 raise Exception('wrong status code: {0}'.format(r
.status_code
))
101 notifications
= r
.json()
104 def get_mailbox(self
):
105 """This functions returns a list of messages found in the conversation.
107 :returns: list -- list of Conversation objects.
109 r
= self
.connection
.get('conversations.json')
111 if r
.status_code
!= 200:
112 raise Exception('wrong status code: {0}'.format(r
.status_code
))
115 return [diaspy
.conversations
.Conversation(str(conversation
['conversation']['id']), self
.connection
)
116 for conversation
in mailbox
]
118 def add_aspect(self
, aspect_name
, visible
=0):
119 """This function adds a new aspect.
121 diaspy
.streams
.Aspects(self
.connection
).add(aspect_name
, visible
)
123 def remove_aspect(self
, aspect_id
):
124 """This function removes an aspect.
126 diaspy
.streams
.Aspects(self
.connection
).remove(aspect_id
)
128 def add_user_to_aspect(self
, user_id
, aspect_id
):
129 """ this function adds a user to an aspect.
131 :param user_id: User ID
133 :param aspect_id: Aspect ID
137 return diaspy
.models
.Aspect(self
.connection
, aspect_id
).addUser(user_id
)
139 def remove_user_from_aspect(self
, user_id
, aspect_id
):
140 """ this function removes a user from an aspect.
142 :param user_id: User ID
144 :param aspect_id: Aspect ID
148 return diaspy
.models
.Aspect(self
.connection
, aspect_id
).removeUser(user_id
)
150 def new_conversation(self
, contacts
, subject
, text
):
151 """Start a new conversation.
153 :param contacts: recipients ids, no guids, comma sperated.
155 :param subject: subject of the message.
157 :param text: text of the message.
160 data
= {'contact_ids': contacts
,
161 'conversation[subject]': subject
,
162 'conversation[text]': text
,
164 'authenticity_token': self
.connection
.get_token()}
166 r
= self
.connection
.post('conversations/',
168 headers
={'accept': 'application/json'})
169 if r
.status_code
!= 200:
170 raise Exception('{0}: Conversation could not be started.'
171 .format(r
.status_code
))