TVGrabXX: change from never updating to always updating
[nonametv.git] / lib / NonameTV / Importer / TVGrabXX.pm
blob4a25e5e8b0935118548a25c730f18950c8351799
1 package NonameTV::Importer::TVGrabXX;
3 use strict;
4 use warnings;
6 =pod
8 Importer for data from other XMLTV sources using tv_grab_xx grabbers.
9 The tv_grab_xx should be run before this importer. The output file
10 of the grabber should be the file: $self->{FileStore} . "/tv_grab/" . $tvgrabber . ".xml";
12 Use grabber_data to specify grabber and the channel.
14 Example: to grab RAI1 using Italian grabber tv_grab_it, the grabber_data
15 will look like 'tv_grab_it;www.raiuno.rai.it'
17 Features:
19 =cut
21 use DateTime;
22 use XML::LibXML;
23 use Encode qw/encode decode/;
25 use NonameTV qw/norm AddCategory ParseXmltv/;
26 use NonameTV::Log qw/d progress error/;
27 use NonameTV::Config qw/ReadConfig/;
28 use NonameTV::DataStore::Helper;
30 use NonameTV::Importer::BaseOne;
32 use base 'NonameTV::Importer::BaseOne';
34 sub new {
35 my $proto = shift;
36 my $class = ref($proto) || $proto;
37 my $self = $class->SUPER::new( @_ );
38 bless ($self, $class);
40 #defined( $self->{UrlRoot} ) or die "You must specify UrlRoot";
42 my $dsh = NonameTV::DataStore::Helper->new( $self->{datastore} );
43 $self->{datastorehelper} = $dsh;
45 my $conf = ReadConfig();
46 $self->{FileStore} = $conf->{FileStore};
48 return $self;
51 sub FetchDataFromSite
53 my $self = shift;
54 my( $batch_id, $data ) = @_;
56 my( $tvgrabber, $tvchannel ) = ( $data->{grabber_info} =~ /^(.*);(.*)$/ );
57 $self->{tvgrabber} = $tvgrabber;
58 $self->{tvchannel} = $tvchannel;
60 my $xmlf = $self->{FileStore} . "/tv_grab/" . $tvgrabber . ".xml";
62 open(XMLFILE, $xmlf);
63 undef $/;
64 my $content = <XMLFILE>;
65 close(XMLFILE);
67 return( $content, 1 );
70 sub ImportContent
72 my $self = shift;
73 my( $batch_id, $cref, $chd ) = @_;
75 my $dsh = $self->{datastorehelper};
77 my $needhelper = 0;
78 my $date;
79 my $time;
80 my $currdate = "x";
82 my $ds = $self->{datastore};
83 $ds->{SILENCE_END_START_OVERLAP}=1;
85 my $prog = ParseXmltv (\$$cref, $self->{tvchannel});
86 my @shows = SortShows( @{$prog} );
87 foreach my $e (@shows) {
89 $e->{channel_id} = $chd->{id};
91 # translate start end from DateTime to string
92 $e->{start_dt}->set_time_zone ('UTC');
93 $e->{start_time} = $e->{start_dt}->ymd('-') . " " . $e->{start_dt}->hms(':');
94 delete $e->{start_dt};
96 # some XMLTV exports don't provide programme "stop" time
97 # helper is needed for such implementations
98 if( $e->{stop_dt} and ! $needhelper ){
99 # translate stop end from DateTime to string
100 $e->{stop_dt}->set_time_zone ('UTC');
101 $e->{end_time} = $e->{stop_dt}->ymd('-') . " " . $e->{stop_dt}->hms(':');
102 delete $e->{stop_dt};
103 } else {
104 $needhelper = 1;
107 # translate channel specific program_type and category to common ones
108 my $pt = $e->{program_type};
109 delete $e->{program_type};
110 my $c = $e->{category};
111 delete $e->{category};
113 if( $pt ){
114 my($program_type, $category ) = $ds->LookupCat( $chd->{xmltvid}, $pt );
115 AddCategory( $e, $program_type, $category );
117 if( $c ){
118 my($program_type, $category ) = $ds->LookupCat( $chd->{xmltvid}, $c );
119 AddCategory( $e, $program_type, $category );
122 if( $needhelper ){
123 ( $date, $time ) = ( $e->{start_time} =~ /^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}):\d{2}$/ );
124 if( $date ne $currdate ){
125 $dsh->StartDate( $date , "00:00" );
126 $currdate = $date;
127 progress("TVGrabXX: $chd->{xmltvid}: Date is $date");
129 $e->{start_time} = $time;
132 d( "TVGrabXX: $chd->{xmltvid}: $e->{start_time} - $e->{title}" );
134 if( $needhelper ){
135 $dsh->AddProgramme($e);
136 } else {
137 $ds->AddProgramme($e);
141 # Success
142 return 1;
145 sub bytime {
147 my( $Y1, $M1, $D1, $h1, $m1, $s1 ) = ( $$a{start_dt} =~ /^(\d+)\-(\d+)\-(\d+)T(\d+)\:(\d+)\:(\d+)$/ );
148 my $t1 = int( sprintf( "%04d%02d%02d%02d%02d%02d", $Y1, $M1, $D1, $h1, $m1, $s1 ) );
150 my( $Y2, $M2, $D2, $h2, $m2, $s2 ) = ( $$b{start_dt} =~ /^(\d+)\-(\d+)\-(\d+)T(\d+)\:(\d+)\:(\d+)$/ );
151 my $t2 = int( sprintf( "%04d%02d%02d%02d%02d%02d", $Y2, $M2, $D2, $h2, $m2, $s2 ) );
153 $t1 <=> $t2;
156 sub SortShows {
157 my ( @shows ) = @_;
159 my @sorted = sort bytime @shows;
161 return @sorted;