From f421b1e4984e1811153032cb56e8fd191a0e6307 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sat, 12 Oct 2019 20:13:38 +0200 Subject: Add new parameter for search --- mastodon/Mastodon.py | 14 +++++++++++--- tests/cassettes/test_search.yaml | 22 +++++++++++----------- tests/test_search.py | 3 +++ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 47f76e1..c0d4c35 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1207,7 +1207,7 @@ class Mastodon: raise MastodonVersionError("Advanced search parameters require Mastodon 2.8.0+") @api_version("1.1.0", "2.8.0", __DICT_VERSION_SEARCHRESULT) - def search(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None): + def search(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None, exclude_unreviewed=True): """ Fetch matching hashtags, accounts and statuses. Will perform webfinger lookups if resolve is True. Full-text search is only enabled if @@ -1221,6 +1221,9 @@ class Mastodon: `offset`, `min_id` and `max_id` can be used to paginate. + `exclude_unreviewed` can be used to restrict search results for hashtags to only + those that have been reviewed by moderators. It is on by default. + Will use search_v1 (no tag dicts in return values) on Mastodon versions before 2.4.1), search_v2 otherwise. Parameters other than resolve are only available on Mastodon 2.8.0 or above - this function will throw a MastodonVersionError @@ -1250,10 +1253,12 @@ class Mastodon: return self.__api_request('GET', '/api/v1/search', params) @api_version("2.4.1", "2.8.0", __DICT_VERSION_SEARCHRESULT) - def search_v2(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None): + def search_v2(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None, exclude_unreviewed=True): """ Identical to `search_v1()`, except in that it returns tags as `hashtag dicts`_, has more parameters, and resolves by default. + + For more details documentation, please see `search()` Returns a `search result dict`_. """ @@ -1261,7 +1266,10 @@ class Mastodon: params = self.__generate_params(locals()) if resolve == False: - del params['resolve'] + del params["resolve"] + + if exclude_unreviewed == False or not self.verify_minimum_version("3.0.0", cached=True): + del params["exclude_unreviewed"] if "result_type" in params: params["type"] = params["result_type"] diff --git a/tests/cassettes/test_search.yaml b/tests/cassettes/test_search.yaml index 56453b3..bbb88f0 100644 --- a/tests/cassettes/test_search.yaml +++ b/tests/cassettes/test_search.yaml @@ -8,9 +8,9 @@ interactions: Connection: [keep-alive] User-Agent: [python-requests/2.18.4] method: GET - uri: http://localhost:3000/api/v2/search?resolve=1&q=mastodonpy_test + uri: http://localhost:3000/api/v2/search?exclude_unreviewed=1&resolve=1&q=mastodonpy_test response: - body: {string: '{"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"","locked":false,"bot":false,"created_at":"2019-06-22T23:11:52.441Z","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":2,"last_status_at":"2019-10-11T21:49:22.805Z","emojis":[],"fields":[]},{"id":"1234567890123457","username":"mastodonpy_test_2","acct":"mastodonpy_test_2","display_name":"","locked":true,"bot":false,"created_at":"2019-06-22T23:11:52.445Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost/@mastodonpy_test_2","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,"last_status_at":null,"emojis":[],"fields":[]}],"statuses":[],"hashtags":[]}'} + body: {string: '{"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"","locked":false,"bot":false,"created_at":"2019-06-22T23:11:52.441Z","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":1,"statuses_count":2,"last_status_at":"2019-10-11T21:50:54.637Z","emojis":[],"fields":[]},{"id":"1234567890123457","username":"mastodonpy_test_2","acct":"mastodonpy_test_2","display_name":"","locked":true,"bot":false,"created_at":"2019-06-22T23:11:52.445Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost/@mastodonpy_test_2","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,"last_status_at":null,"emojis":[],"fields":[]}],"statuses":[],"hashtags":[]}'} headers: Cache-Control: ['no-cache, no-store'] Content-Type: [application/json; charset=utf-8] @@ -21,8 +21,8 @@ interactions: X-Download-Options: [noopen] X-Frame-Options: [SAMEORIGIN] X-Permitted-Cross-Domain-Policies: [none] - X-Request-Id: [4ed49a56-2ee0-489c-822a-135ba9c9203e] - X-Runtime: ['0.028978'] + X-Request-Id: [e3981344-186b-487f-9ac5-1195c31679c7] + X-Runtime: ['0.031916'] X-XSS-Protection: [1; mode=block] content-length: ['1251'] status: {code: 200, message: OK} @@ -35,9 +35,9 @@ interactions: Connection: [keep-alive] User-Agent: [python-requests/2.18.4] method: GET - uri: http://localhost:3000/api/v2/search?resolve=1&q=mastodonpy_test + uri: http://localhost:3000/api/v2/search?exclude_unreviewed=1&resolve=1&q=mastodonpy_test response: - body: {string: '{"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"","locked":false,"bot":false,"created_at":"2019-06-22T23:11:52.441Z","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":2,"last_status_at":"2019-10-11T21:49:22.805Z","emojis":[],"fields":[]},{"id":"1234567890123457","username":"mastodonpy_test_2","acct":"mastodonpy_test_2","display_name":"","locked":true,"bot":false,"created_at":"2019-06-22T23:11:52.445Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost/@mastodonpy_test_2","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,"last_status_at":null,"emojis":[],"fields":[]}],"statuses":[],"hashtags":[]}'} + body: {string: '{"accounts":[{"id":"1234567890123456","username":"mastodonpy_test","acct":"mastodonpy_test","display_name":"","locked":false,"bot":false,"created_at":"2019-06-22T23:11:52.441Z","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":1,"statuses_count":2,"last_status_at":"2019-10-11T21:50:54.637Z","emojis":[],"fields":[]},{"id":"1234567890123457","username":"mastodonpy_test_2","acct":"mastodonpy_test_2","display_name":"","locked":true,"bot":false,"created_at":"2019-06-22T23:11:52.445Z","note":"\u003cp\u003e\u003c/p\u003e","url":"http://localhost/@mastodonpy_test_2","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,"last_status_at":null,"emojis":[],"fields":[]}],"statuses":[],"hashtags":[]}'} headers: Cache-Control: ['no-cache, no-store'] Content-Type: [application/json; charset=utf-8] @@ -48,8 +48,8 @@ interactions: X-Download-Options: [noopen] X-Frame-Options: [SAMEORIGIN] X-Permitted-Cross-Domain-Policies: [none] - X-Request-Id: [b9c4a547-27f6-4b55-9830-73a4e65d55da] - X-Runtime: ['0.026422'] + X-Request-Id: [5e62a49c-ac34-4759-8265-ffee054f1ca2] + X-Runtime: ['0.024392'] X-XSS-Protection: [1; mode=block] content-length: ['1251'] status: {code: 200, message: OK} @@ -62,7 +62,7 @@ interactions: Connection: [keep-alive] User-Agent: [python-requests/2.18.4] method: GET - uri: http://localhost:3000/api/v2/search?resolve=1&q=mastodonpy_test&type=statuses + uri: http://localhost:3000/api/v2/search?exclude_unreviewed=1&resolve=1&q=mastodonpy_test&type=statuses response: body: {string: '{"accounts":[],"statuses":[],"hashtags":[]}'} headers: @@ -75,8 +75,8 @@ interactions: X-Download-Options: [noopen] X-Frame-Options: [SAMEORIGIN] X-Permitted-Cross-Domain-Policies: [none] - X-Request-Id: [bea1135c-ef57-4e75-bbeb-c472c77273b6] - X-Runtime: ['0.016913'] + X-Request-Id: [10deaa1f-2a07-40d0-ab06-cbd50df93eb7] + X-Runtime: ['0.017407'] X-XSS-Protection: [1; mode=block] content-length: ['43'] status: {code: 200, message: OK} diff --git a/tests/test_search.py b/tests/test_search.py index a8ab33a..19d2ad4 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -15,6 +15,9 @@ def test_search(api): assert len(results["accounts"]) == 0 def test_search_pre_2_9_2(api): + api.mastodon_major = 2 + api.mastodon_minor = 9 + api.mastodon_patch = 1 with vcr.use_cassette('test_search.yaml', cassette_library_dir='tests/cassettes_pre_2_9_2', record_mode='none'): results = api.search_v1('mastodonpy_test') assert isinstance(results, dict) -- cgit v1.2.3