3 import diaspy
.connection
4 from diaspy
import notifications
8 """This is the client class used to interact with Diaspora.
9 It can be used as a reference implementation of client using diaspy.
11 def __init__(self
, pod
, username
='', password
=''):
13 `pod` can also be a diaspy.connection.Connection type and
14 Client() will detect it. When giving a connection there is no need
15 to pass username and password.
17 :param pod: The complete url of the diaspora pod to use
18 (or Connection() object).
20 :param username: The username used to log in.
22 :param password: The password used to log in.
25 if type(pod
) == diaspy
.connection
.Connection
:
28 self
.connection
= diaspy
.connection
.Connection(pod
, username
, password
)
29 self
.connection
.login()
30 self
.stream
= diaspy
.streams
.Stream(self
.connection
, 'stream.json')
32 def post(self
, text
, aspect_ids
='public', photos
=None, photo
=''):
33 """This function sends a post to an aspect
35 :param text: text to post
37 :param aspect_ids: Aspect ids to send post to.
39 :param photo: path to picture file
41 :returns: diaspy.models.Post -- the Post which has been created
43 post
= self
.stream
.post(text
, aspect_ids
, photos
, photo
)
46 def get_activity(self
):
47 """This function returns activity stream.
49 :returns: diaspy.streams.Activity
51 return 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 return notifications
.Notifications(self
.connection
)
98 def get_mailbox(self
):
99 """This functions returns a list of messages found in the conversation.
101 :returns: list -- list of Conversation objects.
103 r
= self
.connection
.get('conversations.json')
105 if r
.status_code
!= 200:
106 raise Exception('wrong status code: {0}'.format(r
.status_code
))
109 return [diaspy
.conversations
.Conversation(self
.connection
, conversation
['conversation']['id'])
110 for conversation
in mailbox
]
112 def add_aspect(self
, aspect_name
, visible
=0):
113 """This function adds a new aspect.
115 diaspy
.streams
.Aspects(self
.connection
).add(aspect_name
, visible
)
117 def remove_aspect(self
, aspect_id
):
118 """This function removes an aspect.
120 diaspy
.streams
.Aspects(self
.connection
).remove(aspect_id
)
122 def add_user_to_aspect(self
, user_id
, aspect_id
):
123 """ this function adds a user to an aspect.
125 :param user_id: User ID
127 :param aspect_id: Aspect ID
131 return diaspy
.models
.Aspect(self
.connection
, aspect_id
).addUser(user_id
)
133 def remove_user_from_aspect(self
, user_id
, aspect_id
):
134 """ this function removes a user from an aspect.
136 :param user_id: User ID
138 :param aspect_id: Aspect ID
142 return diaspy
.models
.Aspect(self
.connection
, aspect_id
).removeUser(user_id
)
144 def new_conversation(self
, contacts
, subject
, text
):
145 """Start a new conversation.
147 :param contacts: recipients ids, no guids, comma sperated.
149 :param subject: subject of the message.
151 :param text: text of the message.
154 data
= {'contact_ids': contacts
,
155 'conversation[subject]': subject
,
156 'conversation[text]': text
,
158 'authenticity_token': self
.connection
.get_token()}
160 r
= self
.connection
.post('conversations/',
162 headers
={'accept': 'application/json'})
163 if r
.status_code
!= 200:
164 raise Exception('{0}: Conversation could not be started.'
165 .format(r
.status_code
))