Dumb automated Tomnod map traverser

Few hours ago I read about the Tomnod challenge. We all know about the missing Malaysia Airlines flight MH370 that vanished from radar while en route to Beijing from Kuala Lumpur. Tomnod came up with this pretty novel idea. They made satellite images of places in the route accessible, so that people can view the map tiles and report if they see something interesting. The webapp is pretty cool and the UI is fine, but I thought it would be cooler if they had a play button. Maybe play over a couple of satellite images and you could view them from a distance and report if something is fishy. At present they require user interaction, you need to press buttons to traverse. What if I want to put this up in a shopping mall on a big screen. People waiting to get their products billed can have an eye on it and can report if they see something interesting. I think this would help get a lot of more people involved. To test this out, I made a little javascript which tries to traverse through the map automatically – without any user inputs.

The Script

var count = 0;
var threshold = 10;
function some_function(){
 if (count > threshold) {
 $(".info-panel-group > p:eq(1) > a:eq(1)").click();
 count = 0;
 var e = jQuery.Event("keyup");
 e.which = 37 + Math.floor((Math.random()*4));;
 jQuery("#map-canvas").trigger( e );
 count += 1;

// load jQuery and execute the main function
var interval_handle = window.setInterval(function() {
}, 10000);
$(".tagger-progress-view").append("Stop Moving");

So, now you could display the tomnod webpage on a large screen, run this piece of javascript on top of that and you get a dumb but an automated map traverser. Views?

How it feels to have your project featured on LifeHacker

It was an average day. I woke up at 11, attended a boring DSA class, came back, worked on IIITD’s website, then on GSF’s website and then on Odyssey’s website. Was bored, planned to doze off, but couldn’t sleep. Thought reading about some projects over github would be fun, opened github and saw this:

Screen Shot 2014-01-28 at 5.20.38 pm

RasPod is one of my old baby projects. Most of it was made in about 2 days as an entry to Raspberry Pi’s Summer coding contest. I haven’t really worked on it since then that means around 1.5 years of inactivity. Getting 20-30 likes out of nowhere is strange for such an old hack. I thought the project was probably shared by one of my popular friends on Google Plus; so, I searched #RasPod on Google Plus and got no new posts. I, then googled “RasPod” to check if someone posted about it in a blog or something.

Screen Shot 2014-01-28 at 5.38.09 pm

I was Ecstatic! Flabbergasted! To see a post featuring RasPod on LifeHacker.

Screen Shot 2014-01-28 at 6.08.38 pm

One of the happiest moments of my life! One of the best tokens of appreciation I have received from anybody! Thank you so much LifeHacker you made my day.

Bucket List:

Get a project featured on LifeHacker
Do laundry
World Domination

Make a facebook group photo collage

IIITD students photo collage

That’s the picture of some of the students registered in the IIITD Students group on facebook. Its generated using a script located here. Here’s how to use the script to generate a collage for your facebook group:

Step 1: Go to Facebook’s graph API explorer and do the following:

1) Click on “Get Access Token”

Screen Shot 2013-12-29 at 9.32.12 pm

2) Check the user_groups permission and click on “Get Access Token”.

You should see a page asking for permissions. Just click “Okay”.

Screen Shot 2013-12-29 at 9.30.58 pm Screen Shot 2013-12-29 at 9.34.13 pm

3) Get the facebook group ID using this.

4) Enter “<group_id>?fields=members” in the graph API prompt.

Screen Shot 2013-12-29 at 9.49.15 pm

Step 2: Copy paste the result into a text file.

Use this script to generate a json file of user profile and their corresponding images.

Step 3: Clone this repo and save the JSON file generated in 5th step as user_photos.json.

Step 4: Just open index.html in your favorite browser and you should have something looking like this.

Invite all your friends to like a page on facebook

The other day, I wanted to invite all of my friends to like my college cultural fest’s page: Odyssey 2014. Now, facebook does have an option to invite all of your friends to an event, but not to a page. So, I had to manually click on a ton of “invite” buttons on the page and I have around 300 friends, so I wasn’t in no way clicking on 300 invite buttons; instead I tried to automate this. And here‘s a script to do that.


Just drag Invite all my friends to your bookmarks and click on it when you are on a page, you want your friends to like.

Video Demonstration

How I fixed the “Hash Sum mismatch” problem in apt.

Today, I installed a new Ubuntu system was trying to set up OpenCV on it. I was getting issues installing libopencv-dev package.

As suggested in ubuntu forums and other places, I tried cleaning my lists and tried to download them again:

apt-get clean
rm -rf /var/lib/apt/lists/*
rm -rf /var/lib/apt/lists/partial/*
apt-get clean
apt-get update

This however, didn’t work for me. I got the same “Hash Sum mismatch” error while downloading the amd64 and i386 list. After spending 2 hours trying to fix this problem. I checked what mirrors, I was using to download my packages, by going to Ubuntu Software Center -> Edit -> Software Sources. It was downloading from the Indian server. Then I tried to check with the mirror, if it was up to date with the main Ubuntu server. And it turns out it wasn’t; it was actually running 1 week behind the server. I then changed the Software Source to the Main Server and it worked.

I have a C program, which loops ‘n’ times calling fork each loop. How many child processes do this program create?

First let’s consider a program which loops 2 times:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define N 2
int main(){
 int i;
 for (i = 1; i <= N; i++) fork();
 printf("Hey! from %d\n", getpid());

This program prints “Hey”, 4 times to the console, hence it creates 3 child processes. Let’s figure out how!

Note: The child processes have their local copies of variables, hence any operations on the variables won’t affect the parent’s copy or the other children’s copy.

1) The parent process (P0) runs through the loop 2 times, hence creating 2 child processes (P1 and P2). These child processes get their local copies of ‘i’ which is 1 for the first child and 2 for the second child. Thus, P2 runs through the loop 1 time while the P1 runs through the loop 2 times.

Note: The child always start executing from the instruction immediately after fork, which is i++ in our case.

2) P2 increments i from 2 to 3, followed by the comparison “i <= N” which obviously evaluates to false. Hence, P2 doesn’t create any processes.

3) P1 increments i from 1 to 2, it then executes fork() which creates another child with i as 2, lets call this child “P1_1″. After that it increments i from 2 to 3 and exits out of the loop.

4) P1_1  increments i from 2 to 3, and exits out of the loop.

So, at the end of the loop we have 3 children (P1, P2, P1_1).

Let’s now figure out the number of children for a program which loops ‘N’ times.
Let Pn be the nth child of the parent process, Pn_n be the nth child of the nth child of the parent process and so on.

Children with ‘i’ as 1: 1 (P1)

Children with ‘i’ as 2: 2 (P1_1, P2)

Children with ‘i’ as 3: 4 (P1_2, P2_1, P3, P1_1_1)

Do you notice a pattern in the above written numbers?

Number of children with ‘i’ as 1: 1

Number of children with ‘i’ as 2: 2^1

Number of children with ‘i’ as 3: 2^2


Number of children with ‘i’ as N: 2^(n-1)

Now we know that sum of G.P is \dfrac {a (r^n - 1)}{r - 1}

Hence, total number of child precesses created is: \dfrac {1 (2^n - 1)}{2 - 1}2^n - 1

I have n fork calls followed by another instruction ‘a’ in a C program. How many times will ‘a’ get executed?

If your answer to the above asked question is \dfrac{(n)(n - 1)}{2} you are wrong. Consider the following C program.

#include <stdlib.h>
#include <stdio.h>
int main(){
 pid_t c1 = fork(); // parent
 pid_t c2 = fork(); // parent, c1
 pid_t c3 = fork(); // parent, c1, child of parent & c2, child of c1 & c2

For 1st fork call we have 1 caller i.e the parent, this creates 1 child that then starts executing the rest of the program.

For 2nd fork call we have 2 callers i.e the parent and c1 this creates 2 children these then start executing the rest of the program.

For 3rd fork call we have 4 callers i.e  the parent, c1, child of parent & c2 and child of c1 & c2.

At the end of 3 fork calls we have 1 (1st fork) + 2 (2nd fork) + 4  (3rd fork) = 7 children and 1 parent. Thus, The printf gets executed 8 (2^3) times. We can easily deduce that the pattern in the number of children is a G.P with 2 as its common ratio.

We know that sum of G.P is \dfrac{a (r^n - 1)}{r - 1} where n is the number of terms, a is the first term and r is the common ratio.

For our case: a = 1, r = 2. Hence the sum = \dfrac{(1) (2^n - 1)}{2 - 1} = 2^n - 1

So, for a program containing n fork calls followed by another instruction ‘a’. The instruction will be executed 2^n times (2^n -1 + 1).

Breaking and fixing ‘pipy_downloads’

Pipy_downloads, a part of PyUtilb, is a utility to calculate the total number of downloads that a particular PyPI package has received across all versions tracked by PyPI. I wanted to check the number of downloads for all pypybox2d packages, so I ran the following command:

pypi_downloads pypybox2d

which broke pypi_downloads and popped out the following error message:

Traceback (most recent call last):
 File "/usr/local/bin/pypi_downloads", line 8, in 
 load_entry_point('pyutilib.misc==5.3.1', 'console_scripts', 'pypi_downloads')()
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 166, in main
 _main(args, options.exact)
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 147, in _main
 PyPIDownloadAggregator(pkg, exact=exact).stats()
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 116, in stats
 keys.sort(key=lambda x: [int(y) for y in x.split('.')])
 File "/Library/Python/2.7/site-packages/pyutilib.misc-5.3.1-py2.7.egg/pyutilib/misc/pypi_downloads.py", line 116, in 
 keys.sort(key=lambda x: [int(y) for y in x.split('.')])
ValueError: invalid literal for int() with base 10: '1-r331'

Looking into the code, it seems that Pypy-downloads will break for any package with version format other than x.x.x where x represents a number. It can be fixed with a simple hack:

keys.sort(key=lambda x: [int(y) for y in x.strip('-r331').split('.') if y.isdigit()])

Now, it strips off ‘-r331′ from the version number. And also checkes if each x in x.x.x is a digit, if it isn’t a digit it ignores that part of the version number.

How come photons possess energy?

Photons have 0 rest mass. So how come they possess energy? I mean if you take photons as regular matter you can deduce that they have no momentum as they have 0 mass and hence according to Energy-Momentum relation it doesn’t have energy. Right? Wrong!

The linear momentum’s definition is only valid for regular matter. Photons are not regular matter. Let’s take the example of fields, say electromagnetic field. An electromagnetic field can give momentum to a charge. Hence, according to the conservation of momentum EM fields have momentum. But do they have mass? Of course it doesn’t its a field after all. Hence we deduce that mass is not necessary for momentum to exist at least not for all kinds of matter.

If photon has momentum it sure can have energy. That answers our question!


Do you use Google App Engine with Django templates? Your deployments could be vulnerable to XSS

Just to clear up the title: There are no “security holes” in Django or GAE per se but the way people use these and assume some things cause the XSS vulnerability.

Autoescaping was added in [6671] on Nov. 14 2007 in Django i.e Django 0.96 doesn’t have this functionality. Google App Engine uses Django 0.96 as a default (unless you specify the version explicitly using use_version). Now most of the people new to Google App Engine [especially the ones migrating from other platforms] doesn’t know this thing, hence causing the vulnerability.

How to check if you code is vulnerable:

1) Do you use Django version 0.96 or older. If no, you are safe. If yes, read further.

2) Do you escape each user editable variable in your Django templates using the ‘escape’ filter or are you properly escaping these variables. If yes, you are safe. If no, you are vulnerable.

How to fix:

The most elegant way to fix is to simply use a later version of Django.

For example to use Django 1.2 add the following lines at the top of your handlers.

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

But if for some reason you can’t do that. You’ll have to use the escape filter to escape each user-editable variable in your templates.