XKCD!

Automate the boring stuff!

Isn’t it a manual task to visit a website every day to view your favorite comic strip? Why not let a script do it for you? And maybe send it as an email attachment at a specified time every day of the week! Well, with the help of Python book, ‘Automate the Boring Stuff’ let’s create our small script to download and send that as an email attachment.

Details

I have created download_xkcd.py which scrapes https://xkcd.com for a comic and downloads it in a local folder. The email_xkcd.py contains code to send the downloaded comic as an email attachment. But you don’t want to receive a duplicate comic if there is no new comic’s, right!? So, for this, I keep a record of the previous comic in a text file called comic_records.txt which I will read before sending out an email.

Cron Job

I followed Ole Michelsen’s article to create a Cron Job, which is straight forward in MacOS.

To run your python script every day of the week at 6 PM, you can do something like this:

  0 18 * * * cd /folder/where/your/script/resides/ && python 

Note: If you are using Gmail to send an email then you will need to allow less secure apps to access Gmail! The first time you try to access Gmail using Python, your access will not be granted and you will receive an email from Google saying, Review blocked sign-in attempt, click allowing access to less secure apps link and select Turn on and there you go, you can access your gmail using Python!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s