NonameTV.pm - MonthNumber function added
[nonametv.git] / lib / NonameTV / Importer / Port.pm
bloba4634a6624484c54c74734fe259d63fc347a9d31
1 package NonameTV::Importer::Port;
3 use strict;
4 use warnings;
6 =pod
8 Import data from Xml-files downloaded from PORT.hu
10 Features:
12 =cut
14 use utf8;
16 use DateTime;
17 use XML::LibXML;
19 use NonameTV qw/MyGet norm AddCategory/;
20 use NonameTV::Log qw/info progress error logdie/;
22 use NonameTV::Importer::BaseOne;
24 use base 'NonameTV::Importer::BaseOne';
26 sub new {
27 my $proto = shift;
28 my $class = ref($proto) || $proto;
29 my $self = $class->SUPER::new( @_ );
30 bless ($self, $class);
32 $self->{grabber_name} = "Port";
34 defined( $self->{UrlRoot} ) or die "You must specify UrlRoot";
36 return $self;
39 sub FetchDataFromSite
42 my $self = shift;
43 my( $batch_id, $data ) = @_;
45 my $u = $self->{UrlRoot} . "/" . $data->{grabber_info};
46 progress("Port: Fetching data from $u");
48 my ( $content, $code ) = MyGet ( $u );
50 return( $content, $code );
53 sub ImportContent {
54 my $self = shift;
55 my( $batch_id, $cref, $chd ) = @_;
57 my $channel_id = $chd->{id};
58 my $channel_xmltvid = $chd->{xmltvid};
59 my $ds = $self->{datastore};
60 $ds->{SILENCE_END_START_OVERLAP}=1;
62 progress( "Port: $channel_xmltvid: Processing XML" );
64 my $doc;
65 my $xml = XML::LibXML->new;
67 eval { $doc = $xml->parse_string($$cref); };
68 if( $@ ne "" ) {
69 error( "Port: $batch_id: Failed to parse $@" );
70 return 0;
73 # find for how many channels this file has data
74 my $channels = $doc->findnodes( "//Channel" );
75 if( $channels->size() == 0 ) {
76 error( "Port: $channel_xmltvid: $$cref: No channels found" ) ;
77 return;
79 progress( "Port: $channel_xmltvid: found " . $channels->size() . " channels" );
81 # browse through channels
82 foreach my $chan ($channels->get_nodelist) {
84 my $chan_id = $chan->getAttribute('Id');
86 progress( "Port: $channel_xmltvid: browsing channel id $chan_id for EventDates" );
88 # find all eventdates for this channel
89 my $eventdates = $chan->findnodes( ".//EventDate" );
90 if( $eventdates->size() == 0 ) {
91 error( "Port: $channel_xmltvid: No EventDates found" ) ;
92 next;
94 progress( "Port: $channel_xmltvid: found " . $eventdates->size() . " EventDates" );
96 # browse through channels
97 foreach my $eventdate ($eventdates->get_nodelist) {
99 my $eventdate_date = $eventdate->getAttribute('Date');
101 progress( "Port: $channel_xmltvid: browsing $eventdate_date for events" );
103 # find all events for this EventDate
104 my $events = $eventdate->findnodes( ".//Event" );
105 if( $events->size() == 0 ) {
106 error( "Port: $channel_xmltvid: No Events found for $eventdate_date" ) ;
107 next;
109 progress( "Port: $channel_xmltvid: found " . $events->size() . " events in $eventdate_date" );
111 # browse through events
112 foreach my $event ($events->get_nodelist) {
114 my $event_id = $event->getAttribute('Id');
116 my $startdate= $event->findvalue( 'StartDate' );
117 my $stopdate= $event->findvalue( 'StopDate' );
118 my $title= $event->findvalue( 'Title' );
119 my $shortdesc= $event->findvalue( 'Shortdescription' );
120 my $longdesc= $event->findvalue( 'Longdescription' );
121 my $dvbccateg= $event->findvalue( 'DVBCategoryName' );
122 my $categlevel= $event->findvalue( 'CategoryNibbleLevel1' );
123 my $rating= $event->findvalue( 'Rating' );
125 my $starttime = create_dt( $startdate );
126 next if not $starttime;
128 my $endtime = create_dt( $stopdate );
129 next if not $endtime;
131 next if not $title;
133 progress( "Port: $channel_xmltvid: $starttime - $title" );
135 my $ce = {
136 channel_id => $channel_id,
137 title => $title,
138 start_time => $starttime->ymd("-") . " " . $starttime->hms(":"),
139 end_time => $endtime->ymd("-") . " " . $endtime->hms(":"),
142 $ce->{description} = norm($shortdesc) if $shortdesc;
143 $ce->{description} = norm($longdesc) if $longdesc;
145 # some characters cleanup
146 $ce->{description} =~ s/\x8a/\n/g;
148 if( $dvbccateg ){
149 my($program_type, $category ) = $ds->LookupCat( 'Port', $dvbccateg );
150 AddCategory( $ce, $program_type, $category );
153 $ce->{rating} = $rating if $rating;
155 $ds->AddProgramme( $ce );
161 return 1;
164 sub create_dt {
165 my ( $dtinfo ) = @_;
167 if( $dtinfo !~ /^\d{4}-\d{2}-\d{2}T\d\d:\d\d:\d\d$/ ){
168 return 0;
171 my( $year, $month, $day, $hour, $minute, $second ) = ( $dtinfo =~ /^(\d{4})-(\d{2})-(\d{2})T(\d\d):(\d\d):(\d\d)$/ );
173 my $dt = DateTime->new( year => $year,
174 month => $month,
175 day => $day,
176 hour => $hour,
177 minute => $minute,
178 second => $second,
179 nanosecond => 0,
180 time_zone => 'Europe/Budapest',
183 #$dt->set_time_zone( "UTC" );
185 return $dt;
190 ### Setup coding system
191 ## Local Variables:
192 ## coding: utf-8
193 ## End: