aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhalcy <halcy@ARARAGI-KUN>2022-11-24 22:56:49 +0200
committerhalcy <halcy@ARARAGI-KUN>2022-11-24 22:56:49 +0200
commit00a96c5e9584bcd768d51372fbc4ccd866432771 (patch)
treef979bcc269b3f4a65d6bc56bfff14a3f91f39866
parentc3cfb197a6afaf441d452fa57cb1bbee211ca4b4 (diff)
downloadmastodon.py-00a96c5e9584bcd768d51372fbc4ccd866432771.tar.gz
Add trending APIs
-rw-r--r--CHANGELOG.rst5
-rw-r--r--TODO.md2
-rw-r--r--docs/index.rst3
-rw-r--r--mastodon/Mastodon.py47
-rw-r--r--tests/cassettes/test_trending_links.yaml (renamed from tests/cassettes/test_trends.yaml)8
-rw-r--r--tests/cassettes/test_trending_statuses.yaml60
-rw-r--r--tests/cassettes/test_trending_tags.yaml118
-rw-r--r--tests/test_instance.py4
-rw-r--r--tests/test_trends.py21
9 files changed, 255 insertions, 13 deletions
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
2version number. Breaking changes will be indicated by a change in the minor 2version number. Breaking changes will be indicated by a change in the minor
3(or major) version number, and will generally be avoided. 3(or major) version number, and will generally be avoided.
4 4
5v1.8.0
6------
7* 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.
8* Fix an issue with the fix for the Pleroma date bug (thanks adbenitez)
9
5v1.7.0 10v1.7.0
6------ 11------
7* Cleaned code up a bit (thanks eumiro) 12* 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:
66* [x] Fix the CI 66* [x] Fix the CI
67* [ ] Get test coverage like, real high 67* [ ] Get test coverage like, real high
68* [x] Add all those streaming events?? 68* [x] Add all those streaming events??
69* [ ] Document return values 69* [ ] 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
1089Reading data: Trends 1089Reading data: Trends
1090-------------------- 1090--------------------
1091 1091
1092.. automethod:: Mastodon.trending_tags
1093.. automethod:: Mastodon.trending_statuses
1094.. automethod:: Mastodon.trending_links
1092.. automethod:: Mastodon.trends 1095.. automethod:: Mastodon.trends
1093 1096
1094Reading data: Mutes and blocks 1097Reading 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:
264 Create a new app with given `client_name` and `scopes` (The basic scopes are "read", "write", "follow" and "push" 264 Create a new app with given `client_name` and `scopes` (The basic scopes are "read", "write", "follow" and "push"
265 - more granular scopes are available, please refer to Mastodon documentation for which) on the instance given 265 - more granular scopes are available, please refer to Mastodon documentation for which) on the instance given
266 by `api_base_url`. 266 by `api_base_url`.
267 267
268 Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating in an OAuth flow. 268 Specify `redirect_uris` if you want users to be redirected to a certain page after authenticating in an OAuth flow.
269 You can specify multiple URLs by passing a list. Note that if you wish to use OAuth authentication with redirects, 269 You can specify multiple URLs by passing a list. Note that if you wish to use OAuth authentication with redirects,
270 the redirect URI must be one of the URLs specified here. 270 the redirect URI must be one of the URLs specified here.
@@ -1535,9 +1535,16 @@ class Mastodon:
1535 ### 1535 ###
1536 # Reading data: Trends 1536 # Reading data: Trends
1537 ### 1537 ###
1538 @api_version("2.4.3", "3.0.0", __DICT_VERSION_HASHTAG) 1538 @api_version("2.4.3", "3.5.0", __DICT_VERSION_HASHTAG)
1539 def trends(self, limit=None): 1539 def trends(self, limit=None):
1540 """ 1540 """
1541 Alias for `trending_tags()`_
1542 """
1543 return self.trending_tags(limit = limit)
1544
1545 @api_version("3.5.0", "3.5.0", __DICT_VERSION_HASHTAG)
1546 def trending_tags(self, limit=None):
1547 """
1541 Fetch trending-hashtag information, if the instance provides such information. 1548 Fetch trending-hashtag information, if the instance provides such information.
1542 1549
1543 Specify `limit` to limit how many results are returned (the maximum number 1550 Specify `limit` to limit how many results are returned (the maximum number
@@ -1546,13 +1553,45 @@ class Mastodon:
1546 Does not require authentication unless locked down by the administrator. 1553 Does not require authentication unless locked down by the administrator.
1547 1554
1548 Important versioning note: This endpoint does not exist for Mastodon versions 1555 Important versioning note: This endpoint does not exist for Mastodon versions
1549 between 2.8.0 (inclusive) and 3.0.0 (exclusive). 1556 between 2.8.0 (inclusive) and 3.0.0 (exclusive).
1550 1557
1551 Returns a list of `hashtag dicts`_, sorted by the instance's trending algorithm, 1558 Returns a list of `hashtag dicts`_, sorted by the instance's trending algorithm,
1552 descending. 1559 descending.
1553 """ 1560 """
1554 params = self.__generate_params(locals()) 1561 params = self.__generate_params(locals())
1555 return self.__api_request('GET', '/api/v1/trends', params) 1562 if self.verify_minimum_version("3.5.0", cached=True):
1563 # Starting 3.5.0, old version is deprecated
1564 return self.__api_request('GET', '/api/v1/trends/tags', params)
1565 else:
1566 return self.__api_request('GET', '/api/v1/trends', params)
1567
1568 @api_version("3.5.0", "3.5.0", __DICT_VERSION_STATUS)
1569 def trending_statuses(self):
1570 """
1571 Fetch trending-status information, if the instance provides such information.
1572
1573 Specify `limit` to limit how many results are returned (the maximum number
1574 of results is 10, the endpoint is not paginated).
1575
1576 Returns a list of `status dicts`_, sorted by the instances's trending algorithm,
1577 descending.
1578 """
1579 params = self.__generate_params(locals())
1580 return self.__api_request('GET', '/api/v1/trends/statuses', params)
1581
1582 @api_version("3.5.0", "3.5.0", __DICT_VERSION_CARD)
1583 def trending_links(self):
1584 """
1585 Fetch trending-link information, if the instance provides such information.
1586
1587 Specify `limit` to limit how many results are returned (the maximum number
1588 of results is 10, the endpoint is not paginated).
1589
1590 Returns a list of `card dicts`_, sorted by the instances's trending algorithm,
1591 descending.
1592 """
1593 params = self.__generate_params(locals())
1594 return self.__api_request('GET', '/api/v1/trends/links', params)
1556 1595
1557 ### 1596 ###
1558 # Reading data: Lists 1597 # Reading data: Lists
diff --git a/tests/cassettes/test_trends.yaml b/tests/cassettes/test_trending_links.yaml
index 41cf4af..3d54b25 100644
--- a/tests/cassettes/test_trends.yaml
+++ b/tests/cassettes/test_trending_links.yaml
@@ -13,7 +13,7 @@ interactions:
13 User-Agent: 13 User-Agent:
14 - tests/v311 14 - tests/v311
15 method: GET 15 method: GET
16 uri: http://localhost:3000/api/v1/trends 16 uri: http://localhost:3000/api/v1/trends/links
17 response: 17 response:
18 body: 18 body:
19 string: '[]' 19 string: '[]'
@@ -23,7 +23,7 @@ interactions:
23 Content-Security-Policy: 23 Content-Security-Policy:
24 - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src 24 - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src
25 ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000; 25 ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000;
26 style-src ''self'' http://localhost:3000 ''nonce-wweJU6CNiZOq8p6GOgvKMg==''; 26 style-src ''self'' http://localhost:3000 ''nonce-7UiLUEyJEVY9JgarYKBdCg=='';
27 media-src ''self'' https: data: http://localhost:3000; frame-src ''self'' 27 media-src ''self'' https: data: http://localhost:3000; frame-src ''self''
28 https:; manifest-src ''self'' http://localhost:3000; connect-src ''self'' 28 https:; manifest-src ''self'' http://localhost:3000; connect-src ''self''
29 data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000 29 data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000
@@ -49,9 +49,9 @@ interactions:
49 X-Permitted-Cross-Domain-Policies: 49 X-Permitted-Cross-Domain-Policies:
50 - none 50 - none
51 X-Request-Id: 51 X-Request-Id:
52 - 04d49bc9-1ad7-48de-a189-e93e527942e0 52 - 623f9c94-2540-4c4f-b0ae-1c3db0b9ca35
53 X-Runtime: 53 X-Runtime:
54 - '0.010606' 54 - '0.016081'
55 X-XSS-Protection: 55 X-XSS-Protection:
56 - 1; mode=block 56 - 1; mode=block
57 status: 57 status:
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 @@
1interactions:
2- request:
3 body: null
4 headers:
5 Accept:
6 - '*/*'
7 Accept-Encoding:
8 - gzip, deflate
9 Authorization:
10 - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN
11 Connection:
12 - keep-alive
13 User-Agent:
14 - tests/v311
15 method: GET
16 uri: http://localhost:3000/api/v1/trends/statuses
17 response:
18 body:
19 string: '[]'
20 headers:
21 Cache-Control:
22 - no-store
23 Content-Security-Policy:
24 - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src
25 ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000;
26 style-src ''self'' http://localhost:3000 ''nonce-Qp9U6bbMsEloa3BOJP+ApA=='';
27 media-src ''self'' https: data: http://localhost:3000; frame-src ''self''
28 https:; manifest-src ''self'' http://localhost:3000; connect-src ''self''
29 data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000
30 ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline''
31 ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000;
32 worker-src ''self'' blob: http://localhost:3000'
33 Content-Type:
34 - application/json; charset=utf-8
35 ETag:
36 - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5"
37 Referrer-Policy:
38 - strict-origin-when-cross-origin
39 Transfer-Encoding:
40 - chunked
41 Vary:
42 - Accept, Origin
43 X-Content-Type-Options:
44 - nosniff
45 X-Download-Options:
46 - noopen
47 X-Frame-Options:
48 - SAMEORIGIN
49 X-Permitted-Cross-Domain-Policies:
50 - none
51 X-Request-Id:
52 - 9b65a082-dbab-436e-9544-9d3ceae169b0
53 X-Runtime:
54 - '0.022649'
55 X-XSS-Protection:
56 - 1; mode=block
57 status:
58 code: 200
59 message: OK
60version: 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 @@
1interactions:
2- request:
3 body: null
4 headers:
5 Accept:
6 - '*/*'
7 Accept-Encoding:
8 - gzip, deflate
9 Authorization:
10 - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN
11 Connection:
12 - keep-alive
13 User-Agent:
14 - tests/v311
15 method: GET
16 uri: http://localhost:3000/api/v1/trends/tags
17 response:
18 body:
19 string: '[]'
20 headers:
21 Cache-Control:
22 - no-store
23 Content-Security-Policy:
24 - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src
25 ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000;
26 style-src ''self'' http://localhost:3000 ''nonce-x84HIusoB0oMG/yq6Q4NDg=='';
27 media-src ''self'' https: data: http://localhost:3000; frame-src ''self''
28 https:; manifest-src ''self'' http://localhost:3000; connect-src ''self''
29 data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000
30 ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline''
31 ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000;
32 worker-src ''self'' blob: http://localhost:3000'
33 Content-Type:
34 - application/json; charset=utf-8
35 ETag:
36 - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5"
37 Referrer-Policy:
38 - strict-origin-when-cross-origin
39 Transfer-Encoding:
40 - chunked
41 Vary:
42 - Accept, Origin
43 X-Content-Type-Options:
44 - nosniff
45 X-Download-Options:
46 - noopen
47 X-Frame-Options:
48 - SAMEORIGIN
49 X-Permitted-Cross-Domain-Policies:
50 - none
51 X-Request-Id:
52 - e26034bb-bfdb-4f3a-bbef-9d2e51f88c14
53 X-Runtime:
54 - '0.029510'
55 X-XSS-Protection:
56 - 1; mode=block
57 status:
58 code: 200
59 message: OK
60- request:
61 body: null
62 headers:
63 Accept:
64 - '*/*'
65 Accept-Encoding:
66 - gzip, deflate
67 Authorization:
68 - Bearer __MASTODON_PY_TEST_ACCESS_TOKEN
69 Connection:
70 - keep-alive
71 User-Agent:
72 - tests/v311
73 method: GET
74 uri: http://localhost:3000/api/v1/trends/tags
75 response:
76 body:
77 string: '[]'
78 headers:
79 Cache-Control:
80 - no-store
81 Content-Security-Policy:
82 - 'base-uri ''none''; default-src ''none''; frame-ancestors ''none''; font-src
83 ''self'' http://localhost:3000; img-src ''self'' https: data: blob: http://localhost:3000;
84 style-src ''self'' http://localhost:3000 ''nonce-mRWh2zKUzi/z+WYqVSZD1g=='';
85 media-src ''self'' https: data: http://localhost:3000; frame-src ''self''
86 https:; manifest-src ''self'' http://localhost:3000; connect-src ''self''
87 data: blob: http://localhost:3000 http://localhost:3000 ws://localhost:4000
88 ws://localhost:3035 http://localhost:3035; script-src ''self'' ''unsafe-inline''
89 ''unsafe-eval'' http://localhost:3000; child-src ''self'' blob: http://localhost:3000;
90 worker-src ''self'' blob: http://localhost:3000'
91 Content-Type:
92 - application/json; charset=utf-8
93 ETag:
94 - W/"4f53cda18c2baa0c0354bb5f9a3ecbe5"
95 Referrer-Policy:
96 - strict-origin-when-cross-origin
97 Transfer-Encoding:
98 - chunked
99 Vary:
100 - Accept, Origin
101 X-Content-Type-Options:
102 - nosniff
103 X-Download-Options:
104 - noopen
105 X-Frame-Options:
106 - SAMEORIGIN
107 X-Permitted-Cross-Domain-Policies:
108 - none
109 X-Request-Id:
110 - c1cc20bc-a0b0-4ffa-bd72-686a094e1da0
111 X-Runtime:
112 - '0.015007'
113 X-XSS-Protection:
114 - 1; mode=block
115 status:
116 code: 200
117 message: OK
118version: 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
@@ -63,10 +63,6 @@ def test_nodeinfo(api):
63 assert nodeinfo.version == '2.0' 63 assert nodeinfo.version == '2.0'
64 64
65@pytest.mark.vcr() 65@pytest.mark.vcr()
66def test_trends(api):
67 assert isinstance(api.trends(), list)
68
69@pytest.mark.vcr()
70def test_directory(api): 66def test_directory(api):
71 directory = api.directory() 67 directory = api.directory()
72 assert directory 68 assert 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 @@
1import pytest
2import time
3import vcr
4
5
6@pytest.mark.vcr()
7def test_trending_tags(api):
8 tags = api.trending_tags()
9 assert isinstance(tags, list)
10 tags = api.trends()
11 assert isinstance(tags, list)
12
13@pytest.mark.vcr()
14def test_trending_statuses(api):
15 statuses = api.trending_statuses()
16 assert isinstance(statuses, list)
17
18@pytest.mark.vcr()
19def test_trending_links(api):
20 links = api.trending_links()
21 assert isinstance(links, list)
Powered by cgit v1.2.3 (git 2.41.0)