rendered paste body#!/usr/bin/perl
use LWP::Simple; # libwww-perl providing simple HTML get actions
use HTML::Entities;
use URI::Escape;
use Switch;
my @OPTIONS ; #this contains options
my $NAME ; #Movie name
my $count ;
foreach (@ARGV) {
if ($_ =~ m/^(--)/i) {
push(@OPTIONS, $_) ;
}
else {
$NAME=$NAME. $_ . " ";
}
}
$NAME =~ s/( )$//g ;
my $QUERY = $NAME ;
$QUERY=~ s/ /\+/g ;
subsearch ($QUERY) ;
#Subroutines
# returns text within 'data' between 'beg' and 'end' matching strings
sub parseBetween {
my ($data, $beg, $end)=@_; # grab parameters
my $ldata = lc($data);
my $start = index($ldata, lc($beg)) + length($beg);
my $finish = index($ldata, lc($end), $start);
if ($start != (length($beg) -1) && $finish != -1) {
my $result = substr($data, $start, $finish - $start);
# return w/ decoded numeric character references
# (see http://www.w3.org/TR/html4/charset.html#h-5.3.1)
decode_entities($result);
return $result;
}
return "";
}
sub usage {
print "subsearch.pl <options> <name of the movie>\n" ;
print "\n" ;
print "Options\n" ;
print "--exact only show exact matches\n";
}
sub subsearch {
my ($QUERY)=@_;
my $REQUEST = "http://subsearch.org/search.php?q=" . $QUERY . "&slang=fi" ;
my $RESPONSE = get $REQUEST;
@LINKS = split(/a href=\"/, $RESPONSE);
foreach (@LINKS) {
my $TITLE = parseBetween($_,"<nobr>","</nobr>");
$TITLE =~ s/<span class=\"result\">//;
$TITLE =~ s/<\/span>//;
$_ =~ s/\"[^\.]*//;
if ( $TITLE =~ m/$NAME/i ) {
switch ($_) {
case m/opensubtitles/ { opensubtitles($_, $TITLE) }
case m/subiarkisto/ { subiarkisto($_, $TITLE) }
}
}
}
}
sub subiarkisto {
my ($ID, $TITLE)=@_;
$ID =~ s/[a-z]//g;
$ID =~ s/\///g;
$ID =~ s/://g;
$ID =~ s/\.//g;
$ID =~ s/\&//g;
$ID =~ s/\=//g;
$ID =~ s/\?//g;
print "$TITLE: " . "http:\/\/subiarkisto.org\/subiarkisto.php?action=download&id=" . "$ID\n" ;
}
sub opensubtitles {
my ($ID, $TITLE)=@_;
$ID =~ s/[a-z]//g;
$ID =~ s/\///g;
$ID =~ s/://g;
$ID =~ s/\.//g;
print "$TITLE: ". "http:\/\/www.opensubtitles.com\/fi\/download\/sub\/" ."$ID\n";
}