Word Notifier

Mridu Bhatnagar
5 min readJan 1, 2018

--

01 Jan 2018

Playing around with API

Couple of weeks back. There was a buzz on Twitter. Mr Shashi Tahroor honourable MP tweeting word 'Rodomontade'. And like me many others might have looked up for the meaning of the word ‘Rodomontade’. Mr Tahroor is always known for the good vocabulary he has. And as always Amul came up with a perfect tweet. “Tharooraurus Anyone ?” :p.

Yes. This helped me come up with the first idea for automation using Python. So, I tweaked around the Wordnik API. Read the API documentation and concluded that it did be nice to put this to good use. Made a GET request call to /wordOfTheDay endpoint. And wohoo! The response had everything I needed. The word meaning, usage, origin, source, part_of_speech etc.

GET call to the endpoint /wordOfTheDay.

Now, normally running the python script and getting the word of the day. Was fine. But, I was too lazy to run it everyday. On second thought running the python script or looking up to dictionary for the word, googling the word. I found all those to be same. Manually I’ll have to look up. And then it striked me. Let’s give automation a try.

Wrote a python script to fetch the JSON response from the Wordnik API. And fetch Word of the day. One new word daily.Now, the next question was fetching one word per day is alright. But, daily running the script manually?? Boring. And I was pretty sure I won’t be running it daily. Then it striked to send the fetched word of the day as an email notification.

Explored smtplib-SMTP-Protocol-client in Python. And figured it out how to send an email using Python script. Still, only on running the python script I was able to get the word of the day as email. Automation?? Had no clue.

Email Notification

Had heard of cron job. But, wasn’t sure how to use. Through community came across something known as crontab. And on ubuntu linux distribution there is a command crontab -e. Now, this is where I came a step ahead as far as automation was concerned. More resources and I learned how to use this command crontab -e.

Crontab helps you schedule jobs daily, monthly, weekly. This is how the syntax goes.

minute hour day-of-month month day-of-week command

Example — 30 17 * * 1 /path/to/command means run the command at 5:30 pm on every Monday. Had there been just the script and no api keys needed. My first automation task was done. But, there was more to it. To get the script running I had to set the environment variables first which included getting the API token, email address of the receiver, email account from which mail was sent. More discussions and someone on IRC suggested write a bash script. That sets everything. And from crontab run your bash script.

This was again something I had no exposure to. Some articles, howto’s, errors. And thankfully all sorted. Bash Script done. One thing was getting resolved and another issue came up. Now, when bash script was done. Next step was to add path to python file. Which was meant to run daily.

Now, command used to run a python file looks like /path/to/file/python <filename>.py. This time difference being I was not the one who would be running the file. It had to be present in the bash script. Hence, path had to be /path/to/python /path/to/python/file.

do-it-all.sh
Add path to activate virtual environment, path to file in the bash script

Last and the final leap now. Add path to bash script in crontab -e. And schedule the task. So, this is how it looked like.

First line sets the cronjob

00 10 * * * /path/to/do-it-all.sh

Next morning nothing was more satisfying than seeing new email with Word of the day in the inbox.

Just make sure whatever time you specify in crontab that time your system is up and connected to wifi. Untill I show up again with anacron tab.

NOTE Cron assumes that system is always up and running and is connected to the network. Unlike laptops and desktops. So, if at 10:00 am the system is switched off or not connected to network. The email for that day would be skipped. But, to overcome this shortcoming. There is a command as anacrontab. Now, how crontab and anacrontab are different?

So, ideally anacrontab is used for systems that are not always up and running. Let’s assume you had scheduled your script to run at 10:00 everyday but at 10:00 am your system was switched off, or not connected to network. You won’t be missing out on email that day. Anacrontab helps you schedule tasks and allows you to give delay. So, whenever you switch on your system. After the scheduled delay you’ll receive the email in your inbox. :)

As of now the script uses crontab. I am still exploring the usage of anacrontab. Once that is done. I’ll update.

Here is the script Word-Notifier.

It’s all about joy of little things in life. This was probably one of those. :) Satisfying indeed.

Hoping that it helps someone. Though the possibility of the next word Mr Tharoor using and majority knowing is rare. But, it’s always good to learn.

Originally published at mridubhatnagar.github.io on January 1, 2018.

--

--

Mridu Bhatnagar
Mridu Bhatnagar

Written by Mridu Bhatnagar

Honest, straight from the heart things. I care about, bother about, think about, I experience. I share them here. This has a purpose behind.

No responses yet