From 00a96c5e9584bcd768d51372fbc4ccd866432771 Mon Sep 17 00:00:00 2001 From: halcy Date: Thu, 24 Nov 2022 22:56:49 +0200 Subject: Add trending APIs --- CHANGELOG.rst | 5 ++ TODO.md | 2 +- docs/index.rst | 3 + mastodon/Mastodon.py | 47 ++++++++++- tests/cassettes/test_trending_links.yaml | 60 ++++++++++++++ tests/cassettes/test_trending_statuses.yaml | 60 ++++++++++++++ tests/cassettes/test_trending_tags.yaml | 118 ++++++++++++++++++++++++++++ tests/cassettes/test_trends.yaml | 60 -------------- tests/test_instance.py | 4 - tests/test_trends.py | 21 +++++ 10 files changed, 311 insertions(+), 69 deletions(-) create mode 100644 tests/cassettes/test_trending_links.yaml create mode 100644 tests/cassettes/test_trending_statuses.yaml create mode 100644 tests/cassettes/test_trending_tags.yaml delete mode 100644 tests/cassettes/test_trends.yaml create mode 100644 tests/test_trends.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3079108..ad67a6d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,11 @@ A note on versioning: This librarys major version will grow with the APIs version number. Breaking changes will be indicated by a change in the minor (or major) version number, and will generally be avoided. +v1.8.0 +------ +* BREAKING CHANGE: Switch the base URL to None, throw an error when no base url is passed. Having mastosoc as default was sensible when there were only three mastodon servers. It is not sensible now and trips people up constantly. +* Fix an issue with the fix for the Pleroma date bug (thanks adbenitez) + v1.7.0 ------ * Cleaned code up a bit (thanks eumiro) diff --git a/TODO.md b/TODO.md index f603e49..e0b504a 100644 --- a/TODO.md +++ b/TODO.md @@ -66,4 +66,4 @@ General improvements that would be good to do before doing another release: * [x] Fix the CI * [ ] Get test coverage like, real high * [x] Add all those streaming events?? -* [ ] Document return values +* [ ] Document return values (skipping this for a bit to then do it at the end with tooling) diff --git a/docs/index.rst b/docs/index.rst index 7a4bb48..f9a0334 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1089,6 +1089,9 @@ Reading data: Searching Reading data: Trends -------------------- +.. automethod:: Mastodon.trending_tags +.. automethod:: Mastodon.trending_statuses +.. automethod:: Mastodon.trending_links .. automethod:: Mastodon.trends Reading data: Mutes and blocks diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index bb2d9d8..a4fe768 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -264,7 +264,7 @@ class Mastodon: Create a new app with given `client_name` and `scopes` (The basic scopes are "read", "write", "follow" and "push" - more granular scopes are available, please refer to Mastodon documentation for which) on the instance given by `api_base_url`. - + Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating in an OAuth flow. You can specify multiple URLs by passing a list. Note that if you wish to use OAuth authentication with redirects, the redirect URI must be one of the URLs specified here. @@ -1535,8 +1535,15 @@ class Mastodon: ### # Reading data: Trends ### - @api_version("2.4.3", "3.0.0", __DICT_VERSION_HASHTAG) + @api_version("2.4.3", "3.5.0", __DICT_VERSION_HASHTAG) def trends(self, limit=None): + """ + Alias for `trending_tags()`_ + """ + return self.trending_tags(limit = limit) + + @api_version("3.5.0", "3.5.0", __DICT_VERSION_HASHTAG) + def trending_tags(self, limit=None): """ Fetch trending-hashtag information, if the instance provides such information. @@ -1546,13 +1553,45 @@ class Mastodon: Does not require authentication unless locked down by the administrator. Important versioning note: This endpoint does not exist for Mastodon versions - between 2.8.0 (inclusive) and 3.0.0 (exclusive). + between 2.8.0 (inclusive) and 3.0.0 (exclusive). Returns a list of `hashtag dicts`_, sorted by the instance's trending algorithm, descending. """ params = self.__generate_params(locals()) - return self.__api_request('GET', '/api/v1/trends', params) + if self.verify_minimum_version("3.5.0", cached=True): + # Starting 3.5.0, old version is deprecated + return self.__api_request('GET', '/api/v1/trends/tags', params) + else: + return self.__api_request('GET', '/api/v1/trends', params) + + @api_version("3.5.0", "3.5.0", __DICT_VERSION_STATUS) + def trending_statuses(self): + """ + Fetch trending-status information, if the instance provides such information. + + Specify `limit` to limit how many results are returned (the maximum number + of results is 10, the endpoint is not paginated). + + Returns a list of `status dicts`_, sorted by the instances's trending algorithm, + descending. + """ + params = self.__generate_params(locals()) + return self.__api_request('GET', '/api/v1/trends/statuses', params) + + @api_version("3.5.0", "3.5.0", __DICT_VERSION_CARD) + def trending_links(self): + """ + Fetch trending-link information, if the instance provides such information. + + Specify `limit` to limit how many results are returned (the maximum number + of results is 10, the endpoint is not paginated). + + Returns a list of `card dicts`_, sorted by the instances's trending algorithm, + descending. + """ + params = self.__generate_params(locals()) + return self.__api_request('GET', '/api/v1/trends/links', params) ### # Reading data: Lists diff --git a/tests/cassettes/test_trending_links.yaml b/tests/cassettes/test_trending_links.yaml new file mode 100644 index 0000000..3d54b25 --- /dev/null +++ b/tests/cassettes/test_trending_links.yaml @@ -0,0 +1,60 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Authorization: + - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN + Connection: + - keep-alive + User-Agent: + - tests/v311 + method: GET + uri: http://localhost:3000/api/v1/trends/links + response: + body: + string: '[]' + headers: + Cache-Control: + - no-store + Content-Security-Policy: + - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src + ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000; + style-src ''self'' http://localhost:3000 ''nonce-7UiLUEyJEVY9JgarYKBdCg==''; + media-src ''self'' https: data: http://localhost:3000; frame-src ''self'' + https:; manifest-src ''self'' http://localhost:3000; connect-src ''self'' + data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000 + ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline'' + ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000; + worker-src ''self'' blob: http://localhost:3000' + Content-Type: + - application/json; charset=utf-8 + ETag: + - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5" + Referrer-Policy: + - strict-origin-when-cross-origin + Transfer-Encoding: + - chunked + Vary: + - Accept, Origin + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Frame-Options: + - SAMEORIGIN + X-Permitted-Cross-Domain-Policies: + - none + X-Request-Id: + - 623f9c94-2540-4c4f-b0ae-1c3db0b9ca35 + X-Runtime: + - '0.016081' + X-XSS-Protection: + - 1; mode=block + status: + code: 200 + message: OK +version: 1 diff --git a/tests/cassettes/test_trending_statuses.yaml b/tests/cassettes/test_trending_statuses.yaml new file mode 100644 index 0000000..493555d --- /dev/null +++ b/tests/cassettes/test_trending_statuses.yaml @@ -0,0 +1,60 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Authorization: + - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN + Connection: + - keep-alive + User-Agent: + - tests/v311 + method: GET + uri: http://localhost:3000/api/v1/trends/statuses + response: + body: + string: '[]' + headers: + Cache-Control: + - no-store + Content-Security-Policy: + - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src + ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000; + style-src ''self'' http://localhost:3000 ''nonce-Qp9U6bbMsEloa3BOJP+ApA==''; + media-src ''self'' https: data: http://localhost:3000; frame-src ''self'' + https:; manifest-src ''self'' http://localhost:3000; connect-src ''self'' + data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000 + ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline'' + ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000; + worker-src ''self'' blob: http://localhost:3000' + Content-Type: + - application/json; charset=utf-8 + ETag: + - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5" + Referrer-Policy: + - strict-origin-when-cross-origin + Transfer-Encoding: + - chunked + Vary: + - Accept, Origin + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Frame-Options: + - SAMEORIGIN + X-Permitted-Cross-Domain-Policies: + - none + X-Request-Id: + - 9b65a082-dbab-436e-9544-9d3ceae169b0 + X-Runtime: + - '0.022649' + X-XSS-Protection: + - 1; mode=block + status: + code: 200 + message: OK +version: 1 diff --git a/tests/cassettes/test_trending_tags.yaml b/tests/cassettes/test_trending_tags.yaml new file mode 100644 index 0000000..583aeda --- /dev/null +++ b/tests/cassettes/test_trending_tags.yaml @@ -0,0 +1,118 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Authorization: + - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN + Connection: + - keep-alive + User-Agent: + - tests/v311 + method: GET + uri: http://localhost:3000/api/v1/trends/tags + response: + body: + string: '[]' + headers: + Cache-Control: + - no-store + Content-Security-Policy: + - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src + ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000; + style-src ''self'' http://localhost:3000 ''nonce-x84HIusoB0oMG/yq6Q4NDg==''; + media-src ''self'' https: data: http://localhost:3000; frame-src ''self'' + https:; manifest-src ''self'' http://localhost:3000; connect-src ''self'' + data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000 + ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline'' + ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000; + worker-src ''self'' blob: http://localhost:3000' + Content-Type: + - application/json; charset=utf-8 + ETag: + - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5" + Referrer-Policy: + - strict-origin-when-cross-origin + Transfer-Encoding: + - chunked + Vary: + - Accept, Origin + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Frame-Options: + - SAMEORIGIN + X-Permitted-Cross-Domain-Policies: + - none + X-Request-Id: + - e26034bb-bfdb-4f3a-bbef-9d2e51f88c14 + X-Runtime: + - '0.029510' + X-XSS-Protection: + - 1; mode=block + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Authorization: + - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN + Connection: + - keep-alive + User-Agent: + - tests/v311 + method: GET + uri: http://localhost:3000/api/v1/trends/tags + response: + body: + string: '[]' + headers: + Cache-Control: + - no-store + Content-Security-Policy: + - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src + ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000; + style-src ''self'' http://localhost:3000 ''nonce-mRWh2zKUzi/z+WYqVSZD1g==''; + media-src ''self'' https: data: http://localhost:3000; frame-src ''self'' + https:; manifest-src ''self'' http://localhost:3000; connect-src ''self'' + data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000 + ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline'' + ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000; + worker-src ''self'' blob: http://localhost:3000' + Content-Type: + - application/json; charset=utf-8 + ETag: + - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5" + Referrer-Policy: + - strict-origin-when-cross-origin + Transfer-Encoding: + - chunked + Vary: + - Accept, Origin + X-Content-Type-Options: + - nosniff + X-Download-Options: + - noopen + X-Frame-Options: + - SAMEORIGIN + X-Permitted-Cross-Domain-Policies: + - none + X-Request-Id: + - c1cc20bc-a0b0-4ffa-bd72-686a094e1da0 + X-Runtime: + - '0.015007' + X-XSS-Protection: + - 1; mode=block + status: + code: 200 + message: OK +version: 1 diff --git a/tests/cassettes/test_trends.yaml b/tests/cassettes/test_trends.yaml deleted file mode 100644 index 41cf4af..0000000 --- a/tests/cassettes/test_trends.yaml +++ /dev/null @@ -1,60 +0,0 @@ -interactions: -- request: - body: null - headers: - Accept: - - '*/*' - Accept-Encoding: - - gzip, deflate - Authorization: - - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN - Connection: - - keep-alive - User-Agent: - - tests/v311 - method: GET - uri: http://localhost:3000/api/v1/trends - response: - body: - string: '[]' - headers: - Cache-Control: - - no-store - Content-Security-Policy: - - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src - ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000; - style-src ''self'' http://localhost:3000 ''nonce-wweJU6CNiZOq8p6GOgvKMg==''; - media-src ''self'' https: data: http://localhost:3000; frame-src ''self'' - https:; manifest-src ''self'' http://localhost:3000; connect-src ''self'' - data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000 - ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline'' - ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000; - worker-src ''self'' blob: http://localhost:3000' - Content-Type: - - application/json; charset=utf-8 - ETag: - - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5" - Referrer-Policy: - - strict-origin-when-cross-origin - Transfer-Encoding: - - chunked - Vary: - - Accept, Origin - X-Content-Type-Options: - - nosniff - X-Download-Options: - - noopen - X-Frame-Options: - - SAMEORIGIN - X-Permitted-Cross-Domain-Policies: - - none - X-Request-Id: - - 04d49bc9-1ad7-48de-a189-e93e527942e0 - X-Runtime: - - '0.010606' - X-XSS-Protection: - - 1; mode=block - status: - code: 200 - message: OK -version: 1 diff --git a/tests/test_instance.py b/tests/test_instance.py index e25a686..99a3534 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -62,10 +62,6 @@ def test_nodeinfo(api): assert nodeinfo assert nodeinfo.version == '2.0' -@pytest.mark.vcr() -def test_trends(api): - assert isinstance(api.trends(), list) - @pytest.mark.vcr() def test_directory(api): directory = api.directory() diff --git a/tests/test_trends.py b/tests/test_trends.py new file mode 100644 index 0000000..67599d2 --- /dev/null +++ b/tests/test_trends.py @@ -0,0 +1,21 @@ +import pytest +import time +import vcr + + +@pytest.mark.vcr() +def test_trending_tags(api): + tags = api.trending_tags() + assert isinstance(tags, list) + tags = api.trends() + assert isinstance(tags, list) + +@pytest.mark.vcr() +def test_trending_statuses(api): + statuses = api.trending_statuses() + assert isinstance(statuses, list) + +@pytest.mark.vcr() +def test_trending_links(api): + links = api.trending_links() + assert isinstance(links, list) -- cgit v1.2.3