#!/usr/bin/perl

use strict;
use warnings;
use Carp;
use LWP::Simple;

# First - LWP::Simple.  Download the page using get();.
my $content = get( "http://www.legaltorrents.com/rss.xml" ) or die $!;

my $start = 0;
my ($name, $url);

# go past first "LegalTorrents" title
$name = substr $content, $start+7, ((index $content, "</title>", $start) - ($start+7));
$start = index $content, "<link>", $start+1;
$url = substr $content, $start+6, ((index $content, "</link>", $start) - ($start+6));

while (($start = (index $content, "<title>", $start+1)) != -1){

  # get name and url of torrent  
  $name = substr $content, $start+7, ((index $content, "</title>", $start) - ($start+7));
  $start = index $content, "<link>", $start+1;
  $url = substr $content, $start+6, ((index $content, "</link>", $start) - ($start+6));
  
  if (! -e $name)
  {
    print "Name: $name\n";
    print "URL: $url\n";
    system("nohup btdownloadheadless.bittornado --saveas \"$name\" $url &");
  }
}

