A class that summarizes frequently used methods in twitter api (python)

If you can get and POST with requests, you can use most of the methods provided on the API side.

import requests
from requests_oauthlib import OAuth1
class Twitter():
    def __init__(self, api_key):
        self.OAuth = OAuth1(api_key["CONSUMER_KEY"], api_key["CONSUMER_SECRET"], api_key["ACCESS_TOKEN"], api_key["ACCESS_SECRET"])
        
    def search_query(self, query, until=None, count=100): #Within 1 week
        url = 'https://api.twitter.com/1.1/search/tweets.json'
        query += " exclude:nativeretweets filter:media"
        params = {'q': query, 'result_type': 'mixed', 'until': until, 'count': count, 'include_entities': True}
        request = requests.get(url, params=params, auth=self.OAuth)
        requestJson = json.loads(request.text)
        return requestJson
    
    def update_status(self, status, reply_id=None):
        url = 'https://api.twitter.com/1.1/statuses/update.json'
        params = {'status': status, 'in_reply_to_status_id': reply_id}
        request = requests.post(url, params=params, auth=self.OAuth)
        
    def get_my_tweet(self, count=1, screen_name=screen_name):
        url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
        params = {'count': count, 'screen_name': screen_name}
        request = requests.get(url, params=params, auth=self.OAuth)
        requestJson = json.loads(request.text)
        return requestJson  
    
    def get_tweets_of_user(self, count=100, screen_name=None):
        url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
        params = {'count': count, 'screen_name': screen_name}
        request = requests.get(url, params=params, auth=self.OAuth)
        requestJson = json.loads(request.text)
        return requestJson  
    
    def update_profile(self, profile_url):
        url = 'https://api.twitter.com/1.1/account/update_profile.json'
        params = {'url': profile_url}
        request = requests.post(url, params=params, auth=self.OAuth)
    
    def retweet(self, id_):
        url = 'https://api.twitter.com/1.1/statuses/retweet/{}.json'.format(id_)
        request = requests.post(url, auth=self.OAuth)
        
    def delete_tweet(self, id_):
        url = "https://api.twitter.com/1.1/statuses/destroy/{}.json".format(id_)
        request = requests.post(url, auth=self.OAuth)
        
    def follow(self, user_id):
        url = 'https://api.twitter.com/1.1/friendships/create.json'
        params = {'user_id': user_id}
        request = requests.post(url, params=params, auth=self.OAuth)        

reference

Recommended Posts

A class that summarizes frequently used methods in twitter api (python)
8 Frequently Used Commands in Python Django
Access the Twitter API in Python
[Python] A memo of frequently used phrases (by myself) in Python scripts
Easy! Implement a Twitter bot that runs on Heroku in Python
[Python / Django] Create a web API that responds in JSON format
Generate a first class collection in Python
A class that hits the DMM API
[python] Frequently used techniques in machine learning
Tweet using the Twitter API in Python
Generate a class from a string in Python
A memo that I wrote a quicksort in Python
[Python / Tkinter] A class that creates a scrollable Frame
I wrote a class in Python3 and Java
A program that removes duplicate statements in Python
Testing methods that return random values in Python
Python: Create a class that supports unpacked assignment
I tried to create a class that can easily serialize Json in Python
I made a familiar function that can be used in statistics with Python
case class in python
Summary of statistical data analysis methods using Python that can be used in business
Note the frequently used options in Python + Selenium + Chrome
[LINE Messaging API] Create a rich menu in Python
Playing with a user-local artificial intelligence API in Python
[python] class basic methods
Evernote API in Python
A collection of commands frequently used in server management
What's in that variable (when running a Python script)
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
In Python, create a decorator that dynamically accepts arguments Create a decorator
C API in Python 3
A collection of Excel operations often used in Python
Kaggle's initial equipment-one cell that summarizes frequently used codes-
Class notation in Python
MALSS, a tool that supports machine learning in Python
I made a program to collect images in tweets that I liked on twitter with Python
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
A function that divides iterable into N pieces in Python
Create a list in Python with all followers on twitter
Loop through a generator that returns a date iterator in Python
How to use the __call__ method in a Python class
Let's create a script that registers with Ideone.com in Python.
Creating a Python script that supports the e-Stat API (ver.2)
A note on touching Microsoft's face recognition API in Python
I tried "a program that removes duplicate statements in Python"
Create code that outputs "A and pretending B" in python
I created a class in Python and tried duck typing
Scripts that can be used when using bottle in Python
A set of script files that do wordcloud in Python3
Hit Mastodon's API in Python
Take a screenshot in Python
Create a function in Python
Create a dictionary in Python
String object methods in Python
Use Twitter API with Python
[Python] Frequently used library code
Make a bookmarklet in Python
Python frequently used code snippets
Frequently used commands in virtualenv
Blender Python API in Houdini (Python 3)