[PYTHON] Automatically RT promotional tweets using a running PC anyway

Overview

It's annoying to go to press the retweet button every time, so Seya! It's automated! The story.

It's unexpectedly annoying to repeat retweets many times

I like making music and that kind of thing, The other day, I announced a new song. It will be an advertisement, but please listen to it.

** Isn't it annoying to advertise this many times? ** I suddenly thought. It's annoying, so let's automate it.

Prepare to use Twitter API

Prepare Twitter API usage permission

Please refer to this page to get it.

-Twitter API registration \ (account application method ) to approval procedure summary * Information as of August 2019 -Qiita

For the time being, let's put the acquired one in key.py in an easy-to-understand manner.

key.py


API_KEY = 'hoge'
API_SECRET = 'hoge'
ACCESS_TOKEN = 'hoge'
ACCESS_TOKEN_SECRET = 'hoge'

Next, let's create Twitter_API.py so that we can use the API.

Twitter_api.py



import key

API_KEY = key.API_KEY
API_SECRET = key.API_SECRET
ACCESS_TOKEN = key.ACCESS_TOKEN
ACCESS_TOKEN_SECRET = key.ACCESS_TOKEN_SECRET


def api_proc():
    #Make Twiter's API available
    api = OAuth1Session(
        API_KEY,
        API_SECRET,
        ACCESS_TOKEN,
        ACCESS_TOKEN_SECRET
    )

    return api

Now when you run Twitter_api.api_proc (), you will be given Twitter API permissions as a return value.

Let's actually run the API

Summarize what you want to do

Here's an easy-to-understand summary of what you want to do. --Unlock the specified Tweet ID with your account. --RT the specified Tweet ID with your account.

Why is it necessary to release it? That said, you can't retweet the same tweet twice, so This is because if you want to retweet the same tweet, you need to cancel it once. By the way, no error occurs. (Strictly speaking, exception does not occur)

Get the ID of the tweet

You will be taken to the tweet page on Twitter. If you use a PC, you can use any suitable tweet, so click it to fly. If not, you can usually click on a date to go to that tweet.

I think the URL of that page is as follows.

https://twitter.com/MushroomRecord/status/1346128262871744512

To the right of status of this, that is, 1346128262871744512 is the corresponding ID for this URL. I will use this, so make a note of it.

Run API

This time, it's easy to finish just by running the API with POST, so Scripting is also easy.

main.py


import Twitter_API


def main(tweet_id: int):
    TWITTER = Twitter_API.api_proc()
    URL_TWITTER = 'https://api.twitter.com/1.1/statuses'
    URL_RT = f'{URL_TWITTER}/retweet/{tweet_id}.json'
    URL_UNRT = f'{URL_TWITTER}/unretweet/{tweet_id}.json'

    TWITTER.post(URL_UNRT)
    TWITTER.post(URL_RT)


if __name__ == '__main__':
    tweet_id = 1346128262871744512
    main(tweet_id)

If you do this, I think my promotional tweets will be RT. You should be able to RT any tweet unless you have a private account by changing tweet_id.

Run it regularly using a running PC anyway

I keep my PC and Mac on all the time, so Use this crazy point to make it run regularly.

I thought that there was something like cron, but when I looked it up, It seems that Launchd is better for Mac, I'm not sure.

That's why we use Launchd to perform regular execution.

autoself_retweet.plist


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>autoself_retweet</string>

        <key>ProgramArguments</key>
        <array>
            <string>python path</string>
            <string>Script path</string>
        </array>

        <key>StartInterval</key>
        <integer>3600</integer>

    </dict>
</plist>

Build while thinking that key and some attribute are a set.

--Label sets any name. ** Label and filename must be the same. ** **

--Enter the command and parameters you want to execute in ProgramArguments.

I didn't understand this well, and I really wanted to make it a versatile description such as "Move with cd and load the script there". It didn't work, so it's written as a command in one line with an emphasis on practicality. If you are familiar with it, I would appreciate it if you could teach me.

--Determine how many seconds to execute with StartInterval.

Set so that it can be executed regularly

Now that you have an environment to execute Python scripts every n seconds, you can actually put them on your Mac.

--Install autoself_retweet.plist in ~/Library/LaunchAgents. --Enter the following command in the terminal to start periodic execution.

Terminal


$ launchctl load ~/Library/LaunchAgents/autoself_retweet.plist

--If the script is not executed even if you follow the above procedure, or if you think that the test has failed, unload it with the following command.

Terminal


$ launchctl unload ~/Library/LaunchAgents/autoself_retweet.plist

Let's have a good regular execution life.

reference

Run scripts regularly with launchd -Qiita

Recommended Posts

Automatically RT promotional tweets using a running PC anyway