# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package Bref;

use strict;
use warnings;
use LWP::Simple;
use XML::Simple;

sub new {
	my $class = shift;
	my $self = {
		liste => "liste.txt",
		url_liste => "http://www.canalplus.fr/c-divertissement/pid3848-c-bref.html?tab=1-1",
		episodes => {},
	};
	bless ($self, $class);
	return $self;
}

sub download_page {
	my ($self,$liste) = @_;
	print "Downloading HTML...\n";
	open LISTE , ">>".$self->{liste} or die "Can't open ".$self->{liste};
	my $content = get($liste);
	foreach (split /^/,$content) {
		next unless $_ =~ /bref.html.*vid=/;
		$_ =~ s/\.\.//g;
		$_ =~ s/.*vid=(\d*).*/$1/g;
		print LISTE $_;
#		next unless $_ =~ /switch.*class/;
#		$_ =~ s/\.\.//g;
#		$_ =~ s/.*\((.*)\).*/$1/g;
#		print LISTE $_;
	}
	close LISTE;
}

sub download_xml {
	my $self = shift;
	open LISTE , "<".$self->{liste} or die "Can't open ".$self->{liste};
	my @ids = split /^/, join('',<LISTE>); 
	close LISTE;

	my %ids = map { $_ => 1 } @ids;
	my $xs = XML::Simple->new();
	foreach (reverse sort keys %ids) {
		chomp($_);
	 print "Downloading XML for episode $_ : http://service.canal-plus.com/video/rest/getVideosLiees/cplus/$_ ...\n";
		my $content = get("http://service.canal-plus.com/video/rest/getVideosLiees/cplus/$_");
		my $xml = $xs->XMLin($content);
  $self->parse_all_episodes($xml);
	}
}

sub parse_all_episodes {
	my ($self,$xml) = @_;
 my $liste = [];
 foreach my $ep (@{$xml->{VIDEO}}) {
  $self->{episodes}->{$ep->{ID}} = {
		image_small => $ep->{MEDIA}->{IMAGES}->{PETIT},
		image_large => $ep->{MEDIA}->{IMAGES}->{GRAND},
		titre => $ep->{INFOS}->{TITRAGE}->{TITRE},
		soustitre => $ep->{INFOS}->{TITRAGE}->{SOUS_TITRE},
		vues => $ep->{INFOS}->{NB_VUES},
		note_moyenne => $ep->{INFOS}->{NOTE}->{MOYENNE},
		note_votes => $ep->{INFOS}->{NOTE}->{NB_VOTES},
		date => $ep->{INFOS}->{PUBLICATION}->{DATE},
		heure => $ep->{INFOS}->{PUBLICATION}->{HEURE},
		id => $ep->{ID},
		url => $ep->{URL},
		video_hd => $ep->{MEDIA}->{VIDEOS}->{HD},
		video_hautdebit => $ep->{MEDIA}->{VIDEOS}->{HAUT_DEBIT},
		video_basdebit => $ep->{MEDIA}->{VIDEOS}->{BAS_DEBIT},
 	}
 }
 return $liste;

}

sub parse_episode {
	my ($self,$xml) = @_;

	return {
		image_small => $xml->{VIDEO}->{MEDIA}->{IMAGES}->{PETIT},
		image_large => $xml->{VIDEO}->{MEDIA}->{IMAGES}->{GRAND},
		titre => $xml->{VIDEO}->{INFOS}->{TITRAGE}->{TITRE},
		soustitre => $xml->{VIDEO}->{INFOS}->{TITRAGE}->{SOUS_TITRE},
		vues => $xml->{VIDEO}->{INFOS}->{NB_VUES},
		note_moyenne => $xml->{VIDEO}->{INFOS}->{NOTE}->{MOYENNE},
		note_votes => $xml->{VIDEO}->{INFOS}->{NOTE}->{NB_VOTES},
		date => $xml->{VIDEO}->{INFOS}->{PUBLICATION}->{DATE},
		heure => $xml->{VIDEO}->{INFOS}->{PUBLICATION}->{HEURE},
		id => $xml->{VIDEO}->{ID},
		url => $xml->{VIDEO}->{URL},
		video_hd => $xml->{VIDEO}->{MEDIA}->{VIDEOS}->{HD},
		video_hautdebit => $xml->{VIDEO}->{MEDIA}->{VIDEOS}->{HAUT_DEBIT},
		video_basdebit => $xml->{VIDEO}->{MEDIA}->{VIDEOS}->{BAS_DEBIT},
	};
}

sub get_episodes {
	my $self = shift;
	return $self->{episodes};
}

sub download_all_episodes {
	my $self = shift;
 unlink $self->{liste};
	$self->download_page($self->{url_liste});
	foreach ((0..10)) {
		$self->download_page($self->{url_liste}."&page=$_");
	}
	$self->download_xml();
}

1;
