Tag Archives: python

Feeds in your inbox

Google Reader will be dead by the 1st of July. Everyone has moved or at least is considering moving to new feed aggregators. There is a heap of options out there (out of which I recommend that you try out Newsblur). But none of them has been able to boast of a good public API as our good old Google Reader did.

I used to have this IFTTT recipe that would send the articles of the most important feeds I follow to my inbox. Now that Reader is dying and no other options out there seem to offer IFTTT channels, the best solution seemed to be rss2email.

rss2email (how appropriately named!) is a tool used to send new feed items to your email inbox. It has been first written in Python by Aaron Swartz (who happened to commit suicide a few months back). The source can be found in github.

Prerequisites

It would be much convenient if you have your own server (or a shared host). Even a desktop that runs on Linux or Mac OS X would do, but make sure it stays powered on most of the time and has a constant internet connection. This is because rss2email should be invoked periodically to check feeds using a cron job. To send email, you need to have a mail server installed and configured. If you’re using a web host, this might have already been done for you. Also make sure you have Python installed.

Setting up rss2email

1. Download

Download rss2email with wget and extract.

wget http://www.allthingsrss.com/rss2email/rss2email-2.70.tar.gz
tar xf rss2email-2.70.tar.gz

2. Configure

This is the most important step. The newly extracted directory contains a file called config.py which contains all the necessary configs. Most are self-descriptive. Make sure you configure the mail server settings correct. Here’s my setup:

SMTP_SEND = 1
SMTP_SERVER = “mail.thameera.com:[port]
AUTHREQUIRED = 1
SMTP_USER = ‘[emailid]@thameera.com’
SMTP_PASS = ‘[password]

You can set the email address that would be in the ‘From‘ field of emails in the DEFAULT_FROM config.

3. Set your ‘To:’ address

See the r2e script in your extracted directory? That’s what you need to invoke for the tasks we do from now on. First, set the address of where the mail should be delivered to using the following command:

./r2e new

4. Add feeds

Time to add those feeds. You can add them one by one using ./r2e add. For example, to add the feed of this blog:

./r2e add http://blog.thameera.com/feed

Now if you run the following command it would grab all the feeds and send them to your email:

./r2e run

However, it would send all the items available in the feeds in the first run. This could even be hundreds. Since most of you wouldn’t want this to happen, make sure you run it with --no-send first.

./r2e run –no-send

The next time you run r2e, it would mail only the new feed items.

5. Set the cron job

You’re almost done. The last step would be to set up a cron job that would invoke r2e periodically. If you don’t know how to set up cron jobs, be a lamb and look for a tutorial online. The cron job should cd to the r2e directory and run the command ./r2e run. You can set the time interval as you wish. A small interval means you get the articles mailed quickly, but would cause more server load and cost bandwidth. I’ve set mine to 15 minutes.

That’s it. No more Google Reader. You can sit back and relax while rss2email makes sure you never miss an important feed item. If you’re feeling adventurous, you can also go ahead and edit the python scripts to change the email format and tweak to make it more awesome.

Action.io, django and poya days

Last week I got an early access invite to the private beta of Action.io. Coincidentally, I wanted to learn some Django. Hence Next Poya When? was born and it only took a few hours to start coding from scratch to deploying successfully in the Google App Engine.

Action.io is a place to start your projects without worrying about all the mundane setup tasks. Once you sign up, you can create boxes. There are cardboard boxes, plastic boxes, and.., oh wait, wrong kind of boxes. The boxes in action.io are comparable to VPSs. You can choose from Ruby on Rails, Django, Node.js and Go boxes to start with. If you get a Django box it’s already pre-installed with python, django and all other utilities required for a typical Django project. All the boxes come with a Web IDE, Emacs and everyone’s favorite Vim as editors. There is PostgreSQL and MongoDB integration as well.

Action.io interface

Action.io interface (click to enlarge)

To set up, all I needed to do was copying my .bashrc and .vimrc files, install some Vim plugins with Pathogen and add the SSH key of my box to Github. All this takes less than 10 minutes. (But don’t tell that to your Project Manager, give him 2 hours effort and he wouldn’t know!)

Now create a project and start coding right away!

django-admin.py startproject poya

Once you build stuff to some extent and want to test how it’s doing, the site can be previewed easily. For example, start the server with

python manage.py runserver 0.0.0.0:3000

and click on Preview -> Port 3000. You can choose from a range of ports from 1024 to 9999.

Previewing the site

Previewing the site

Once done, deploying was a piece of cake. Action.io has Heroku and Google App Engine integration. Deploying the site in GAE took only a single command:

appcfg.py --oauth2 --noauth_local_webserver update nextpoyawhen

Then I pointed my domain to GAE and, voila, we have nextpoyawhen.com moving like jagger!

It’s really amazing how fast you can get everything done with action.io. If I did all this in my laptop it’d have taken ages. It goes without saying how mundane and time-consuming it is to install all libraries, dependencies and get the environment set up in your machine. More often than not, we get tired of the project even before we really start it. We spend 80% of time setting the things up, and much less time coding.

Also, since action.io lives in the web, it’s accessible from anywhere. If someone reports you a bug when you’re at office or travelling, you can just log into your box from the browser, do a hotfix and deploy within minutes. No effort spent for syncing files. You get your favorite environment setup anywhere in the world.

Action.io doesn’t work offline though, at least yet. It doesn’t require a fast connection, but if yours gets disconnected often, be wary. I hope they would implement the offline capability soon.

As for Django, I’m impressed by the framework. It’s quite easy to catch up with, and it’s Python. You can host Django sites in most web hosting providers (unlike node.js), including Google App Engine and Heroku both of which have free plans to serve small sites. I’m studying with The Django Book, recommended by Raditha. Of course, nextpoyawhen could’ve been done with some client-side javascript, but that’d have defeated the purpose. I wanted to do something with django and try out action.io, so it’s like catching two stones with one angry bird, or whatever that is. The source code is hosted in github.

So, the next time you are tired of working all week and want to know when the next Poya holiday is on, you know where to look. :)

Adding captions to images with Python

So I wanted to add captions to a set of images and wrote some lines of Python. Python has some cool image libraries so the job was real easy. Have never used Python before except to write some kindergarten-level programs, so this may not be the best way to code the thing. But it does the job.

	import Image, ImageDraw, ImageFont, os

	pathz="/home/thameera/ws/img_caption/pics"
	if os.path.exists(pathz) and os.path.isdir(pathz):
		for name in os.listdir(pathz):
			print name
			fullname=pathz+'/'+name
			im = Image.open(fullname)
			namez=name[:-4]
			d = ImageDraw.Draw(im)
			f = ImageFont.truetype("Arial.ttf", 16)
			d.text((4,0), namez, font=f)
			im.save(fullname)
		
	else:
		print "Directory not found"
 

It scans the given images directory and adds the file name sans extension as a caption, as shown below:


Original:will.i.am_1.jpg
   
After adding the caption