From b20b236c4ca10a5376ea2f612a1ad324f21d7072 Mon Sep 17 00:00:00 2001 From: Harmon Date: Mon, 20 Sep 2021 16:49:36 -0500 Subject: [PATCH] Add Client.unmute and Client.mute --- cassettes/test_mute_and_unmute.yaml | 125 ++++++++++++++++++++++++++++ docs/client.rst | 7 ++ tests/test_client.py | 6 ++ tweepy/client.py | 50 +++++++++++ 4 files changed, 188 insertions(+) create mode 100644 cassettes/test_mute_and_unmute.yaml diff --git a/cassettes/test_mute_and_unmute.yaml b/cassettes/test_mute_and_unmute.yaml new file mode 100644 index 0000000..b06597e --- /dev/null +++ b/cassettes/test_mute_and_unmute.yaml @@ -0,0 +1,125 @@ +interactions: +- request: + body: '{"target_user_id": "17874544"}' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '30' + Content-Type: + - application/json + User-Agent: + - Python/3.9.6 Requests/2.25.1 Tweepy/4.0.0-alpha + method: POST + uri: https://api.twitter.com/2/users/1072250532645998596/muting + response: + body: + string: !!binary | + H4sIAAAAAAAAAKpWSkksSVSyqlbKLS3JzEtXsiopKk2trQUAAAD//wMAKOKMLRgAAAA= + headers: + api-version: + - '2.24' + cache-control: + - no-cache, no-store, max-age=0 + content-disposition: + - attachment; filename=json.json + content-encoding: + - gzip + content-length: + - '50' + content-type: + - application/json; charset=utf-8 + date: + - Mon, 20 Sep 2021 21:47:48 UTC + server: + - tsa_a + set-cookie: + - personalization_id="v1_tx5lZdBKqUcj82GIzmOxSg=="; Max-Age=63072000; Expires=Wed, + 20 Sep 2023 21:47:48 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None + - guest_id=v1%3A163217446858292923; Max-Age=63072000; Expires=Wed, 20 Sep 2023 + 21:47:48 GMT; Path=/; Domain=.twitter.com; Secure; SameSite=None + strict-transport-security: + - max-age=631138519 + x-access-level: + - read-write-directmessages + x-connection-hash: + - b749ed00177b9e8d4fa0590d2ddcb7210910a14c470247d2796add7567c67c6f + x-content-type-options: + - nosniff + x-frame-options: + - SAMEORIGIN + x-rate-limit-limit: + - '50' + x-rate-limit-remaining: + - '49' + x-rate-limit-reset: + - '1632175368' + x-xss-protection: + - '0' + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + Content-Length: + - '0' + Cookie: + - guest_id=v1%3A163217446858292923; personalization_id="v1_tx5lZdBKqUcj82GIzmOxSg==" + User-Agent: + - Python/3.9.6 Requests/2.25.1 Tweepy/4.0.0-alpha + method: DELETE + uri: https://api.twitter.com/2/users/1072250532645998596/muting/17874544 + response: + body: + string: !!binary | + H4sIAAAAAAAAAKpWSkksSVSyqlbKLS3JzEtXskpLzClOra0FAAAA//8DAL40mrYZAAAA + headers: + api-version: + - '2.24' + cache-control: + - no-cache, no-store, max-age=0 + content-disposition: + - attachment; filename=json.json + content-encoding: + - gzip + content-length: + - '51' + content-type: + - application/json; charset=utf-8 + date: + - Mon, 20 Sep 2021 21:47:48 UTC + server: + - tsa_a + strict-transport-security: + - max-age=631138519 + x-access-level: + - read-write-directmessages + x-connection-hash: + - b749ed00177b9e8d4fa0590d2ddcb7210910a14c470247d2796add7567c67c6f + x-content-type-options: + - nosniff + x-frame-options: + - SAMEORIGIN + x-rate-limit-limit: + - '50' + x-rate-limit-remaining: + - '49' + x-rate-limit-reset: + - '1632175368' + x-xss-protection: + - '0' + status: + code: 200 + message: OK +version: 1 diff --git a/docs/client.rst b/docs/client.rst index 5b7fa50..acfb2e0 100644 --- a/docs/client.rst +++ b/docs/client.rst @@ -80,6 +80,13 @@ Follows .. automethod:: Client.follow +Mutes +----- + +.. automethod:: Client.unmute + +.. automethod:: Client.mute + User lookup ----------- diff --git a/tests/test_client.py b/tests/test_client.py index 619195b..b052dc7 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -93,6 +93,12 @@ class TweepyTestCase(unittest.TestCase): user_id = 783214 # User ID for @Twitter self.client.get_users_following(user_id) + @tape.use_cassette("test_mute_and_unmute.yaml", serializer="yaml") + def test_mute_and_unmute(self): + user_id = 17874544 # User ID for @TwitterSupport + self.client.mute(user_id) + self.client.unmute(user_id) + @tape.use_cassette("test_get_user.yaml", serializer="yaml") def test_get_user(self): self.client.get_user(username="Twitter") diff --git a/tweepy/client.py b/tweepy/client.py index 31a8124..21086e2 100644 --- a/tweepy/client.py +++ b/tweepy/client.py @@ -1244,6 +1244,56 @@ class Client: user_auth=True ) + # Mutes + + def unmute(self, target_user_id): + """Allows an authenticated user ID to unmute the target user. + + The request succeeds with no action when the user sends a request to a + user they're not muting or have already unmuted. + + Parameters + ---------- + target_user_id : Union[int, str] + The user ID of the user that you would like to unmute. + + Returns + ------- + Union[dict, requests.Response, Response] + + References + ---------- + https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/delete-users-user_id-muting + """ + source_user_id = self.access_token.partition('-')[0] + route = f"/2/users/{source_user_id}/muting/{target_user_id}" + + return self._make_request("DELETE", route, user_auth=True) + + def mute(self, target_user_id): + """Allows an authenticated user ID to mute the target user. + + Parameters + ---------- + target_user_id : Union[int, str] + The user ID of the user that you would like to mute. + + Returns + ------- + Union[dict, requests.Response, Response] + + References + ---------- + https://developer.twitter.com/en/docs/twitter-api/users/mutes/api-reference/post-users-user_id-muting + """ + id = self.access_token.partition('-')[0] + route = f"/2/users/{id}/muting" + + return self._make_request( + "POST", route, json={"target_user_id": str(target_user_id)}, + user_auth=True + ) + # User lookup def get_user(self, *, id=None, username=None, user_auth=False, **params): -- 2.25.1