aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2019-04-28 21:15:47 +0200
committerLorenz Diener <[email protected]>2019-04-28 21:15:47 +0200
commitc82b0b19196d5d390419d71e2b45f11ec1c74a05 (patch)
treec0563458afd26ddf9c7ed705fdfbbba9ccdf87ac
parentc815cdb2128da4b9a96de56f739c1f24bf01755b (diff)
downloadmastodon.py-c82b0b19196d5d390419d71e2b45f11ec1c74a05.tar.gz
Add more parameters to search API
-rw-r--r--mastodon/Mastodon.py44
-rw-r--r--tests/cassettes/test_search.yaml98
-rw-r--r--tests/test_search.py11
3 files changed, 137 insertions, 16 deletions
diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py
index 679aac8..dd710b7 100644
--- a/mastodon/Mastodon.py
+++ b/mastodon/Mastodon.py
@@ -1090,28 +1090,56 @@ class Mastodon:
1090 ### 1090 ###
1091 # Reading data: Searching 1091 # Reading data: Searching
1092 ### 1092 ###
1093 @api_version("1.1.0", "2.1.0", __DICT_VERSION_SEARCHRESULT) 1093 @api_version("2.8.0", "2.8.0", __DICT_VERSION_SEARCHRESULT)
1094 def search(self, q, resolve=False): 1094 def search(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None):
1095 """ 1095 """
1096 Fetch matching hashtags, accounts and statuses. Will search federated 1096 Fetch matching hashtags, accounts and statuses. Will perform webfinger
1097 instances if resolve is True. Full-text search is only enabled if 1097 lookups if resolve is True. Full-text search is only enabled if
1098 the instance supports it, and is restricted to statuses the logged-in 1098 the instance supports it, and is restricted to statuses the logged-in
1099 user wrote or was mentioned in. 1099 user wrote or was mentioned in.
1100
1101 `result_type` can be one of "accounts", "hashtags" or "statuses", to only
1102 search for that type of object.
1103
1104 Specify `account_id` to only get results from the account with that id.
1105
1106 `offset`, `min_id` and `max_id` can be used to paginate.
1107
1108 Returns a `search result dict`_, with tags as `hashtag dicts`_.
1109 """
1110 return self.search_v2(q, resolve=resolve, result_type=result_type, account_id=account_id,
1111 offset=offset, min_id=min_id, max_id=max_id)
1112
1113 @api_version("1.1.0", "2.1.0", __DICT_VERSION_SEARCHRESULT)
1114 def search_v1(self, q, resolve=False):
1115 """
1116 Identical to `search_v2()`, except in that it does not return
1117 tags as `hashtag dicts`_.
1100 1118
1101 Returns a `search result dict`_. 1119 Returns a `search result dict`_.
1102 """ 1120 """
1103 params = self.__generate_params(locals()) 1121 params = self.__generate_params(locals())
1122 if resolve == False:
1123 del params['resolve']
1104 return self.__api_request('GET', '/api/v1/search', params) 1124 return self.__api_request('GET', '/api/v1/search', params)
1105 1125
1106 @api_version("2.4.3", "2.4.3", __DICT_VERSION_SEARCHRESULT) 1126 @api_version("2.8.0", "2.8.0", __DICT_VERSION_SEARCHRESULT)
1107 def search_v2(self, q, resolve=False): 1127 def search_v2(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None):
1108 """ 1128 """
1109 Identical to `search()`, except in that it returns tags as 1129 Identical to `search_v1()`, except in that it returns tags as
1110 `hashtag dicts`_. 1130 `hashtag dicts`_, has more parameters, and resolves by default.
1111 1131
1112 Returns a `search result dict`_. 1132 Returns a `search result dict`_.
1113 """ 1133 """
1114 params = self.__generate_params(locals()) 1134 params = self.__generate_params(locals())
1135
1136 if resolve == False:
1137 del params['resolve']
1138
1139 if "result_type" in params:
1140 params["type"] = params["result_type"]
1141 del params["result_type"]
1142
1115 return self.__api_request('GET', '/api/v2/search', params) 1143 return self.__api_request('GET', '/api/v2/search', params)
1116 1144
1117 ### 1145 ###
diff --git a/tests/cassettes/test_search.yaml b/tests/cassettes/test_search.yaml
index b4ecccc..1983178 100644
--- a/tests/cassettes/test_search.yaml
+++ b/tests/cassettes/test_search.yaml
@@ -8,15 +8,13 @@ interactions:
8 Connection: [keep-alive] 8 Connection: [keep-alive]
9 User-Agent: [python-requests/2.18.4] 9 User-Agent: [python-requests/2.18.4]
10 method: GET 10 method: GET
11 uri: http://localhost:3000/api/v1/search?resolve=0&q=mastodonpy_test 11 uri: http://localhost:3000/api/v1/search?q=mastodonpy_test
12 response: 12 response:
13 body: {string: '{"hashtags":[],"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"John 13 body: {string: '{"hashtags":[],"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"","locked":true,"bot":false,"created_at":"2019-04-27T20:56:20.155Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost/@mastodonpy_test","avatar":"http://localhost/avatars/original/missing.png","avatar_static":"http://localhost/avatars/original/missing.png","header":"http://localhost/headers/original/missing.png","header_static":"http://localhost/headers/original/missing.png","followers_count":0,"following_count":0,"statuses_count":0,"emojis":[],"fields":[]}],"statuses":[]}'}
14 Lennon","locked":true,"bot":false,"created_at":"2019-04-27T20:03:12.393Z","note":"\u003cp\u003eI
15 walk funny\u003c/p\u003e","url":"http://localhost/@mastodonpy_test","avatar":"http://localhost/system/accounts/avatars/123/456/789/012/345/original/mastodonpyupload_.jpeg?1556389071","avatar_static":"http://localhost/system/accounts/avatars/123/456/789/012/345/original/mastodonpyupload_.jpeg?1556389071","header":"http://localhost/system/accounts/headers/123/456/789/012/345/original/mastodonpyupload_.jpeg?1556389071","header_static":"http://localhost/system/accounts/headers/123/456/789/012/345/original/mastodonpyupload_.jpeg?1556389071","followers_count":0,"following_count":0,"statuses_count":61,"emojis":[],"fields":[{"name":"bread","value":"toasty.","verified_at":null},{"name":"lasagna","value":"no!!!","verified_at":null}]}],"statuses":[]}'}
16 headers: 14 headers:
17 Cache-Control: ['max-age=0, private, must-revalidate'] 15 Cache-Control: ['max-age=0, private, must-revalidate']
18 Content-Type: [application/json; charset=utf-8] 16 Content-Type: [application/json; charset=utf-8]
19 ETag: [W/"e9c64c1dd043adc13b1f271dc991b218"] 17 ETag: [W/"9b6c418bdbb3a0abd5277c65cc102118"]
20 Referrer-Policy: [strict-origin-when-cross-origin] 18 Referrer-Policy: [strict-origin-when-cross-origin]
21 Transfer-Encoding: [chunked] 19 Transfer-Encoding: [chunked]
22 Vary: ['Accept-Encoding, Origin'] 20 Vary: ['Accept-Encoding, Origin']
@@ -24,9 +22,93 @@ interactions:
24 X-Download-Options: [noopen] 22 X-Download-Options: [noopen]
25 X-Frame-Options: [SAMEORIGIN] 23 X-Frame-Options: [SAMEORIGIN]
26 X-Permitted-Cross-Domain-Policies: [none] 24 X-Permitted-Cross-Domain-Policies: [none]
27 X-Request-Id: [4173731a-1280-4915-8023-adc11ffbcbb8] 25 X-Request-Id: [0d222257-5114-433c-b10b-1e7c849a6b9a]
28 X-Runtime: ['0.105438'] 26 X-Runtime: ['0.025732']
29 X-XSS-Protection: [1; mode=block] 27 X-XSS-Protection: [1; mode=block]
30 content-length: ['973'] 28 content-length: ['610']
29 status: {code: 200, message: OK}
30- request:
31 body: null
32 headers:
33 Accept: ['*/*']
34 Accept-Encoding: ['gzip, deflate']
35 Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN]
36 Connection: [keep-alive]
37 User-Agent: [python-requests/2.18.4]
38 method: GET
39 uri: http://localhost:3000/api/v2/search?resolve=1&q=mastodonpy_test
40 response:
41 body: {string: '{"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"","locked":true,"bot":false,"created_at":"2019-04-27T20:56:20.155Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost/@mastodonpy_test","avatar":"http://localhost/avatars/original/missing.png","avatar_static":"http://localhost/avatars/original/missing.png","header":"http://localhost/headers/original/missing.png","header_static":"http://localhost/headers/original/missing.png","followers_count":0,"following_count":0,"statuses_count":0,"emojis":[],"fields":[]}],"statuses":[],"hashtags":[]}'}
42 headers:
43 Cache-Control: ['max-age=0, private, must-revalidate']
44 Content-Type: [application/json; charset=utf-8]
45 ETag: [W/"dc01e7fe955c38320762927b42201386"]
46 Referrer-Policy: [strict-origin-when-cross-origin]
47 Transfer-Encoding: [chunked]
48 Vary: ['Accept-Encoding, Origin']
49 X-Content-Type-Options: [nosniff]
50 X-Download-Options: [noopen]
51 X-Frame-Options: [SAMEORIGIN]
52 X-Permitted-Cross-Domain-Policies: [none]
53 X-Request-Id: [fe4a8ac5-89c8-40fa-b156-b53907927af8]
54 X-Runtime: ['0.036339']
55 X-XSS-Protection: [1; mode=block]
56 content-length: ['610']
57 status: {code: 200, message: OK}
58- request:
59 body: null
60 headers:
61 Accept: ['*/*']
62 Accept-Encoding: ['gzip, deflate']
63 Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN]
64 Connection: [keep-alive]
65 User-Agent: [python-requests/2.18.4]
66 method: GET
67 uri: http://localhost:3000/api/v2/search?resolve=1&q=mastodonpy_test
68 response:
69 body: {string: '{"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"","locked":true,"bot":false,"created_at":"2019-04-27T20:56:20.155Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost/@mastodonpy_test","avatar":"http://localhost/avatars/original/missing.png","avatar_static":"http://localhost/avatars/original/missing.png","header":"http://localhost/headers/original/missing.png","header_static":"http://localhost/headers/original/missing.png","followers_count":0,"following_count":0,"statuses_count":0,"emojis":[],"fields":[]}],"statuses":[],"hashtags":[]}'}
70 headers:
71 Cache-Control: ['max-age=0, private, must-revalidate']
72 Content-Type: [application/json; charset=utf-8]
73 ETag: [W/"dc01e7fe955c38320762927b42201386"]
74 Referrer-Policy: [strict-origin-when-cross-origin]
75 Transfer-Encoding: [chunked]
76 Vary: ['Accept-Encoding, Origin']
77 X-Content-Type-Options: [nosniff]
78 X-Download-Options: [noopen]
79 X-Frame-Options: [SAMEORIGIN]
80 X-Permitted-Cross-Domain-Policies: [none]
81 X-Request-Id: [db56fc6d-bc14-4197-ad8e-526451c31ef8]
82 X-Runtime: ['0.031569']
83 X-XSS-Protection: [1; mode=block]
84 content-length: ['610']
85 status: {code: 200, message: OK}
86- request:
87 body: null
88 headers:
89 Accept: ['*/*']
90 Accept-Encoding: ['gzip, deflate']
91 Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN]
92 Connection: [keep-alive]
93 User-Agent: [python-requests/2.18.4]
94 method: GET
95 uri: http://localhost:3000/api/v2/search?resolve=1&q=mastodonpy_test&type=statuses
96 response:
97 body: {string: '{"accounts":[],"statuses":[],"hashtags":[]}'}
98 headers:
99 Cache-Control: ['max-age=0, private, must-revalidate']
100 Content-Type: [application/json; charset=utf-8]
101 ETag: [W/"2f5f5aeb866795515ef92460da252331"]
102 Referrer-Policy: [strict-origin-when-cross-origin]
103 Transfer-Encoding: [chunked]
104 Vary: ['Accept-Encoding, Origin']
105 X-Content-Type-Options: [nosniff]
106 X-Download-Options: [noopen]
107 X-Frame-Options: [SAMEORIGIN]
108 X-Permitted-Cross-Domain-Policies: [none]
109 X-Request-Id: [0af31e14-f026-43f7-9fdc-b8e3130479cf]
110 X-Runtime: ['0.043284']
111 X-XSS-Protection: [1; mode=block]
112 content-length: ['43']
31 status: {code: 200, message: OK} 113 status: {code: 200, message: OK}
32version: 1 114version: 1
diff --git a/tests/test_search.py b/tests/test_search.py
index 7a39581..f05a023 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -2,5 +2,16 @@ import pytest
2 2
3@pytest.mark.vcr() 3@pytest.mark.vcr()
4def test_search(api): 4def test_search(api):
5 results = api.search_v1('mastodonpy_test')
6 assert isinstance(results, dict)
7
8 results = api.search_v2('mastodonpy_test')
9 assert isinstance(results, dict)
10
5 results = api.search('mastodonpy_test') 11 results = api.search('mastodonpy_test')
6 assert isinstance(results, dict) 12 assert isinstance(results, dict)
13
14 results = api.search('mastodonpy_test', result_type="statuses")
15 assert isinstance(results, dict)
16 assert len(results["hashtags"]) == 0
17 assert len(results["accounts"]) == 0
Powered by cgit v1.2.3 (git 2.41.0)