2 from diaspy
.streams
import Outer
3 from diaspy
.models
import Aspect
7 """This class abstracts a D* user.
8 This object goes around the limitations of current D* API and will
9 extract user data using black magic.
10 However, no chickens are harmed when you use it.
12 If user has not posted yet diaspy will not be able to extract the information
13 from his/her posts. Since there is no official way to do it we rely
14 on user posts. If this will be the case user will be notified with appropriate
17 When creating new User() one can pass either guid or handle as
18 an optional parameter. GUID takes precedence over handle.
23 def __init__(self
, connection
, guid
='', handle
=''):
24 self
._connection
= connection
25 self
.guid
, self
.handle
= guid
, handle
26 if handle
and guid
: self
.fetchguid(guid
)
27 elif guid
and not handle
: self
.fetchguid(guid
)
28 elif handle
and not guid
: self
.fetchhandle(handle
)
30 def __getitem__(self
, key
):
33 def _sephandle(self
, handle
):
34 """Separate D* handle into pod pod and user.
36 :param handle: diaspora id: user@pod.example.com
38 :returns: two-tuple (pod, user)
40 if re
.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', handle
) is None:
41 raise Exception('invalid handle: {0}'.format(handle
))
42 handle
= handle
.split('@')
43 pod
, user
= handle
[1], handle
[0]
46 def _postproc(self
, request
):
47 """Makes necessary modifications to user data and
50 :param request: request object
51 :type request: request
53 if request
.status_code
!= 200:
54 raise Exception('wrong error code: {0}'.format(request
.status_code
))
56 request
= request
.json()
58 if not len(request
): raise ('Cannot extract user data: no posts to analyze')
59 data
= request
[0]['author']
61 names
= [('id', 'id'),
62 ('diaspora_id', 'diaspora_id'),
64 ('name', 'diaspora_name'),
65 ('avatar', 'image_urls'),
70 self
.stream
= Outer(self
._connection
, location
='people/{0}.json'.format(self
.data
['guid']))
72 def fetchhandle(self
, diaspora_id
, protocol
='https'):
73 """Fetch user data using Diaspora handle.
75 pod
, user
= self
._sephandle
(diaspora_id
)
76 request
= self
._connection
.session
.get('{0}://{1}/u/{2}.json'.format(protocol
, pod
, user
))
77 self
._postproc
(request
)
79 def fetchguid(self
, guid
):
80 """Fetch user data using guid.
82 request
= self
._connection
.get('people/{0}.json'.format(guid
))
83 self
._postproc
(request
)
87 """This class represents user's list of contacts.
89 def __init__(self
, connection
):
90 self
._connection
= connection
92 def add(self
, user_id
, aspect_ids
):
93 """Add user to aspects of given ids.
95 :param user_id: user guid
97 :param aspect_ids: list of aspect ids
98 :type aspect_ids: list
100 for aid
in aspect_ids
: Aspect(self
._connection
, aid
).addUser(user_id
)
102 def remove(self
, user_id
, aspect_ids
):
103 """Remove user from aspects of given ids.
105 :param user_id: user guid
107 :param aspect_ids: list of aspect ids
108 :type aspect_ids: list
110 for aid
in aspect_ids
: Aspect(self
._connection
, aid
).removeUser(user_id
)
112 def get(self
, set=''):
113 """Returns list of user contacts.
114 Contact is a User() who is in one or more of user's
117 By default, it will return list of users who are in
120 If `set` is `all` it will also include users who only share
121 with logged user and are not in his/hers aspects.
123 If `set` is `only_sharing` it will return users who are only
124 sharing with logged user and ARE NOT in his/hers aspects.
126 :param set: if passed could be 'all' or 'only_sharing'
130 if set: params
['set'] = set
132 request
= self
._connection
.get('contacts.json', params
=params
)
133 if request
.status_code
!= 200:
134 raise Exception('status code {0}: cannot get contacts'.format(request
.status_code
))
135 contacts
= [User(user
['guid']) for user
in request
.json()]