[PYTHON] Assign tweets to shortcut keys

I want to easily tweet Nyan

When I get stuck in a study or program, I want to tweet something. In such a case, it is stupid to start the client and tweet each time. Then why not assign it to a shortcut key? I thought. I'll do my best to summarize what I actually did.

--Environment - zorin os 12(debian) - python3

Get token

In order to tweet from python, you have to get a token with twitter API. https://dev.twitter.com/ Jump to the URL here Screenshot from 2017-05-05 01-30-05.png

Press My apps on the right side of the top of the screen. Please log in when prompted. Let's stop useless resistance.

Screenshot from 2017-05-05 01-32-37.png

Your app will come out like this. If you want to create a new one, please press Create New App at the top right of the screen. This time we will proceed if there is no application.

Press Create New App and you will see this screen. Screenshot from 2017-05-05 01-37-32.png

Please fill in the following in each input field.


Name Application name (appropriately given)

Description Application description

Website URL of your own website (It is a required item, so if you do not have it, please do your best to get it)

Callback URL This time I'm just tweeting, so it doesn't have to be.


After inputting to the end safely, press the check box and it is at the bottom of the screen Press __Create Your Twitter Application __. Screenshot from 2017-05-05 08-08-41.png

Then press __Keys and Access Token __ above the app name. Once pressed, scroll down and press __Create my access token __.

Screenshot from 2017-05-05 08-12-59.png

If you press it, a token will be generated without permission. I'm sure it will look like this.

Screenshot from 2017-05-05 08-14-29.png

You will need the "Consumer Key", "Consumer Secret", "Access Token", "Access Token Secret" that I filled in here when writing the program. Please do not teach it to others. Once the Access Token is issued, the API settings are complete. Keep the page open as you will use the token later.

Processing on the Python side

If you don't have tweepy, type pip install tweepy on the console If you can install it successfully, please create the document "--. Py" in a suitable place. Basically, a place that is easy to understand is good. Then, in that text,


#I'm writing with python3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy

CONSUMER_KEY = 'CONSUMER_Enter the KEY'
CONSUMER_SECRET = 'CONSUMER_Enter SECRET'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
ACCESS_TOKEN = 'ACCESS_Enter TOKEN'
ACCESS_SECRET = 'ACCESS_Enter SECRET'
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)

api = tweepy.API(auth)
api.update_status(status='Nyan')#Nyanの部分をお好きな文章に

Please write like this. Please check http://docs.tweepy.org/en/v3.5.0/ for detailed specifications. This is the end of the python side.

Allow execution with shortcut keys

To be able to execute with a shortcut key, it must be commanded. So, this time, let's make the command "Run the tweet program written in python in the shell." First of all, write a script in shell. Make text on your desktop to make the name descriptive.

#!/bin/sh
command="python /Desktop/python/tweet.py"#Absolute path where the python program is located
eval $command

You have to move it to / usr / local / bin, so start the file manager with sudo nautilus. Screenshot from 2017-05-17 13-02-36.png You should see something like this. Go from another location to your computer and then to / usr / local / bin. Screenshot from 2017-05-17 13-06-19.png

Next, copy the script you wrote in the shell to / usr / local / bin. At that time, delete the extension (.txt part) from ~~~ .txt so that the name is easy to understand and does not cover. The tweet is now a command.

Assign key

This is the last, if you understand, please skip it. Screenshot from 2017-05-16 13-10-09.png First, let's open the settings and select Keyboard. Screenshot from 2017-05-16 13-10-22.png I think the screen will look like this, so select a shortcut and select __ your own shortcut __. Then press the __ + __ button at the bottom left of the screen to add a new shortcut. Screenshot from 2017-05-16 13-12-09.png

When you actually press it, you can add it like this. The name of the shortcut should be self-explanatory, and the command is the name of the script you put in / usr / local / bin. Screenshot from 2017-05-17 13-15-10.png After confirming the addition, set it to your favorite key. You can now tweet from the shortcut key. Ah, but if you tweet the same word in a row, you can't tweet due to API restrictions. Is it about once a day? Well, I've explained it in a rough way, but that's it. Please send a good Twitter life to everyone.

Recommended Posts

Assign tweets to shortcut keys
[Blender] Assign shortcut keys to your own functions inside the script
Convenient shortcut keys for Linux commands! !! !!