You can fetch tag suggestions using diaspy
[diaspy.git] / diaspy / search.py
CommitLineData
454bb4b4
MM
1#!/usr/bin/env python3
2
3"""This module holds functionality related to searching.
4"""
5
6
cf6a800f
MM
7from diaspy import errors
8
9
454bb4b4
MM
10class Search():
11 """This object is used for searching for content on Diaspora*.
12 """
a91996ea
MM
13 def __init__(self, connection):
14 self._connection = connection
15
51ab5af5 16 def lookupUser(self, handle):
a91996ea 17 """This function will launch a webfinger lookup from the pod for the
a34d1853
MM
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
a91996ea
MM
21 :param string: Handle to search for.
22 """
eb61ceda 23 request = self._connection.get('people', headers={'accept': 'text/html'}, params={'q': handle})
a34d1853 24 return request.status_code
eb61ceda 25
5131bd9e 26 def user(self, query):
7c6fbe5b
MM
27 """Searches for a user.
28 Will return list of dictionaries containing
29 data of found users.
eb61ceda 30 """
5131bd9e 31 request = self._connection.get('people.json', params={'q': query, 'utf-8': '%u2713'})
cf6a800f
MM
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:
7c6fbe5b 47 raise errors.SearchError('wrong status code: {0}'.format(request.status_code))
cf6a800f 48 return [i['name'] for i in request.json()]