| 1 | import re |
| 2 | from diaspy.streams import Outer |
| 3 | from diaspy.models import Aspect |
| 4 | from diaspy import errors |
| 5 | from diaspy import search |
| 6 | |
| 7 | |
| 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: |
| 14 | raise errors.InvalidHandleError('{0}'.format(handle)) |
| 15 | handle = handle.split('@') |
| 16 | pod, user = handle[1], handle[0] |
| 17 | return (pod, user) |
| 18 | |
| 19 | |
| 20 | class User(): |
| 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. |
| 25 | |
| 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 |
| 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 |
| 31 | will be notified with appropriate exception message. |
| 32 | |
| 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. |
| 39 | """ |
| 40 | def __init__(self, connection, guid='', handle='', fetch='posts', id=0): |
| 41 | self._connection = connection |
| 42 | self.stream = [] |
| 43 | self.data = { |
| 44 | 'guid': guid, |
| 45 | 'handle': handle, |
| 46 | 'id': id, |
| 47 | } |
| 48 | self._fetch(fetch) |
| 49 | |
| 50 | def __getitem__(self, key): |
| 51 | return self.data[key] |
| 52 | |
| 53 | def __str__(self): |
| 54 | return self['guid'] |
| 55 | |
| 56 | def __repr__(self): |
| 57 | return '{0} ({1})'.format(self['diaspora_name'], self['guid']) |
| 58 | |
| 59 | def _fetchstream(self): |
| 60 | self.stream = Outer(self._connection, location='people/{0}.json'.format(self['guid'])) |
| 61 | |
| 62 | def _fetch(self, fetch): |
| 63 | """Fetch user posts or data. |
| 64 | """ |
| 65 | if fetch == 'posts': |
| 66 | if self['handle'] and not self['guid']: self.fetchhandle() |
| 67 | else: self.fetchguid() |
| 68 | elif fetch == 'data' and self['handle']: |
| 69 | self.fetchprofile() |
| 70 | |
| 71 | def _finalize_data(self, data): |
| 72 | """Adjustments are needed to have similar results returned |
| 73 | by search feature and fetchguid()/fetchhandle(). |
| 74 | """ |
| 75 | names = [('id', 'id'), |
| 76 | ('guid', 'guid'), |
| 77 | ('name', 'name'), |
| 78 | ('avatar', 'avatar'), |
| 79 | ('handle', 'diaspora_id'), |
| 80 | ] |
| 81 | final = {} |
| 82 | for f, d in names: |
| 83 | final[f] = data[d] |
| 84 | return final |
| 85 | |
| 86 | def _postproc(self, request): |
| 87 | """Makes necessary modifications to user data and |
| 88 | sets up a stream. |
| 89 | |
| 90 | :param request: request object |
| 91 | :type request: request |
| 92 | """ |
| 93 | if request.status_code != 200: raise Exception('wrong error code: {0}'.format(request.status_code)) |
| 94 | request = request.json() |
| 95 | if not len(request): raise errors.UserError('cannot extract user data: no posts to analyze') |
| 96 | self.data = self._finalize_data(request[0]['author']) |
| 97 | |
| 98 | def fetchhandle(self, protocol='https'): |
| 99 | """Fetch user data and posts using Diaspora handle. |
| 100 | """ |
| 101 | pod, user = sephandle(self['handle']) |
| 102 | request = self._connection.get('{0}://{1}/u/{2}.json'.format(protocol, pod, user), direct=True) |
| 103 | self._postproc(request) |
| 104 | self._fetchstream() |
| 105 | |
| 106 | def fetchguid(self): |
| 107 | """Fetch user data and posts using guid. |
| 108 | """ |
| 109 | if self['guid']: |
| 110 | request = self._connection.get('people/{0}.json'.format(self['guid'])) |
| 111 | self._postproc(request) |
| 112 | self._fetchstream() |
| 113 | else: |
| 114 | raise errors.UserError('GUID not set') |
| 115 | |
| 116 | def fetchprofile(self): |
| 117 | """Fetches user data. |
| 118 | """ |
| 119 | data = search.Search(self._connection).user(self['handle'])[0] |
| 120 | self.data = data |
| 121 | |
| 122 | def getHCard(self): |
| 123 | """Returns XML string containing user HCard. |
| 124 | """ |
| 125 | request = self._connection.get('hcard/users/{0}'.format(self['guid'])) |
| 126 | if request.status_code != 200: raise errors.UserError('could not fetch hcard for user: {0}'.format(self['guid'])) |
| 127 | return request.text |
| 128 | |
| 129 | |
| 130 | class Me(): |
| 131 | """Object represetnting current user. |
| 132 | """ |
| 133 | _userinfo_regex = re.compile(r'window.current_user_attributes = ({.*})') |
| 134 | _userinfo_regex_2 = re.compile(r'gon.user=({.*});gon.preloads') |
| 135 | |
| 136 | def __init__(self, connection): |
| 137 | self._connection = connection |
| 138 | |
| 139 | def getInfo(self): |
| 140 | """This function returns the current user's attributes. |
| 141 | |
| 142 | :returns: dict -- json formatted user info. |
| 143 | """ |
| 144 | request = self._connection.get('bookmarklet') |
| 145 | userdata = self._userinfo_regex.search(request.text) |
| 146 | if userdata is None: userdata = self._userinfo_regex_2.search(request.text) |
| 147 | if userdata is None: raise errors.DiaspyError('cannot find user data') |
| 148 | userdata = userdata.group(1) |
| 149 | return json.loads(userdata) |
| 150 | |
| 151 | |
| 152 | class Contacts(): |
| 153 | """This class represents user's list of contacts. |
| 154 | """ |
| 155 | def __init__(self, connection): |
| 156 | self._connection = connection |
| 157 | |
| 158 | def add(self, user_id, aspect_ids): |
| 159 | """Add user to aspects of given ids. |
| 160 | |
| 161 | :param user_id: user guid |
| 162 | :type user_id: str |
| 163 | :param aspect_ids: list of aspect ids |
| 164 | :type aspect_ids: list |
| 165 | """ |
| 166 | for aid in aspect_ids: Aspect(self._connection, aid).addUser(user_id) |
| 167 | |
| 168 | def remove(self, user_id, aspect_ids): |
| 169 | """Remove user from aspects of given ids. |
| 170 | |
| 171 | :param user_id: user guid |
| 172 | :type user_id: str |
| 173 | :param aspect_ids: list of aspect ids |
| 174 | :type aspect_ids: list |
| 175 | """ |
| 176 | for aid in aspect_ids: Aspect(self._connection, aid).removeUser(user_id) |
| 177 | |
| 178 | def get(self, set=''): |
| 179 | """Returns list of user contacts. |
| 180 | Contact is a User() who is in one or more of user's |
| 181 | aspects. |
| 182 | |
| 183 | By default, it will return list of users who are in |
| 184 | user's aspects. |
| 185 | |
| 186 | If `set` is `all` it will also include users who only share |
| 187 | with logged user and are not in his/hers aspects. |
| 188 | |
| 189 | If `set` is `only_sharing` it will return users who are only |
| 190 | sharing with logged user and ARE NOT in his/hers aspects. |
| 191 | |
| 192 | :param set: if passed could be 'all' or 'only_sharing' |
| 193 | :type set: str |
| 194 | """ |
| 195 | params = {} |
| 196 | if set: params['set'] = set |
| 197 | |
| 198 | request = self._connection.get('contacts.json', params=params) |
| 199 | if request.status_code != 200: |
| 200 | raise Exception('status code {0}: cannot get contacts'.format(request.status_code)) |
| 201 | return [User(self._connection, guid=user['guid'], handle=user['handle'], fetch=None) for user in request.json()] |