From 1f36deb11950a627b52a587fc3ec30c37f9bb456 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sat, 12 Oct 2019 22:05:35 +0200 Subject: [PATCH] Add, test and document profile directory API (fixes #188) --- docs/index.rst | 20 +++++++++++++------- mastodon/Mastodon.py | 13 +++++++++++++ tests/cassettes/test_directory.yaml | 29 +++++++++++++++++++++++++++++ tests/setup.sql | 6 ++++-- tests/test_instance.py | 9 ++++++++- 5 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 tests/cassettes/test_directory.yaml diff --git a/docs/index.rst b/docs/index.rst index b9fcde1..ef94b2a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -263,12 +263,13 @@ User dicts 'header_static': # URL for their header image, never animated 'source': # Additional information - only present for user dict returned # from account_verify_credentials() - 'moved_to_account': # If set, an account dict of the account this user has + 'moved_to_account': # If set, a user dict of the account this user has # set up as their moved-to address. 'bot': # Boolean indicating whether this account is automated. 'fields': # List of up to four dicts with free-form 'name' and 'value' profile info. # For fields with "this is me" type verification, verified_at is set to the # last verification date (It is None otherwise) + 'emojis': # List of custom emoji used in name, bio or fields } mastodon.account_verify_credentials()["source"] @@ -626,7 +627,7 @@ Search result dicts mastodon.search("") # Returns the following dictionary { - 'accounts': # List of account dicts resulting from the query + 'accounts': # List of user dicts resulting from the query 'hashtags': # List of hashtag dicts resulting from the query 'statuses': # List of toot dicts resulting from the query } @@ -651,7 +652,7 @@ Instance dicts 'stats: # A dictionary containing three stats, user_count (number of local users), # status_count (number of local statuses) and domain_count (number of known # instance domains other than this one). - 'contact_account': # Account dict of the primary contact for the instance + 'contact_account': # User dict of the primary contact for the instance 'languages': # Array of ISO 639-1 (two-letter) language codes the instance # has chosen to advertise. 'registrations': # Boolean indication whether registrations on this instance are open @@ -691,11 +692,11 @@ Report dicts 'comment': # Text comment submitted with the report 'created_at': # Time at which this report was created, as a datetime object 'updated_at': # Last time this report has been updated, as a datetime object - 'account': # Account dict of the user that filed this report + 'account': # User dict of the user that filed this report 'target_account': # Account that has been reported with this report 'assigned_account': # If the report as been assigned to an account, - # account dict of that account (None if not) - 'action_taken_by_account': # Account dict of the account that processed this report + # User dict of that account (None if not) + 'action_taken_by_account': # User dict of the account that processed this report 'statuses': # List of statuses attached to the report, as toot dicts } @@ -796,7 +797,7 @@ Admin account dicts 'locale': # For local users, the locale the user has set, 'invite_request': # If the user requested an invite, the invite request comment of that user. (TODO permanent?) 'invited_by_account_id': # Present if the user was invited by another user and set to the inviting users id. - 'account': # The users account, as a standard account dict + 'account': # The users account, as a standard user dict } App registration and user authentication @@ -942,6 +943,11 @@ Reading data: Follow suggestions .. automethod:: Mastodon.suggestions +Reading data: Profile directory +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. authomethod:: Mastodon.directory + Reading data: Lists ------------------- These functions allow you to view information about lists. diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 9c6f1ce..d07bae7 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -1238,6 +1238,19 @@ class Mastodon: """ return self.__api_request('GET', '/api/v1/suggestions') + ### + # Reading data: Follow suggestions + ### + @api_version("3.0.0", "3.0.0", __DICT_VERSION_ACCOUNT) + def directory(self): + """ + Fetch the contents of the profile directory, if enabled on the server. + + Returns a list of `user dicts`_. + + """ + return self.__api_request('GET', '/api/v1/directory') + ### # Reading data: Endorsements ### diff --git a/tests/cassettes/test_directory.yaml b/tests/cassettes/test_directory.yaml new file mode 100644 index 0000000..6c478e3 --- /dev/null +++ b/tests/cassettes/test_directory.yaml @@ -0,0 +1,29 @@ +interactions: +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.18.4] + method: GET + uri: http://localhost:3000/api/v1/directory + response: + body: {string: '[{"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":[]}]'} + headers: + Cache-Control: ['no-cache, no-store'] + Content-Type: [application/json; charset=utf-8] + Referrer-Policy: [strict-origin-when-cross-origin] + Transfer-Encoding: [chunked] + Vary: ['Accept-Encoding, Origin'] + X-Content-Type-Options: [nosniff] + X-Download-Options: [noopen] + X-Frame-Options: [SAMEORIGIN] + X-Permitted-Cross-Domain-Policies: [none] + X-Request-Id: [fedf048d-8ed4-4185-bcb4-11384f0db697] + X-Runtime: ['0.141196'] + X-XSS-Protection: [1; mode=block] + content-length: ['597'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/setup.sql b/tests/setup.sql index 74b1d80..0230655 100644 --- a/tests/setup.sql +++ b/tests/setup.sql @@ -46,13 +46,15 @@ INSERT INTO accounts ( username, locked, created_at, - updated_at + updated_at, + discoverable ) VALUES ( 1234567890123457, 'mastodonpy_test_2', 't', now(), - now() + now(), + t ); INSERT INTO users ( diff --git a/tests/test_instance.py b/tests/test_instance.py index e7dee1f..2762ecb 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -43,8 +43,15 @@ 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() + assert directory + assert isinstance(directory, list) + assert len(directory) > 0 + +