Add trending APIs

This commit is contained in:
halcy 2022-11-24 22:56:49 +02:00
parent c3cfb197a6
commit 00a96c5e95
9 changed files with 255 additions and 13 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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.
@ -1552,8 +1559,40 @@ class Mastodon:
descending.
"""
params = self.__generate_params(locals())
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
###

View File

@ -13,7 +13,7 @@ interactions:
User-Agent:
- tests/v311
method: GET
uri: http://localhost:3000/api/v1/trends
uri: http://localhost:3000/api/v1/trends/links
response:
body:
string: '[]'
@ -23,7 +23,7 @@ interactions:
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=='';
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
@ -49,9 +49,9 @@ interactions:
X-Permitted-Cross-Domain-Policies:
- none
X-Request-Id:
- 04d49bc9-1ad7-48de-a189-e93e527942e0
- 623f9c94-2540-4c4f-b0ae-1c3db0b9ca35
X-Runtime:
- '0.010606'
- '0.016081'
X-XSS-Protection:
- 1; mode=block
status:

View File

@ -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

View File

@ -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

View File

@ -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()

21
tests/test_trends.py Normal file
View File

@ -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)