#!/usr/bin/perl -w

# IMPORTANT: The retrieved tariff data may only be used by the LCR plugin
# showing the logo and the URL www.teltarif.de as their source.

# WICHTIG: Die bezogenen Tarifdaten dürfen nur für die Anzeige im LCR Plugin,
# mit Einblendung des Logos und der URL www.teltarif.de als Quelle, verwendet werden.

use strict;
use warnings;
use HTML::Parser;
use LWP::Simple;

my $url = "http://www.telefontarife.de/";
my $content = get($url);

my $target_counter = 0;
my $line_counter = 0;

my @targets = ( "City", "Nah", "Fern", "D1", "D2", "E-Plus", "O2" );

# pVariable (p stands for pattern)
my $pcity		= '<!-- City-Tarif -->';
my $pregio50		= '<!-- Regio 50 Tarif -->';
my $pfern		= '<!-- Fern Tarif -->';

my $pd1			= '<!-- D1-Netz -->';
my $pd2			= '<!-- D2-Netz -->';
my $peplus	= '<!-- E-Plus-Netz -->';
my $po2			= '<!-- O2-Netz -->';

my $comment_start = "false";
my $tmp = 0;

# specify events here rather than in a subclass
my $p = HTML::Parser->new( api_version => 3,
         start_h	=> [\&start,"tagname, attr, attrseq, text"],
         end_h		=> [\&end,"tagname, text"],
	 text_h		=> [\&text,"text, is_cdata"],
         comment_h	=> [\&comment,"event, text"]
#         default_h   => [sub { print shift }, "text"],
       );

# Remove whitespace from the start and end of the string
sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	$string =~ s/\n+$//;
	return $string;
}

sub comment
{
	my($self, $comment) = @_;
	
	if ( $comment eq $pcity  )
	{
		$comment_start = "true";
		$target_counter++;
	}
	
	if ( $comment eq $pregio50 )
	{
		$comment_start = "true";
		$target_counter++;
	}
	
	if ( $comment eq $pfern )
	{
		$comment_start = "true";
		$target_counter++;
	}
	
	if ( $comment eq $pd1 )
	{
		$comment_start = "true";
		$target_counter++;
	}
	
	if ( $comment eq $pd2 )
	{
		$comment_start = "true";
		$target_counter++;
	}
	
	if ( $comment eq $peplus )
	{
		$comment_start = "true";
		$target_counter++;
	}
	
	if ( $comment eq $po2 )
	{
		$comment_start = "true";
		$target_counter++;
	}
	
	
}       
sub start
{
	my ($tag, $attr, $attrseq, $origtext) = @_;
  
#      unless ($tag =~ /^a$/) {
#          print $origtext;
#          return;
	if ( ( $tag =~ /^b$/ ) ) {
		return;
	}
  }
sub end {
	my($tagname, $origtext) = @_;
	
	# the provider's number is printed in bold
	if ( ( $tagname =~ /^b$/ ) && ( $comment_start eq "true" ) ) {
		$comment_start = "false";
		$line_counter = 1;
		return;
	}
}

sub text {
	my($text, $is_cdata) = @_;
	my @result;
	# find the telephone number for the provider
	if ( $text =~ /\d/ && ( $comment_start eq "true" ) )
	{
#		print $targets[$target_counter-1],": ",$text,"\n";
		print $text,"\n";
		$line_counter++;
		return;
	}
	if ( $line_counter == 1 )
	{
		# get rid of the newlines
		@result = split( /\n+/, $text);
		$text = $result[1];
		# get rid of the hard-coded html whitespace
		$text =~ s/&nbsp;/ /s;
#		print "Preis: ",$text,"\n";
		print $text,"\n";
		$line_counter++;
	}
	elsif ( $line_counter == 2 )
	{
		# get rid of the newlines
		@result = split( /\n+/, $text);
		$result[1] = substr($result[1], 0, length($result[1])-5);
#		print "Takt: ",$result[1],"\n";
		print $result[1],"\n";
		$line_counter++;
	}
	# not interested in more lines, we have all our information
	if ( $line_counter gt 2 )
	{
		$line_counter = 0;
	}
}

$p->parse($content);
