f3d0048467e4a78f36cc9e4092fe3047e7c840df
2 from diaspy
.streams
import Outer
3 from diaspy
.models
import Aspect
4 from diaspy
import errors
5 from diaspy
import search
9 """This class abstracts a D* user.
10 This object goes around the limitations of current D* API and will
11 extract user data using black magic.
12 However, no chickens are harmed when you use it.
14 The parameter fetch should be either 'posts', 'data' or 'none'. By
15 default it is 'posts' which means in addition to user data, stream
16 will be fetched. If user has not posted yet diaspy will not be able
17 to extract the information from his/her posts. Since there is no official
18 way to do it we rely on user posts. If this will be the case user
19 will be notified with appropriate exception message.
21 If fetch is 'data', only user data will be fetched. If the user is
22 not found, no exception will be returned.
24 When creating new User() one can pass either guid, handle and/or id as
25 optional parameters. GUID takes precedence over handle when fetching
26 user stream. When fetching user data, handle is required.
28 def __init__(self
, connection
, guid
='', handle
='', fetch
='posts', id=0):
29 self
._connection
= connection
38 def __getitem__(self
, key
):
45 return '{0} ({1})'.format(self
['diaspora_name'], self
['guid'])
47 def _do_fetch(self
, fetch
):
49 if self
['handle'] and self
['guid']: self
.fetchguid()
50 elif self
['guid'] and not self
['handle']: self
.fetchguid()
51 elif self
['handle'] and not self
['guid']: self
.fetchhandle()
52 elif fetch
== 'data' and len(self
['handle']):
53 self
._postproc
(search
.Search(self
._connection
).users(query
=handle
)[0])
56 """Separate D* handle into pod pod and user.
58 :returns: two-tuple (pod, user)
60 if re
.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', self
['handle']) is None:
61 raise Exception('invalid handle: {0}'.format(self
['handle']))
62 handle
= self
['handle'].split('@')
63 pod
, user
= handle
[1], handle
[0]
66 def _finalize_data(self
, data
):
67 names
= [('id', 'id'),
68 ('handle', 'diaspora_id'),
70 ('name', 'diaspora_name'),
71 ('avatar', 'image_urls'),
78 def _postproc(self
, request
):
79 """Makes necessary modifications to user data and
82 :param request: request object
83 :type request: request
85 if request
.status_code
!= 200:
86 raise Exception('wrong error code: {0}'.format(request
.status_code
))
88 request
= request
.json()
89 if not len(request
): raise errors
.UserError('cannot extract user data: no posts to analyze')
90 self
.data
= self
._finalize
_data
(request
[0]['author'])
91 self
.stream
= Outer(self
._connection
, location
='people/{0}.json'.format(self
['guid']))
93 def fetchhandle(self
, protocol
='https'):
94 """Fetch user data and posts using Diaspora handle.
96 pod
, user
= self
._sephandle
()
97 request
= self
._connection
.session
.get('{0}://{1}/u/{2}.json'.format(protocol
, pod
, user
))
98 self
._postproc
(request
)
101 """Fetch user data and posts using guid.
103 request
= self
._connection
.get('people/{0}.json'.format(self
['guid']))
104 self
._postproc
(request
)
108 """This class represents user's list of contacts.
110 def __init__(self
, connection
):
111 self
._connection
= connection
113 def add(self
, user_id
, aspect_ids
):
114 """Add user to aspects of given ids.
116 :param user_id: user guid
118 :param aspect_ids: list of aspect ids
119 :type aspect_ids: list
121 for aid
in aspect_ids
: Aspect(self
._connection
, aid
).addUser(user_id
)
123 def remove(self
, user_id
, aspect_ids
):
124 """Remove user from aspects of given ids.
126 :param user_id: user guid
128 :param aspect_ids: list of aspect ids
129 :type aspect_ids: list
131 for aid
in aspect_ids
: Aspect(self
._connection
, aid
).removeUser(user_id
)
133 def get(self
, set=''):
134 """Returns list of user contacts.
135 Contact is a User() who is in one or more of user's
138 By default, it will return list of users who are in
141 If `set` is `all` it will also include users who only share
142 with logged user and are not in his/hers aspects.
144 If `set` is `only_sharing` it will return users who are only
145 sharing with logged user and ARE NOT in his/hers aspects.
147 :param set: if passed could be 'all' or 'only_sharing'
151 if set: params
['set'] = set
153 request
= self
._connection
.get('contacts.json', params
=params
)
154 if request
.status_code
!= 200:
155 raise Exception('status code {0}: cannot get contacts'.format(request
.status_code
))
156 contacts
= [User(self
._connection
, user
['guid'], user
['handle'], 'none', user
['id']) for user
in request
.json()]