Twitter from command line

Since the time I have started writing code, the toughest job for me is to be in peace with the black and green screen, the terminal. As it is being “The Thing” which keeps my lovely husband (ah ha, really?) from me. So as an initiative of my “peace making process” I have started doing my day mostly on this boring screen. A part of that is me trying to do twitter from the command line. Thus, let us make the (boring) terminal interesting.

To reach the aim I needed a Python module to access the Twitter api. I used a module called python-twitter. Click is a Python package to create command line applications. I used it to have a better command line interface. I used Microsoft Visual Studio Code as my primary editor. Like all previous projects, I am leaned on Jupyter Notebook to try out code snippets. I used Pipenv for the first time here.

import sys
import twitter
import json
import click

After importing the modules (as mentioned above) required, the job was to create the boolean command line flags through click, so,

@click.option("--tweet", "-t", is_flag=True, help="Does tweet.")
@click.option("--timeline", "-n", is_flag=True, help="Shows user's timeline.")
   "--directmessage", "-m", is_flag=True, help="Shows user's direct messages."

I learnt click from this blogpost, it was really helpful.

I wrote a config.json file where I have the required authentication details, such as consumer key, consumer secret, access token key, access token secret and user id. I got them from my Twitter developer account. In the account you have to set your access level as “Read, Write and Direct Messages”. I am creating an object of the twitter.Api class. I am passing different arguments, tweet, timeline and direct message to subsequently do tweet, see my timeline and get to see my direct messages, in the command line.

I used Black to format my code. Formatting of the code makes the code more readable and easy to review.

The next and final job was to upload it on [PyPI] using twine. For this I followed a blogpost I did earlier I The source code of the project is available on my github.

If you notice, I have used many things for the first time in this small learning effort. Projects of this size are really helpful to learn new things.

Happy tweeting (from the command line).