#!/usr/bin/perl -w ####################################################################### # This program is free software; you can redistribute it and/or # # modify it under the terms of the GNU General Public License # # version 3 as published by the Free Software Foundation. # # # # This program is distributed in the hope that it will be useful, but # # WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # # General Public License for more details. # # # # Written and (C) by Christopher Wellons # # Contact for comments & bug reports # ####################################################################### ## To use this, all you need is wget and Perl. The program takes any ## number of arguments refering to South Park Episodes. Episodes are ## in the form SSEE (S = season, E = episode). Seasons can be one or ## two digits. These are equal, ## ## spd.pl 0203 ## ## spd.pl 203 ## ## The program also accepts ranges of episodes, ## ## spd.pl 203-211 ## ## Ranges do not work across seasons. If a non-number is entered, the ## program will attempt to search for an episode. This, for example, ## will download the famous Make Love, Not Warcraft episode, ## ## spd.pl warcraft ## ## Enjoy! ## ## Default options $verbose = 1; $quiet = 0; $waitlen = 2; $wgetopts = ""; ## Strings $q = ""; $q = "-q" if ($quiet); $baseurl = "http://www.southparkzone.com"; ## Parse arguments (to do) if (@ARGV < 1) { print "USAGE: spd [ episode_number/range | search_term ] [...]\n"; exit (); } ## Build list of episodes @eps = (); foreach $i (@ARGV) { @eps = (@eps, expand_num($i)); } foreach $ep (@eps) { $title = undef; $num = undef; $id = $ep; ## Search term? Then search for it, damnit if (!($ep =~ /^\d{3,4}$/)) { pstat("Searching term \"$ep\" ...\n"); $id = search_ep($ep); if (!defined($id)) { perror ("search query \"$ep\" failed\n"); next; } } pstat("Getting mirror number ... \n"); ($num, $title) = get_num ($id); ## Get episode URL pstat("Getting episode URL ...\n"); if (!defined($title)) { ($url, $title) = get_url ($num); } else { $url = get_url ($num); } ## Download episode pstat("Downloading episode: $title ($num)\n"); $id = sprintf ("%.4d", $id); if (!defined($title)) { $title = "Unknown Title"; } else { $title = clean_title ($title); } $filename = "South Park - $id $title.flv"; system "wget $wgetopts $q $url -O \"$filename\"" || perror("failed to download episode $id\n"); sleep($waitlen); } ## Expand number range sub expand_num { $_ = $_[0]; ## Check for ID or non-range if (!/^\d{3,4}-\d{3,4}$/) { return $_; } s/-/../; return eval; } ## Print status message sub pstat { if ($verbose) { print $_[0]; } } ## Print error message and exit sub perror { print STDERR $_[0]; } ## Get episode number and title from episode ID sub get_num { my $id = $_[0]; my $title; open (HTMLPAGE, "wget $wgetopts -q -O - $baseurl/episodes/$id/ |"); while () { if (/([^-]) -/) { $title = $1; } if (/episodeinfo\.php\?mirror=(\d+)/) { $num = $1; last; } } if (!defined($num)) { perror("failed to retrieve episode number\n"); } close (HTMLPAGE); return ($num, $title); } ## Get episode URL based on episode number sub get_url { my $num = $_[0]; my $title; open (XMLPAGE, "wget $wgetopts -q -O - $baseurl/episodeinfo.php?mirror=$num |"); while (<XMLPAGE>) { if (/(http.+flv)/) { $url = $1; last; } if (/<title>(.+)<\/title>/) { $title = $1; } } if (!defined($url)) { perror ("failed to retrieve episode URL\n"); } return ($url, $title); } sub clean_title { my $title = $_[0]; $title =~ s/&#\d\d;//g; $title =~ s/[^\w ]//g; return $title; } sub search_ep { $query = $_[0]; $search_base = "http://www.southparkzone.com/search/results.php?q="; open (QPAGE, "wget $wgetopts -q -O - \"$search_base$query\" |"); while (<QPAGE>) { if (/\.\.\/episodes\/(\d{4})\//) { return $1; } } return undef; close (QPAGE); }