Improvements in `User()`, new tests, updated information about tests
[diaspy.git] / diaspy / people.py
CommitLineData
beaa09fb
MM
1import re
2from diaspy.streams import Outer
3
4
5class 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 def __init__(self, connection):
12 self._connection = connection
13 self.data = {}
141216df 14 self.stream = []
beaa09fb
MM
15
16 def __getitem__(self, key):
17 return self.data[key]
18
19 def _sephandle(self, handle):
20 """Separate D* handle into pod pod and user.
21
22 :param handle: diaspora id: user@pod.example.com
23 :type handle: str
24 :returns: two-tuple (pod, user)
25 """
26 if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', handle) is None:
27 raise Exception('invalid handle: {0}'.format(handle))
28 handle = handle.split('@')
29 pod, user = handle[1], handle[0]
30 return (pod, user)
31
32 def _gethandle(self, diaspora_id, protocol='https'):
33 """Get user data using handle.
34 """
35 pod, user = self._sephandle(diaspora_id)
141216df
MM
36 request = self._connection.session.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user))
37 if request.status_code != 200:
38 raise Exception('wrong error code: {0}'.format())
39 else:
40 request = request.json()
beaa09fb
MM
41 data = request[0]['author']
42 self.data = data
43 self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid']))
44
45 def _getguid(self, guid):
46 """Get user data using guid.
47 """
141216df
MM
48 request = self._connection.get('people/{0}.json'.format(guid))
49 if request.status_code != 200:
50 raise Exception('wrong error code: {0}'.format())
51 else:
52 request = request.json()
beaa09fb
MM
53 data = request[0]['author']
54 self.data = data
55 self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid']))
56
57 def fetchguid(self, guid):
58 """Fetch user data using guid.
59 """
60 self._getguid(guid)
61
62 def fetchhandle(self, diaspora_id, protocol='https'):
63 """Fetch user data using diaspora id.
64 """
65 self._gethandle(diaspora_id, protocol)