| 1 | #!/usr/bin/env perl |
|---|
| 2 | # |
|---|
| 3 | # simple grabber for Al Jazeera and CCTV data from http://zaxmltv.flash.za.org |
|---|
| 4 | # |
|---|
| 5 | |
|---|
| 6 | use warnings; |
|---|
| 7 | |
|---|
| 8 | use XMLTV; |
|---|
| 9 | use Shepherd::Common; |
|---|
| 10 | use POSIX qw(strftime mktime); |
|---|
| 11 | |
|---|
| 12 | # |
|---|
| 13 | # global variables and settings |
|---|
| 14 | # |
|---|
| 15 | # supported Shepherd channels and the corresponding number @ zaxmltv |
|---|
| 16 | |
|---|
| 17 | my @supported = ("AlJazeera","CCTVNews"); |
|---|
| 18 | my %channels = ( |
|---|
| 19 | 'AlJazeera' => "406", |
|---|
| 20 | 'CCTVNews' =>"448" |
|---|
| 21 | ); |
|---|
| 22 | |
|---|
| 23 | $| = 1; |
|---|
| 24 | my $writer; |
|---|
| 25 | my $baseurl = "http://zaxmltv.flash.za.org"; |
|---|
| 26 | |
|---|
| 27 | # |
|---|
| 28 | # go go go! |
|---|
| 29 | # |
|---|
| 30 | |
|---|
| 31 | my %stats; |
|---|
| 32 | my $o; |
|---|
| 33 | |
|---|
| 34 | Shepherd::Common::program_begin(\$o, "zaxmltv grabber", "0.03", \%stats); |
|---|
| 35 | &start_writing_xmltv(); |
|---|
| 36 | |
|---|
| 37 | my ($channels, $opt_channels, $gaps) =(); |
|---|
| 38 | my $channel_xmlid; |
|---|
| 39 | |
|---|
| 40 | ($channels, $opt_channels, $gaps) = Shepherd::Common::read_channels($o, @supported); |
|---|
| 41 | |
|---|
| 42 | # first write the channel names and id's |
|---|
| 43 | while (my ($name, $num) = each (%channels)) { |
|---|
| 44 | if ($channels->{$name} || $opt_channels->{$name}) { |
|---|
| 45 | $channel_xmlid = $channels->{$name} || $opt_channels->{$name}; |
|---|
| 46 | $writer->write_channel( {'display-name' => [[ "$name", $o->{lang} ]], 'id' => $channel_xmlid } ); |
|---|
| 47 | }else{ |
|---|
| 48 | Shepherd::Common::log("ignoring unwanted channel $name"); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | #now the actual program data for each channel |
|---|
| 53 | while (my ($name, $num) = each (%channels)) { |
|---|
| 54 | if ($channels->{$name} || $opt_channels->{$name}) { |
|---|
| 55 | $channel_xmlid = $channels->{$name} || $opt_channels->{$name}; |
|---|
| 56 | Shepherd::Common::log("grabbing data for $name - $channel_xmlid"); |
|---|
| 57 | &get_data($num); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | $writer->end(); |
|---|
| 62 | |
|---|
| 63 | Shepherd::Common::program_end($o, %stats); |
|---|
| 64 | exit(0); |
|---|
| 65 | |
|---|
| 66 | ############################################################################## |
|---|
| 67 | |
|---|
| 68 | sub start_writing_xmltv |
|---|
| 69 | { |
|---|
| 70 | my %writer_args = ( encoding => 'ISO-8859-1' ); |
|---|
| 71 | if ($o->{outputfile}) { |
|---|
| 72 | my $fh = new IO::File(">$o->{outputfile}") || die "can't open $o->{outputfile}: $!"; |
|---|
| 73 | $writer_args{OUTPUT} = $fh; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | $writer = new XMLTV::Writer(%writer_args); |
|---|
| 77 | $writer->start ( { 'source-info-name' => "$baseurl",'generator-info-name' => "$o->{program_name} v$o->{version_number}"} ); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | ############################################################################## |
|---|
| 81 | |
|---|
| 82 | sub get_data |
|---|
| 83 | { |
|---|
| 84 | my $num = $_[0]; |
|---|
| 85 | my $data = &Shepherd::Common::get_url(url => $baseurl."/schedule/zaxmltv_get.php", postvars => "chanlist[]=$num"); |
|---|
| 86 | if (!$data) { |
|---|
| 87 | Shepherd::Common::log("Failed to get XML from '$baseurl'"); |
|---|
| 88 | $stats{failed_html_get}++; |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | my $listing = XMLTV::parse($data); |
|---|
| 92 | # strip the cruft from the source xml, we only want the programs. |
|---|
| 93 | my ($encoding, $credits, $ch, $progs) = @$listing; |
|---|
| 94 | foreach (@$progs) { |
|---|
| 95 | $_->{channel} = $channel_xmlid; |
|---|
| 96 | $writer->write_programme($_); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | ############################################################################## |
|---|