New `diaspy.people.User()` object (plus manual)
[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 = {}
14
15 def __getitem__(self, key):
16 return self.data[key]
17
18 def _sephandle(self, handle):
19 """Separate D* handle into pod pod and user.
20
21 :param handle: diaspora id: user@pod.example.com
22 :type handle: str
23 :returns: two-tuple (pod, user)
24 """
25 if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', handle) is None:
26 raise Exception('invalid handle: {0}'.format(handle))
27 handle = handle.split('@')
28 pod, user = handle[1], handle[0]
29 return (pod, user)
30
31 def _gethandle(self, diaspora_id, protocol='https'):
32 """Get user data using handle.
33 """
34 pod, user = self._sephandle(diaspora_id)
35 request = self._connection.session.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user)).json()
36 data = request[0]['author']
37 self.data = data
38 self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid']))
39
40 def _getguid(self, guid):
41 """Get user data using guid.
42 """
43 request = self._connection.get('people/{0}.json'.format(guid)).json()
44 data = request[0]['author']
45 self.data = data
46 self.stream = Outer(self._connection, location='people/{0}.json'.format(self.data['guid']))
47
48 def fetchguid(self, guid):
49 """Fetch user data using guid.
50 """
51 self._getguid(guid)
52
53 def fetchhandle(self, diaspora_id, protocol='https'):
54 """Fetch user data using diaspora id.
55 """
56 self._gethandle(diaspora_id, protocol)