2 from diaspy
.streams
import Outer
6 """This class abstracts a D* user.
7 This object goes around the limitations of current D* API and will
8 extract user data using black magic.
9 However, no chickens are harmed when you use it.
11 If user has not posted yet diaspy will not be able to extract the information
12 from his/her posts. Since there is no official way to do it we rely
13 on user posts. If this will be the case user will be notified with appropriate
16 When creating new User() one can pass either guid or handle as
17 an optional parameter. GUID takes precedence over handle.
22 def __init__(self
, connection
, guid
='', handle
=''):
23 self
._connection
= connection
24 self
.guid
, self
.handle
= guid
, handle
25 if handle
and guid
: self
.fetchguid(guid
)
26 elif guid
and not handle
: self
.fetchguid(guid
)
27 elif handle
and not guid
: self
.fetchhandle(handle
)
29 def __getitem__(self
, key
):
32 def _sephandle(self
, handle
):
33 """Separate D* handle into pod pod and user.
35 :param handle: diaspora id: user@pod.example.com
37 :returns: two-tuple (pod, user)
39 if re
.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', handle
) is None:
40 raise Exception('invalid handle: {0}'.format(handle
))
41 handle
= handle
.split('@')
42 pod
, user
= handle
[1], handle
[0]
45 def _postproc(self
, request
):
46 """Makes necessary modifications to user data and
49 :param request: request object
50 :type request: request
52 if request
.status_code
!= 200:
53 raise Exception('wrong error code: {0}'.format(request
.status_code
))
55 request
= request
.json()
57 if not len(request
): raise ('Cannot extract user data: no posts to analyze')
58 data
= request
[0]['author']
60 names
= [('id', 'id'),
61 ('diaspora_id', 'diaspora_id'),
63 ('name', 'diaspora_name'),
64 ('avatar', 'image_urls'),
69 self
.stream
= Outer(self
._connection
, location
='people/{0}.json'.format(self
.data
['guid']))
71 def fetchhandle(self
, diaspora_id
, protocol
='https'):
72 """Fetch user data using Diaspora handle.
74 pod
, user
= self
._sephandle
(diaspora_id
)
75 request
= self
._connection
.session
.get('{0}://{1}/u/{2}.json'.format(protocol
, pod
, user
))
76 self
._postproc
(request
)
78 def fetchguid(self
, guid
):
79 """Fetch user data using guid.
81 request
= self
._connection
.get('people/{0}.json'.format(guid
))
82 self
._postproc
(request
)
86 """This class represents user's list of contacts.
88 def __init__(self
, connection
):
89 self
._connection
= connection
91 def get_only_sharing(self
):
92 """Returns contacts who are only sharing with you.
96 params
= {'set':'only_sharing'}
97 request
= self
._connection
.get('contacts.json', params
=params
)
98 if request
.status_code
!= 200:
99 raise Exception('status code {0}: cannot get contacts'.format(request
.status_code
))
100 contacts
= [User(user
['guid']) for user
in request
.json()]