The Essentials

Unheard-of-solutions made familiar

Automatic Torrent download from eztv_it

leave a comment »

If you live on this side on the Atlantic or the Pacific, TV buffs all face a problem. We do not get the TV shows that air in the US! Even if the show does air here, it’s an ancient season which has been out in Blu Ray since the cave men.

However, thankfully to the internet, we can always torrent download the TV show anytime we want. But wait,
1. there’s keeping track of all the TV shows you watch,
2. remembering to download the episode after it airs,
3. sifting through torrent sites in search for the torrent that actually works.

Thats a lot of work especially since we don’t have TiVo either. But what are computers for?
I present here my solution to automate this entire process of,
1. downloading your tv shows as soon as they are available online,
2. not having to sift through all the torrent sites,
3. have them downloaded even if you forget.

What you will need
1. One laptop running linux.
2. One internet connection.
3. One php5 installation.
4. One crontab

Go to ez_tv’s account on Twitter. You will notice almost every update is a link to a torrent for a tv show. You can find almost every show here, including one’s that don’t air in the US. But the problem is, you would have to search the posts for the TV show you are looking for. This can be hell, especially if you try to use it a couple days after your TV show has aired.
Fortunately, Twitter provides apis, which provide unprotected tweets as RESTful services. This basically means, you can get all those posts as raw text and write a program which will sift through it and get your favorite TV shows.

I used php to write a quick hack on being able to do this. I also used this plugin for making REST calls to twitter.
You can find my code here:

<?php
require_once('rest.php');

$rws = new RestServiceClient('http://twitter.com/statuses/user_timeline.json');
user_id = 37039456;#eztv_it user_id
$rws->count = 20;
$rws->excuteRequest();
$json = $rws->getResponse();
$decodejson = json_decode($json);

#XYZ is the regex that gets the right show name you are looking for.
$shows = array('/XYZ */');

foreach($decodejson as $text){
foreach($shows as $showregx){
if(preg_match($showregx,$text->text,$showarr)){
$stringarr = explode('-',$text->text);

#get the torrent link
$splitter = '/http*/';
$matches = preg_split($splitter,$text->text);

#get filename for downloaded torrent
$filenamecreator = '/.it\//';
$filename = preg_split($filenamecreator,$matches[1]);

#download file and save it. make sure this dir has write permissions
$myFile = '/Downloads/'.$filename[1];
$file_contents = file_get_contents('http'.$matches[1]);
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh,$file_contents);
}
}
}

#add to transmission and start the download! yeayy...
$dir = '/Downloads/twitter';
$dh = opendir($dir);
$filestoaddtotransmission;
while(false !== ($torrfile = readdir($dh))){
if($torrfile != '.' && $torrfile != '..')
$filestoaddtotransmission = $filestoaddtotransmission.' '.$dir.'/'.$torrfile;
}
`transmission $filestoaddtotransmission &`;
closedir($dh);
?>

All you need to do next is to pop this in your crontab.
Open up crontab by typing

your-terminal-prompt:~$crontab -e

You should see like this (if its the first time, it will ask your choice of editor to view it in):

# m h dom mon dow command

Check out crontab on wikipedia to know how to put entries here. You should run the php script from the command line (with php command) and not using wget through your local webserver. This is because, it’s a little trickier to set up backtick operator to run with GUI programs like transmission, when the script is running through a web server.
:wq

Written by Niranjan Prithviraj

November 21, 2009 at 8:41 am

Posted in Technology

Tagged with , , , , ,

Jquery for beginners – 1

leave a comment »

This is for jquery beginners.

Recently I was working with jquery for some very simple and straighforward ajax. It was so simple, that i created this post with QuickPress.
One of the fields in my Ajax request was a phone number containing atleast one ‘+’ sign. For some reason, the plus sign was never actually part of the request string I was sending. (I found this out after a good deal of investigation). This cascaded and caused a lot of other problems , server side validation stopped working and such…

The simple and cool thing with Jquery is that, if ‘dataType’ attribute of the ajax request is left blank, a preliminary clean up of the data string/hash is done. Therefore ff you have plus signs in your data string, they will be cleaned up.

To stop this from happening, set the ‘dataType’ attribute to ‘text’ and everything will be fine again.

Written by Niranjan Prithviraj

July 20, 2009 at 10:03 am

SSL on nginx

leave a comment »

I have worked with Apache and understand how the server files are all organized and what everything means.
However when it comes to nginx, I wasn’t as adept.

The challenge was to set up SSL on nginx. It’s actually pretty easy. But not that easy in Scalr.
Another problem I faced was that, all the websources i found, to sucessfully set this up, was not in one place. Also, some sources were misleading.

Here, I will point to the resources that helped along the way. At the end of these steps you would have created self signed SSL certificates on nginx and setup SSL sucessfully. Ofcourse you might not need to create your own certificates, but its still fun to learn how to.

Create self-signed SSL certificates for nginx here on Ubuntu. Simple enough.

Once you’ve followed that perfectly, simply adding the following lines to nginx.conf worked for me.
This code is originally brought to you by Getting HTTPS working on your farm.

Oh ya.. you can find njinx.conf at /etc/nginx/nginx.conf

server {
listen 443;
ssl on;

# path to your certificate
ssl_certificate /etc/ssl/certs/myssl.crt;

# path to your ssl key
ssl_certificate_key /etc/ssl/private/myssl.key;

ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!ADH;
ssl_prefer_server_ciphers on;

location / {
proxy_pass http://backend; proxy_buffering on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header HTTPS on;

client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 15;
proxy_intercept_errors on;
}
}

This entry is basically a replica of what is there already for port 80. The difference would be the first few lines that includes the path to the certificates.
This seems to work for me.

UPDATE:
You need to add this to the existing entry for port 80 for redirecting all pages to HTTPS.
rewrite ^(.*) https://your.domain.com$1 redirect;

If you need only one directory to be redirected, for ex: login directory, then add this to the entry for port 80.
rewrite ^/dirname(.*) https://your.domain.com/dirname$1 redirect;

Written by Niranjan Prithviraj

June 18, 2009 at 8:36 pm