[PYTHON] Twitter slander repelling machine

SNS slander that is often talked about these days In some cases, even suicides are reported. So this time, I took off because I wanted to save such a person with the power of technology.

Of course, we would like to ask Twitter API for cooperation. Let's go! !! !!

By the way, what is API?

"API" is an acronym for "Application Programming Interface".

An interface, in computer terms, means something that connects "something" to "something." For example, USB is also one of the interfaces because it connects a "personal computer" and a "peripheral device".

In other words, API means that "something" and "something" connect "application, software" and "program".

An API makes it possible to share functions with software developed by a third party by exposing some of the software and applications to the outside. USB is an interface that connects an external device and a personal computer, but API connects software to each other. In other words, you will be able to share authentication functions between different software and services, share chat functions, import numerical data from one side, and analyze that data with another program.

To put it simply, publishing an API means creating a contact point in the software for communicating with the outside, and making it possible to communicate and cooperate with external applications. It was difficult for Japan and other countries to cooperate during the isolation period, but by making good use of Dejima in Nagasaki, which is the only place where trade with other countries was allowed, trade and cultural exchange with Japan, There is a history story that it became possible to exchange information, but if you think of Japan as one software and other countries as another software, Dejima was in charge of those two. It is an API function to connect.

Get your Twitter API key

No matter what you do, you must first get an API key. It's like a permit to use the window. This is the best mountain. If you survive this, let's do our best only by copying.

API key acquisition method

The acquisition method changes frequently, but this is the 2020 version. Detailed explanation from the example sentence of the 2020 version Twitter API usage application to the acquisition of the API key This site was the easiest to understand.

Let's make a slanderous repulsion program

This time I will use Python. First, for API authentication, a library called Requests-OAuthlib is used. Well, it's like a security guard before entering the window.

pip install requests requests-oauthlib

Finally the code. Please copy and paste.

# coding=utf-8

import json
from requests_oauthlib import OAuth1Session

#Authentication process
CK = 'Your API key'
CS = 'Your API secret key'
AT = 'Your access token'
ATS = 'Your access token secret'
twitter = OAuth1Session(CK, CS, AT, ATS)

#Tweet search endpoint
url = 'https://api.twitter.com/1.1/search/tweets.json'
#User block endpoint
url2 = 'https://api.twitter.com/1.1/blocks/create.json'
#Wording that is the target of slander
bad_words = ['Stupid', 'Stupid', 'Why']

for bad_word in bad_words:
    #Parameters to pass to the endpoint
    keyword = '@@downtownakasiya '  #Reply account
    keyword += bad_word
    keyword += ' exclude:retweets'  #RT excluded

    params = {
        'count': 50,  #Number of tweets to get
        'q': keyword,  #Search keyword
    }
    req = twitter.get(url, params=params)

    if req.status_code == 200:  #What to do when it works
        res = json.loads(req.text)
        for line in res['statuses']:
            params2 = {'user_id': line['user']['id']}  #Users to block
            req2 = twitter.post(url2, params=params2)

            if req2.status_code != 200:
                print("Failed2: %d" % req2.status_code)
    else:  #What to do if something goes wrong
        print("Failed: %d" % req.status_code)

#Note: Twitter API cannot make search hits for rips older than a week

Save this code as "twitter.py" and use the command

python twitter.py

If you hit it, the slanderous comment will be hidden in a blink of an eye.

Summary

Since the person himself knows what kind of bad words will hurt the most, I made it possible for him to choose the wording that is the target of slander. I sincerely hope that this program will be of some help to the world.

By the way, this is my Twitter dirt. Feel free to message me! https://twitter.com/downtownakasiya

Recommended Posts

Twitter slander repelling machine
Twitter slander repulsion machine (strongest version)