2 from diaspy
.streams
import Outer
3 from diaspy
.models
import Aspect
4 from diaspy
import errors
8 """This class abstracts a D* user.
9 This object goes around the limitations of current D* API and will
10 extract user data using black magic.
11 However, no chickens are harmed when you use it.
13 The parameter fetch should be either 'posts', 'data' or 'none'. By
14 default it is 'posts' which means in addition to user data, stream
15 will be fetched. If user has not posted yet diaspy will not be able
16 to extract the information from his/her posts. Since there is no official
17 way to do it we rely on user posts. If this will be the case user
18 will be notified with appropriate exception message.
20 If fetch is 'data', only user data will be fetched. If the user is
21 not found, no exception will be returned.
23 When creating new User() one can pass either guid, handle and/or id as
24 optional parameters. GUID takes precedence over handle when fetching
25 user stream. When fetching user data, handle is required.
30 def __init__(self
, connection
, guid
='', handle
='', fetch
='posts', id=0):
31 self
._connection
= connection
39 def __getitem__(self
, key
):
46 return '{0} ({1})'.format(self
['diaspora_name'], self
['guid'])
48 def _do_fetch(self
, fetch
):
50 if self
['handle'] and self
['guid']: self
.fetchguid()
51 elif self
['guid'] and not self
['handle']: self
.fetchguid()
52 elif self
['handle'] and not self
['guid']: self
.fetchhandle()
53 elif fetch
== 'data' and len(self
['handle']):
57 """Separate D* handle into pod pod and user.
59 :returns: two-tuple (pod, user)
61 if re
.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', self
['handle']) is None:
62 raise Exception('invalid handle: {0}'.format(self
['handle']))
63 handle
= self
['handle'].split('@')
64 pod
, user
= handle
[1], handle
[0]
67 def _finalize_data(self
, data
, names
):
73 def _postproc(self
, request
):
74 """Makes necessary modifications to user data and
77 :param request: request object
78 :type request: request
80 if request
.status_code
!= 200:
81 raise Exception('wrong error code: {0}'.format(request
.status_code
))
83 request
= request
.json()
84 if not len(request
): raise errors
.UserError('cannot extract user data: no posts to analyze')
85 names
= [('id', 'id'),
86 ('diaspora_id', 'diaspora_id'),
88 ('name', 'diaspora_name'),
89 ('avatar', 'image_urls'),
91 self
.data
= self
._finalize
_data
(request
[0]['author'], names
)
92 self
.stream
= Outer(self
._connection
, location
='people/{0}.json'.format(self
['guid']))
94 def fetchhandle(self
, protocol
='https'):
95 """Fetch user data and posts using Diaspora handle.
97 pod
, user
= self
._sephandle
()
98 request
= self
._connection
.session
.get('{0}://{1}/u/{2}.json'.format(protocol
, pod
, user
))
99 self
._postproc
(request
)
102 """Fetch user data and posts using guid.
104 request
= self
._connection
.get('people/{0}.json'.format(self
['guid']))
105 self
._postproc
(request
)
107 def fetchprofile(self
, protocol
='https'):
108 """Fetch user data using Diaspora handle.
110 request
= self
._connection
.get('people.json?q={0}'.format(self
['handle']))
111 if request
.status_code
!= 200:
112 raise Exception('wrong error code: {0}'.format(request
.status_code
))
114 request
= request
.json()
116 names
= [('id', 'id'),
117 ('handle', 'diaspora_id'),
119 ('name', 'diaspora_name'),
120 ('avatar', 'image_urls'),
122 self
.data
= self
._finalize
_data
(request
[0], names
)
126 """This class represents user's list of contacts.
128 def __init__(self
, connection
):
129 self
._connection
= connection
131 def add(self
, user_id
, aspect_ids
):
132 """Add user to aspects of given ids.
134 :param user_id: user guid
136 :param aspect_ids: list of aspect ids
137 :type aspect_ids: list
139 for aid
in aspect_ids
: Aspect(self
._connection
, aid
).addUser(user_id
)
141 def remove(self
, user_id
, aspect_ids
):
142 """Remove user from aspects of given ids.
144 :param user_id: user guid
146 :param aspect_ids: list of aspect ids
147 :type aspect_ids: list
149 for aid
in aspect_ids
: Aspect(self
._connection
, aid
).removeUser(user_id
)
151 def get(self
, set=''):
152 """Returns list of user contacts.
153 Contact is a User() who is in one or more of user's
156 By default, it will return list of users who are in
159 If `set` is `all` it will also include users who only share
160 with logged user and are not in his/hers aspects.
162 If `set` is `only_sharing` it will return users who are only
163 sharing with logged user and ARE NOT in his/hers aspects.
165 :param set: if passed could be 'all' or 'only_sharing'
169 if set: params
['set'] = set
171 request
= self
._connection
.get('contacts.json', params
=params
)
172 if request
.status_code
!= 200:
173 raise Exception('status code {0}: cannot get contacts'.format(request
.status_code
))
174 contacts
= [User(self
._connection
, user
['guid'], user
['handle'], 'none', user
['id']) for user
in request
.json()]