New methods in `diaspy.people.Contacts()` (with tests) small changes in
[diaspy.git] / diaspy / people.py
1 import re
2 from diaspy.streams import Outer
3
4
5 class User:
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.
10
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
14 exception message.
15
16 When creating new User() one can pass either guid or handle as
17 an optional parameter. GUID takes precedence over handle.
18 """
19 data = {}
20 stream = []
21
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)
28
29 def __getitem__(self, key):
30 return self.data[key]
31
32 def _sephandle(self, handle):
33 """Separate D* handle into pod pod and user.
34
35 :param handle: diaspora id: user@pod.example.com
36 :type handle: str
37 :returns: two-tuple (pod, user)
38 """
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]
43 return (pod, user)
44
45 def _postproc(self, request):
46 """Makes necessary modifications to user data and
47 sets up a stream.
48
49 :param request: request object
50 :type request: request
51 """
52 if request.status_code != 200:
53 raise Exception('wrong error code: {0}'.format(request.status_code))
54 else:
55 request = request.json()
56
57 if not len(request): raise ('Cannot extract user data: no posts to analyze')
58 data = request[0]['author']
59 final = {}
60 names = [('id', 'id'),
61 ('diaspora_id', 'diaspora_id'),
62 ('guid', 'guid'),
63 ('name', 'diaspora_name'),
64 ('avatar', 'image_urls'),
65 ]
66 for d, f in names:
67 final[f] = data[d]
68 self.data = final
69 self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid']))
70
71 def fetchhandle(self, diaspora_id, protocol='https'):
72 """Fetch user data using Diaspora handle.
73 """
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)
77
78 def fetchguid(self, guid):
79 """Fetch user data using guid.
80 """
81 request = self._connection.get('people/{0}.json'.format(guid))
82 self._postproc(request)
83
84
85 class Contacts():
86 """This class represents user's list of contacts.
87 """
88 def __init__(self, connection):
89 self._connection = connection
90
91 def get_only_sharing(self):
92 """Returns contacts who are only sharing with you.
93
94 :returns: list
95 """
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()]
101 return contacts
102
103 def get_all(self):
104 """Returns list of all contacts.
105
106 :returns: list
107 """
108 params = {'set': 'all'}
109 request = self._connection.get('contacts.json', params=params)
110 if request.status_code != 200:
111 raise Exception('status code {0}: cannot get contacts'.format(request.status_code))
112 contacts = [User(user['guid']) for user in request.json()]
113 return contacts
114
115 def get(self):
116 """Returns list of user contacts.
117 """
118 request = self._connection.get('contacts.json')
119 if request.status_code != 200:
120 raise Exception('status code {0}: cannot get contacts'.format(request.status_code))
121 contacts = [User(user['guid']) for user in request.json()]
122 return contacts