Commit | Line | Data |
---|---|---|
beaa09fb MM |
1 | import re |
2 | from diaspy.streams import Outer | |
d589deff | 3 | from diaspy.models import Aspect |
65b1f099 | 4 | from diaspy import errors |
7c6fbe5b | 5 | from diaspy import search |
beaa09fb MM |
6 | |
7 | ||
6d8d47ce MM |
8 | def sephandle(handle): |
9 | """Separate Diaspora* handle into pod pod and user. | |
10 | ||
11 | :returns: two-tuple (pod, user) | |
12 | """ | |
13 | if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', handle) is None: | |
615edb73 | 14 | raise errors.InvalidHandleError('{0}'.format(handle)) |
6d8d47ce MM |
15 | handle = handle.split('@') |
16 | pod, user = handle[1], handle[0] | |
17 | return (pod, user) | |
18 | ||
19 | ||
488d7ff6 | 20 | class User(): |
beaa09fb MM |
21 | """This class abstracts a D* user. |
22 | This object goes around the limitations of current D* API and will | |
23 | extract user data using black magic. | |
24 | However, no chickens are harmed when you use it. | |
17d8f406 | 25 | |
7b99bf75 JR |
26 | The parameter fetch should be either 'posts', 'data' or 'none'. By |
27 | default it is 'posts' which means in addition to user data, stream | |
3cf4514e MM |
28 | will be fetched. If user has not posted yet diaspy will not be able |
29 | to extract the information from his/her posts. Since there is no official | |
30 | way to do it we rely on user posts. If this will be the case user | |
7b99bf75 | 31 | will be notified with appropriate exception message. |
3cf4514e | 32 | |
7b99bf75 JR |
33 | If fetch is 'data', only user data will be fetched. If the user is |
34 | not found, no exception will be returned. | |
35 | ||
36 | When creating new User() one can pass either guid, handle and/or id as | |
37 | optional parameters. GUID takes precedence over handle when fetching | |
38 | user stream. When fetching user data, handle is required. | |
beaa09fb | 39 | """ |
7b99bf75 | 40 | def __init__(self, connection, guid='', handle='', fetch='posts', id=0): |
beaa09fb | 41 | self._connection = connection |
6cd1bae0 | 42 | self.stream = [] |
b9fb4030 JR |
43 | self.data = { |
44 | 'guid': guid, | |
45 | 'handle': handle, | |
1e192d06 | 46 | 'id': id, |
b9fb4030 | 47 | } |
9aa1c960 | 48 | self._fetch(fetch) |
beaa09fb MM |
49 | |
50 | def __getitem__(self, key): | |
51 | return self.data[key] | |
52 | ||
1e192d06 MM |
53 | def __str__(self): |
54 | return self['guid'] | |
55 | ||
56 | def __repr__(self): | |
57 | return '{0} ({1})'.format(self['diaspora_name'], self['guid']) | |
58 | ||
9aa1c960 MM |
59 | def _fetch(self, fetch): |
60 | """Fetch user posts or data. | |
61 | """ | |
b9fb4030 | 62 | if fetch == 'posts': |
f50cbea3 | 63 | if self['handle'] and not self['guid']: self.fetchhandle() |
9aa1c960 | 64 | else: self.fetchguid() |
f50cbea3 | 65 | elif fetch == 'data' and self['handle']: |
1f779f83 | 66 | self.fetchprofile() |
b9fb4030 | 67 | |
7c6fbe5b | 68 | def _finalize_data(self, data): |
6d8d47ce | 69 | """Adjustments are needed to have similar results returned |
f50cbea3 | 70 | by search feature and fetchguid()/fetchhandle(). |
6d8d47ce | 71 | """ |
f50cbea3 MM |
72 | names = [('id', 'id'), |
73 | ('guid', 'guid'), | |
74 | ('name', 'name'), | |
75 | ('avatar', 'avatar'), | |
76 | ('handle', 'diaspora_id'), | |
77 | ] | |
7b99bf75 | 78 | final = {} |
6d8d47ce | 79 | for f, d in names: |
7b99bf75 JR |
80 | final[f] = data[d] |
81 | return final | |
beaa09fb | 82 | |
a8fdc14a MM |
83 | def _postproc(self, request): |
84 | """Makes necessary modifications to user data and | |
85 | sets up a stream. | |
86 | ||
87 | :param request: request object | |
88 | :type request: request | |
beaa09fb | 89 | """ |
6d8d47ce | 90 | if request.status_code != 200: raise Exception('wrong error code: {0}'.format(request.status_code)) |
f50cbea3 | 91 | request = request.json() |
65b1f099 | 92 | if not len(request): raise errors.UserError('cannot extract user data: no posts to analyze') |
7c6fbe5b | 93 | self.data = self._finalize_data(request[0]['author']) |
1f779f83 | 94 | self.stream = Outer(self._connection, location='people/{0}.json'.format(self['guid'])) |
beaa09fb | 95 | |
1f779f83 | 96 | def fetchhandle(self, protocol='https'): |
7b99bf75 | 97 | """Fetch user data and posts using Diaspora handle. |
beaa09fb | 98 | """ |
f50cbea3 | 99 | pod, user = sephandle(self['handle']) |
73a9e0d3 | 100 | request = self._connection.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user), direct=True) |
a8fdc14a | 101 | self._postproc(request) |
beaa09fb | 102 | |
1f779f83 | 103 | def fetchguid(self): |
7b99bf75 | 104 | """Fetch user data and posts using guid. |
beaa09fb | 105 | """ |
f50cbea3 MM |
106 | if self['guid']: |
107 | request = self._connection.get('people/{0}.json'.format(self['guid'])) | |
3311aa0c MM |
108 | self._postproc(request) |
109 | else: | |
110 | raise errors.UserError('GUID not set') | |
3cf4514e | 111 | |
5131bd9e MM |
112 | def fetchprofile(self): |
113 | """Fetches user data. | |
114 | """ | |
f50cbea3 | 115 | data = search.Search(self._connection).user(self['handle'])[0] |
6d8d47ce | 116 | self.data = data |
5131bd9e | 117 | |
dd0a4d9f MM |
118 | |
119 | class Contacts(): | |
120 | """This class represents user's list of contacts. | |
121 | """ | |
122 | def __init__(self, connection): | |
123 | self._connection = connection | |
124 | ||
d589deff MM |
125 | def add(self, user_id, aspect_ids): |
126 | """Add user to aspects of given ids. | |
dd0a4d9f | 127 | |
d589deff MM |
128 | :param user_id: user guid |
129 | :type user_id: str | |
130 | :param aspect_ids: list of aspect ids | |
131 | :type aspect_ids: list | |
dd0a4d9f | 132 | """ |
7a818fdb | 133 | for aid in aspect_ids: Aspect(self._connection, aid).addUser(user_id) |
27f09973 | 134 | |
d589deff MM |
135 | def remove(self, user_id, aspect_ids): |
136 | """Remove user from aspects of given ids. | |
27f09973 | 137 | |
d589deff MM |
138 | :param user_id: user guid |
139 | :type user_id: str | |
140 | :param aspect_ids: list of aspect ids | |
141 | :type aspect_ids: list | |
27f09973 | 142 | """ |
7a818fdb | 143 | for aid in aspect_ids: Aspect(self._connection, aid).removeUser(user_id) |
27f09973 | 144 | |
d589deff | 145 | def get(self, set=''): |
27f09973 | 146 | """Returns list of user contacts. |
d589deff MM |
147 | Contact is a User() who is in one or more of user's |
148 | aspects. | |
149 | ||
7a818fdb MM |
150 | By default, it will return list of users who are in |
151 | user's aspects. | |
152 | ||
d589deff MM |
153 | If `set` is `all` it will also include users who only share |
154 | with logged user and are not in his/hers aspects. | |
7a818fdb | 155 | |
d589deff MM |
156 | If `set` is `only_sharing` it will return users who are only |
157 | sharing with logged user and ARE NOT in his/hers aspects. | |
158 | ||
159 | :param set: if passed could be 'all' or 'only_sharing' | |
160 | :type set: str | |
27f09973 | 161 | """ |
d589deff MM |
162 | params = {} |
163 | if set: params['set'] = set | |
164 | ||
165 | request = self._connection.get('contacts.json', params=params) | |
27f09973 MM |
166 | if request.status_code != 200: |
167 | raise Exception('status code {0}: cannot get contacts'.format(request.status_code)) | |
f50cbea3 | 168 | return [User(self._connection, guid=user['guid'], handle=user['handle'], fetch=None) for user in request.json()] |