#!/usr/bin/perl
# This makes neat little feeds.

use strict;
use warnings;

use CGI qw(:standard);

sub print_header() {
	print "Content-type:text/html\n\n";

	print <<HEAD_HTML

<html>
<head>
<title>Search feed creator (privateish beta)</title>
</head>
<body>
<h1>Search feed creator (privateish beta)</h1>
<p>This application will create feeds that:
<ol>
<li>Search for a specific term on <a href="http://www.tokyotosho.com">TokyoTosho</a>.</li>
<li>Filter those by making sure a specific term (Say, a fansub group name) appears in the file name</li>
<li>Format the result as a nice RSS feed with enclosures, ready for use in any video podcast reader (Like iTunes, or <a href="http://www.getmiro.com/">Miro Player</a>.
</ol>
Basically, it makes it possible to automatically download episodes of some anime series or drama.
</p>
HEAD_HTML
;

}

sub print_footer() {
	print "</body></html>";
}

sub print_form() {
	print <<FORM_HTML
<h2>Make a link</h2>
<p>Example values are for a feed of Dattebayos Naruto fansubs.</p>
<p>
	<form action="" method="post">
		Search term: <input type="text" name="ani_name" value="DB Naruto"></input><br/>
		File name must include: <input type="text" name="filter_by" value="[DB]"></input><br/>
		Feed title: <input type="text" name="feed_title" value="Naruto [DB]"></input><br/>
		Feed image: <input type="text" name="feed_image" value="http://halcy.de/feedicons/naruto.jpg"></input> (Hey, please use something you host yourself. Bandwith stealing is bad.)<br/>
		<input type="submit" value="Lets-a go!" />
	</form>
</p>
FORM_HTML
;
}

sub print_link( $$$$ ) {
	my $anime = shift();
	my $filter = shift();
	my $title = shift();
	my $image = shift();

	print "<h2>Feed for $title</h2><p><a href=\"";
	print 'http://halcy.de/misc/itunesify.pl?u=http%3A%2F%2Fpipes.yahoo.com%2Fpipes%2Fpipe.run%3FAnime_title%3D' . $anime . '%26Must_contain%3D' . $filter .  '%26_id%3DUjKOuZn33BGtlmhWCB2yXQ%26_render%3Drss&amp;i=' . $image . '&amp;t=' . $title;
	print "\">Here is your feed for $title</a>. Please do not hit it up too often, or you <i>will</i> break stuff. Once or twice per hour should be enough.</p>";
}

my $anime = param( 'ani_name' );
my $filter = param( 'filter_by' );
my $title = param( 'feed_title' );
my $image = param( 'feed_image' );

if( defined( $anime ) ) {
	print_header();
	print_link( $anime, $filter, $title, $image );
	print_footer();
} else {
	print_header();
	print_form();
	print_footer();
}