Some cleanups and added ExtremeSports v2 Importer, as of time of writing you need...
[nonametv.git] / lib / NonameTV / Importer / ExtremeSports_v2.pm
blob21eace7c313398c8f297909bee4dea95247f5b58
1 package NonameTV::Importer::ExtremeSports_v2;
3 use strict;
4 use warnings;
6 =pod
8 Import data for Extreme Sports.
9 Version 2 - Mail - Working.
11 Features:
13 =cut
15 use utf8;
17 use DateTime;
18 use Spreadsheet::ParseExcel;
19 #use Data::Dumper;
21 use NonameTV qw/norm AddCategory MonthNumber/;
22 use NonameTV::DataStore::Helper;
23 use NonameTV::Log qw/progress error/;
24 use NonameTV::Config qw/ReadConfig/;
26 use NonameTV::Importer::BaseFile;
28 use base 'NonameTV::Importer::BaseFile';
30 sub new {
31 my $proto = shift;
32 my $class = ref($proto) || $proto;
33 my $self = $class->SUPER::new( @_ );
34 bless ($self, $class);
36 my $dsh = NonameTV::DataStore::Helper->new( $self->{datastore} );
37 $self->{datastorehelper} = $dsh;
39 return $self;
42 sub ImportContentFile {
43 my $self = shift;
44 my( $file, $chd ) = @_;
46 $self->{fileerror} = 0;
48 if( $file =~ /\.xls$/i ){
49 $self->ImportFlatXLS( $file, $chd );
50 } else {
51 error( "ExtremeSports: Unknown file format: $file" );
54 return;
57 sub ImportFlatXLS
59 my $self = shift;
60 my( $file, $chd ) = @_;
62 my $dsh = $self->{datastorehelper};
63 my $ds = $self->{datastore};
65 my %columns = ();
66 my $date;
67 my $currdate = "x";
69 progress( "ExtremeSports FlatXLS: $chd->{xmltvid}: Processing flat XLS $file" );
71 my $oBook = Spreadsheet::ParseExcel::Workbook->Parse( $file );
73 my($iR, $oWkS, $oWkC);
75 my( $time, $episode );
76 my( $program_title , $program_description );
77 my @ces;
79 # main loop
80 foreach my $oWkS (@{$oBook->{Worksheet}}) {
81 # Not using this yet.
82 if( $oWkS->{Name} !~ /Eng/ ){
83 progress( "ExtremeSports: $chd->{xmltvid}: Skipping other language: $oWkS->{Name}" );
84 next;
87 progress("--------- SHEET: $oWkS->{Name}");
89 # start from row 2
90 # the first row looks like one cell saying like "EPG DECEMBER 2007 (Yamal - HotBird)"
91 # the 2nd row contains column names Date, Time (local), Progran, Description
92 #for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
93 for(my $iR = 1 ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
95 # date (column 1)
96 $oWkC = $oWkS->{Cells}[$iR][0];
97 next if( ! $oWkC );
98 $date = ParseDate( $oWkC->Value );
99 #$date = $oWkC->Value;
100 next if( ! $date );
102 unless( $date ) {
103 progress("SKIPPING :D");
104 next;
107 if($date ne $currdate ) {
108 if( $currdate ne "x" ) {
109 # save last day if we have it in memory
110 # FlushDayData( $channel_xmltvid, $dsh , @ces );
111 $dsh->EndBatch( 1 );
116 my $batchid = $chd->{xmltvid} . "_" . $date;
117 $dsh->StartBatch( $batchid , $chd->{id} );
118 $dsh->StartDate( $date , "06:00" );
119 $currdate = $date;
121 progress("ExtremeSports: Date is: $date");
124 #if($iR == 28) { next; }
126 # time (column 1)
127 # print "hejhejhej";
128 $oWkC = $oWkS->{Cells}[$iR][1];
129 next if( ! $oWkC );
130 my $time = ParseTime( $oWkC->Value );
131 next if( ! $time );
133 #use Data::Dumper; print Dumper($oWkS->{Cells}[28]);
136 my $title;
137 my $test;
138 my $season;
139 my $episode;
141 # print "hejhej";
142 # program_title (column 3)
143 $oWkC = $oWkS->{Cells}[$iR][3];
145 # Here's where the magic happends.
146 # Love goes out to DrForr.
147 $test = $oWkC->Value;
149 $title = norm($test) if $test ne "";
150 # If no series title, get it from episode name.
153 $oWkC = $oWkS->{Cells}[$iR][5];
154 my $desc = $oWkC->Value;
156 if( $time and $title ){
158 # empty last day array
159 undef @ces;
161 progress("$time $title");
163 my $ce = {
164 channel_id => $chd->{id},
165 title => norm($title),
166 start_time => $time,
167 description => norm($desc),
170 ## Episode
171 $oWkC = $oWkS->{Cells}[$iR][29];
172 my $episode = $oWkC->Value;
174 $ce->{episode} = ". " . ($episode-1) . " ." if $episode ne "";
176 ## END
178 $dsh->AddProgramme( $ce );
180 push( @ces , $ce );
183 } # next row
185 } # next worksheet
187 $dsh->EndBatch( 1 );
189 return;
192 sub ParseDate {
193 my ( $text ) = @_;
195 #print ">$text<\n";
197 my( $year, $day, $month );
199 # format '2011-04-13'
200 if( $text =~ /^(\d+)\/(\d+)\/(\d+)$/i ){
201 ( $month, $day, $year ) = ( $text =~ /^(\d+)\/(\d+)\/(\d{2})$/i );
203 # format '2011-05-16'
204 } elsif( $text =~ /^\d{4}-\d{2}-\d{2}$/i ){
205 ( $year, $month, $day ) = ( $text =~ /^(\d{4})-(\d{2})-(\d{2})$/i );
208 $year += 2000 if $year < 100;
210 my $dt = DateTime->new(
211 year => $year,
212 month => $month,
213 day => $day,
214 time_zone => "Europe/Stockholm"
217 $dt->set_time_zone( "UTC" );
220 return $dt->ymd("-");
221 #return $year."-".$month."-".$day;
224 sub ParseTime {
225 my( $text ) = @_;
227 #print "ParseTime: >$text<\n";
229 my( $hour , $min );
231 if( $text =~ /^\d+:\d+$/ ){
232 ( $hour , $min ) = ( $text =~ /^(\d+):(\d+)$/ );
235 return sprintf( "%02d:%02d", $hour, $min );
240 ### Setup coding system
241 ## Local Variables:
242 ## coding: utf-8
243 ## End: