| 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """This module holds functionality related to searching. |
| 4 | """ |
| 5 | |
| 6 | |
| 7 | from diaspy import errors |
| 8 | |
| 9 | |
| 10 | class Search(): |
| 11 | """This object is used for searching for content on Diaspora*. |
| 12 | """ |
| 13 | def __init__(self, connection): |
| 14 | self._connection = connection |
| 15 | |
| 16 | def lookupUser(self, handle): |
| 17 | """This function will launch a webfinger lookup from the pod for the |
| 18 | handle requested. Response code is returned and if the lookup was successful, |
| 19 | user should soon be searchable via pod used for connection. |
| 20 | |
| 21 | :param string: Handle to search for. |
| 22 | """ |
| 23 | request = self._connection.get('people', headers={'accept': 'text/html'}, params={'q': handle}) |
| 24 | return request.status_code |
| 25 | |
| 26 | def user(self, query): |
| 27 | """Searches for a user. |
| 28 | Will return list of dictionaries containing |
| 29 | data of found users. |
| 30 | """ |
| 31 | request = self._connection.get('people.json', params={'q': query, 'utf-8': '%u2713'}) |
| 32 | if request.status_code != 200: |
| 33 | raise errors.SearchError('wrong status code: {0}'.format(request.status_code)) |
| 34 | return request.json() |
| 35 | |
| 36 | def tags(self, query, limit=10): |
| 37 | """Retrieve tag suggestions. |
| 38 | |
| 39 | :param query: query used to search |
| 40 | :type query: str |
| 41 | :param limit: maxmal number of suggestions returned |
| 42 | :type limit: int |
| 43 | """ |
| 44 | params = {'q': query, 'limit': limit} |
| 45 | request = self._connection.get('tags', params=params, headers={'x-csrf-token': repr(self._connection)}) |
| 46 | if request.status_code != 200: |
| 47 | raise errors.SearchError('wrong status code: {0}'.format(request.status_code)) |
| 48 | return [i['name'] for i in request.json()] |