Add Client.follow and Client.unfollow
authorHarmon <Harmon758@gmail.com>
Wed, 24 Feb 2021 03:08:49 +0000 (21:08 -0600)
committerHarmon <Harmon758@gmail.com>
Wed, 24 Feb 2021 03:08:49 +0000 (21:08 -0600)
tweepy/client.py

index 323fdacee7c263e9b7791b2e56d833137f0a066e..9e41f7212a61a97949a6e2a27db2006c6115cdd2 100644 (file)
@@ -125,6 +125,19 @@ class Client:
 
         return Response(data, includes, errors, meta)
 
+    def follow(self, user_id):
+        """
+        Follow user
+        https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/post-users-source_user_id-following
+        """
+        source_user_id = self.access_token.partition('-')[0]
+        route = f"/2/users/{source_user_id}/following"
+
+        return self._make_request(
+            "POST", route, json={"target_user_id": str(user_id)},
+            user_auth=True
+        )
+
     def get_tweets(self, ids, **params):
         """
         Tweet lookup
@@ -271,6 +284,18 @@ class Client:
             data_type=Tweet
         )
 
+    def unfollow(self, user_id):
+        """
+        Unfollow user
+        https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/delete-users-source_id-following
+        """
+        source_user_id = self.access_token.partition('-')[0]
+        route = f"/2/users/{source_user_id}/following/{user_id}"
+
+        return self._make_request(
+            "DELETE", route, user_auth=True
+        )
+
     def unhide_reply(self, tweet_id):
         """
         Unhide replies