def __init__(self, connection, guid='', handle='', fetch='posts', id=0):
self._connection = connection
+ self.handle = handle
+ self.guid = guid
self.data = {
'guid': guid,
'handle': handle,
'id': id,
}
- self._do_fetch(fetch)
+ self._fetch(fetch)
def __getitem__(self, key):
return self.data[key]
def __repr__(self):
return '{0} ({1})'.format(self['diaspora_name'], self['guid'])
- def _do_fetch(self, fetch):
+ def _fetch(self, fetch):
+ """Fetch user posts or data.
+ """
if fetch == 'posts':
- if self['handle'] and self['guid']: self.fetchguid()
- elif self['guid'] and not self['handle']: self.fetchguid()
- elif self['handle'] and not self['guid']: self.fetchhandle()
- elif fetch == 'data' and len(self['handle']):
+ if self.handle and not self.guid: self.fetchhandle()
+ else: self.fetchguid()
+ elif fetch == 'data' and self.handle:
self.fetchprofile()
def _sephandle(self):
:returns: two-tuple (pod, user)
"""
- if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', self['handle']) is None:
- raise Exception('invalid handle: {0}'.format(self['handle']))
- handle = self['handle'].split('@')
+ if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', self.handle) is None:
+ raise errors.UserError('invalid handle: {0}'.format(self.handle))
+ handle = self.handle.split('@')
pod, user = handle[1], handle[0]
return (pod, user)
def fetchguid(self):
"""Fetch user data and posts using guid.
"""
- request = self._connection.get('people/{0}.json'.format(self['guid']))
+ request = self._connection.get('people/{0}.json'.format(self.guid))
self._postproc(request)
def fetchprofile(self, protocol='https'):
stream.update()
post.delete()
stream.purge()
- self.assertNotIn(post.post_id, [p.post_id for p in stream])
+ self.assertNotIn(post.id, [p.id for p in stream])
def testPostingText(self):
stream = diaspy.streams.Stream(test_connection)
self.assertRaises(Exception, user._sephandle, h)
def testGettingUserByHandle(self):
- user = diaspy.people.User(test_connection)
- user.fetchhandle(testconf.diaspora_id)
+ user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
+ user.fetchhandle()
self.assertEqual(testconf.guid, user['guid'])
self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
self.assertIn('id', user.data)
self.assertEqual(type(user.stream), diaspy.streams.Outer)
def testGettingUserByGUID(self):
- user = diaspy.people.User(test_connection)
- user.fetchguid(testconf.guid)
+ user = diaspy.people.User(test_connection, guid=testconf.guid)
+ user.fetchguid()
self.assertEqual(testconf.diaspora_id, user['diaspora_id'])
self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
self.assertIn('id', user.data)