Alot of new importers, bugfixes at Turner (other sort of date in excel-files)
[nonametv.git] / lib / NonameTV / Importer / TravelChannel_v2.pm
blob1b3c6690b8eb939b8481d41ef5abb177c1d92260
1 package NonameTV::Importer::TravelChannel_v2;
3 use strict;
4 use warnings;
6 =pod
8 Import data for TravelChannel.
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/MyGet 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 $self->{MinMonths} = 1 unless defined $self->{MinMonths};
37 $self->{MaxMonths} = 2 unless defined $self->{MaxMonths};
39 defined( $self->{UrlRoot} ) or die "You must specify UrlRoot";
41 my $dsh = NonameTV::DataStore::Helper->new( $self->{datastore} );
42 $self->{datastorehelper} = $dsh;
44 return $self;
47 sub ImportContentFile {
48 my $self = shift;
49 my( $file, $chd ) = @_;
51 $self->{fileerror} = 0;
53 if( $file =~ /\.xls$/i ){
54 $self->ImportFlatXLS( $file, $chd );
55 } else {
56 error( "TC: Unknown file format: $file" );
59 return;
62 sub ImportFlatXLS
64 my $self = shift;
65 my( $file, $chd ) = @_;
67 my $dsh = $self->{datastorehelper};
68 my $ds = $self->{datastore};
70 my %columns = ();
71 my $date;
72 my $currdate = "x";
74 progress( "TC FlatXLS: $chd->{xmltvid}: Processing flat XLS $file" );
76 my $oBook = Spreadsheet::ParseExcel::Workbook->Parse( $file );
78 my($iR, $oWkS, $oWkC);
80 my( $time, $episode );
81 my( $program_title , $program_description );
82 my @ces;
84 # main loop
85 foreach my $oWkS (@{$oBook->{Worksheet}}) {
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 = 2 ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
95 # date (column 1)
96 $oWkC = $oWkS->{Cells}[$iR][1];
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("TC: Date is: $date");
124 #if($iR == 28) { next; }
126 # time (column 1)
127 # print "hejhejhej";
128 $oWkC = $oWkS->{Cells}[$iR][2];
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;
140 #my $epg;
142 # print "hejhej";
143 # program_title (column 3)
144 $oWkC = $oWkS->{Cells}[$iR][3];
146 # Here's where the magic happends.
147 # Love goes out to DrForr.
148 $title = $oWkC->Value;
151 # EPG is actually listing.
152 $oWkC = $oWkS->{Cells}[$iR][8];
153 my $epg = $oWkC->Value;
155 $oWkC = $oWkS->{Cells}[$iR][6];
156 my $subtitle = $oWkC->Value;
158 $oWkC = $oWkS->{Cells}[$iR][4];
159 my $sea = $oWkC->Value;
161 $oWkC = $oWkS->{Cells}[$iR][5];
162 my $epi = $oWkC->Value;
164 # Episode info in xmltv-format
165 if( ($epi > 0) and ($sea > 0) )
167 $episode = sprintf( "%d . %d .", $sea-1, $epi-1 );
169 elsif( $epi > 0 )
171 $episode = sprintf( ". %d .", $epi-1 );
174 if( $time and $title ){
176 # empty last day array
177 undef @ces;
179 progress("$time $title");
181 my $ce = {
182 channel_id => $chd->{id},
183 title => norm($title),
184 start_time => $time,
185 subtitle => norm($subtitle),
186 episode => $episode,
187 description => $epg,
190 ## Episodes and so on ( Doesn't seem to work, fix this later. )
192 ## END
194 $dsh->AddProgramme( $ce );
196 push( @ces , $ce );
199 } # next row
201 } # next worksheet
203 $dsh->EndBatch( 1 );
205 return;
208 sub ParseDate {
209 my ( $text ) = @_;
211 #print ">$text<\n";
213 my( $year, $day, $month );
215 # format '2011-04-13'
216 # if( $text =~ /^(\d+)\/(\d+)\/(\d+)$/i ){
217 # ( $month, $day, $year ) = ( $text =~ /^(\d+)\/(\d+)\/(\d{2})$/i );
219 # format '2011-05-16'
220 # } elsif( $text =~ /^\d{4}-\d{2}-\d{2}$/i ){
221 # ( $year, $month, $day ) = ( $text =~ /^(\d{4})-(\d{2})-(\d{2})$/i );
224 #if( $text =~ /^(\d+)-(\w)-(\d+)$/ ){
225 # ( $day, $month, $year ) = ( $text =~ /^(\d+)-(\w)-(\d+)$/ );
227 if( $text =~ /^(\d+)-Jan-(\d+)$/ ){
228 ( $day, $year ) = ( $text =~ /^(\d+)-Jan-(\d+)$/ );
229 $month = "01";
230 } elsif( $text =~ /^(\d+)-Feb-(\d+)$/ ){
231 ( $day, $year ) = ( $text =~ /^(\d+)-Feb-(\d+)$/ );
232 $month = "02";
233 } elsif( $text =~ /^(\d+)-Mar-(\d+)$/ ){
234 ( $day, $year ) = ( $text =~ /^(\d+)-Mar-(\d+)$/ );
235 $month = "03";
236 } elsif( $text =~ /^(\d+)-Apr-(\d+)$/ ){
237 ( $day, $year ) = ( $text =~ /^(\d+)-Apr-(\d+)$/ );
238 $month = "04";
239 } elsif( $text =~ /^(\d+)-May-(\d+)$/ ){
240 ( $day, $year ) = ( $text =~ /^(\d+)-May-(\d+)$/ );
241 $month = "05";
242 } elsif( $text =~ /^(\d+)-Jun-(\d+)$/ ){
243 ( $day, $year ) = ( $text =~ /^(\d+)-Jun-(\d+)$/ );
244 $month = "06";
245 } elsif( $text =~ /^(\d+)-Jul-(\d+)$/ ){
246 ( $day, $year ) = ( $text =~ /^(\d+)-Jul-(\d+)$/ );
247 $month = "07";
248 } elsif( $text =~ /^(\d+)-Aug-(\d+)$/ ){
249 ( $day, $year ) = ( $text =~ /^(\d+)-Aug-(\d+)$/ );
250 $month = "08";
251 } else {
252 return undef;
255 my %mon2num = qw(
256 jan 1 feb 2 mar 3 apr 4 maj 5 jun 6
257 jul 7 aug 8 sep 9 okt 10 nov 11 dec 12
260 #print ">$month<";
262 #$month = $mon2num{ lc substr($month, 0, 3) };
264 $year += 2000 if $year < 100;
266 my $dt = DateTime->new(
267 year => $year,
268 month => $month,
269 day => $day,
270 time_zone => "Europe/Stockholm"
273 $dt->set_time_zone( "UTC" );
276 return $dt->ymd("-");
277 #return $year."-".$month."-".$day;
280 sub ParseTime {
281 my( $text ) = @_;
283 #print "ParseTime: >$text<\n";
285 my( $hour , $min );
287 if( $text =~ /^\d+:\d+$/ ){
288 ( $hour , $min ) = ( $text =~ /^(\d+):(\d+)$/ );
291 return sprintf( "%02d:%02d", $hour, $min );
296 ### Setup coding system
297 ## Local Variables:
298 ## coding: utf-8
299 ## End: