-
Notifications
You must be signed in to change notification settings - Fork 0
/
NzSatEpg.pl
264 lines (216 loc) · 9.32 KB
/
NzSatEpg.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Copyright 2013, mm1352000
# This file is part of NzSatEpg.
# NzSatEpg is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
# NzSatEpg is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero Public License for more details.
# You should have received a copy of the GNU Affero Public License along with
# NzSatEpg. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
package XmlTv::NzSatEpg;
use DateTime;
use HTML::Entities qw(decode_entities);
use LWP::UserAgent;
use Channels::AmazingDiscoveries;
use Channels::ArirangWorld;
use Channels::Daystar;
use Channels::Ewtn;
use Channels::GodTv;
use Channels::HopeChannel;
use Channels::Inspiration;
use Channels::NhkWorld;
use Channels::Ovation;
use Channels::PressTv;
use Channels::RussiaToday;
use Channels::Tbn;
use Channels::ThreeAbn;
#==================================================================================================
# Important Variables
#==================================================================================================
# This is the full path to the output file.
$::outputFile = 'C:\Program Files\XMLTV\tvguide.xml';
# This is the directory that is used to store the files that are downloaded by this script.
$::tempPath = 'C:\Program Files\XMLTV';
# This is the full path to the debug file. Note that debug is only printed to this file if the
# environment variable 'DEBUG_NZSATEPG' has been set.
$::debugFile = '&STDOUT';
# This is the timeout period (in seconds) used by the global web user agent.
$::webTimeout = 30;
# This is the common time zone which all schedule data times will be converted to.
$::targetTimeZone = 'Pacific/Auckland';
# This is the number of days of data, starting from today in the target time zone, which will be
# gathered (wherever possible).
$::daysToGrab = 14;
%::monthMap = (
'January' => 1,
'Jan' => 1,
'February' => 2,
'Feb' => 2,
'March' => 3,
'Mar' => 3,
'April' => 4,
'Apr' => 4,
'May' => 5,
'May' => 5,
'June' => 6,
'Jun' => 6,
'July' => 7,
'Jul' => 7,
'August' => 8,
'Aug' => 8,
'September' => 9,
'Sep' => 9,
'October' => 10,
'Oct' => 10,
'November' => 11,
'Nov' => 11,
'December' => 12,
'Dec' => 12
);
#===============================================================================================
# Main Section
#===============================================================================================
# Open all the streams that this script uses.
$| = 1;
open(OUTPUT, ">$::outputFile") || die __LINE__ . ": Failed to open the output stream for writing.\n";
if (!exists $ENV{'DEBUG_NZSATEPG'}) {
$::debugFile = ($^O =~ m/MSWin/) ? 'NUL' : '/dev/null';
}
open($::dbg, ">$::debugFile") || die __LINE__ . ": Failed to open the debug stream for writing.\n";
$::dbg->autoflush(1);
# Pretend to be a Firefox browser. The 3ABN website rejects our requests if we don't set the agent.
$::wua = LWP::UserAgent->new(timeout => $::webTimeout);
$::wua->agent("Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0");
$::startDate = DateTime->today()->set_time_zone($::targetTimeZone)->truncate(to => 'day');
$::endDate = $::startDate->clone()->add(days => $::daysToGrab);
print $::dbg "Starting data grab for $::daysToGrab days ($::startDate to $::endDate) in time zone $::targetTimeZone...\n";
print OUTPUT "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<!DOCTYPE tv SYSTEM \"dtds/xmltv.dtd\">\n\n<tv>\n";
print "NzSatEpg v1.0 alpha, AGPL licence, copyright 2013 mm1352000\n";
print "Start grabbing...\n";
printChannelData(XmlTv::NzSatEpg::Channels::AmazingDiscoveries::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::ArirangWorld::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::Daystar::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::Ewtn::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::GodTv::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::HopeChannel::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::Inspiration::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::NhkWorld::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::Ovation::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::PressTv::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::RussiaToday::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::Tbn::getChannelData());
printChannelData(XmlTv::NzSatEpg::Channels::ThreeAbn::getChannelData());
print "Grabbing completed successfully!\n";
print OUTPUT "</tv>\n";
close(OUTPUT);
close($::dbg);
sub toHoursAndMinutes {
my $rawTime = shift;
my $hours;
$rawTime =~ m/^\s*(\d+):(\d+)\s*([AaPp][Mm])?\s*$/;
$hours = ($1 == 12) ? 0 : $1;
$hours += 12 if (uc($3) eq 'PM');
return ($hours, $2);
}
# This subroutine is used to tidy up the titles and descriptions that get put into the output file.
sub tidyText {
my $text = shift || '';
# Deal with links.
$text =~ s/<a.*?href="([^"]*)".*?>\s*(.*?)\s*<\/a>/$2 ($1)/gi;
# Replace newline, HTML break and multiple spaces with a single space.
$text =~ s/(\r)?\n(\r)?/ /gs;
$text =~ s/<br(\s*\/)?>(<br(\s*\/)?>)?/ /gi;
$text =~ s/\s\s+/ /g;
# Convert HTML entities (eg. & ´) into characters.
$text = decode_entities($text);
# Some entities should be left alone.
$text =~ s/&/&/gi;
$text =~ s/</</gi;
$text =~ s/>/>/gi;
$text =~ s/"/"/gi;
# Encode unicode characters correctly.
while ($text =~ m/(\P{IsASCII})/) {
my $code = sprintf("%04x", ord($1));
$text =~ s/\x{$code}/&#x$code;/g;
}
# Remove leading and trailing whitespace.
$text =~ s/^\s*(.*?)\s*$/$1/s;
return $text;
}
sub printChannelData {
my ($data) = @_;
return if (!defined $data);
foreach my $channel (@$data) {
printChannelEntry($channel);
my $id = $channel->{'id'};
my $schedule = $channel->{'schedule'};
foreach my $prog (sort keys %$schedule) {
printProgrammeEntry($id, $schedule->{$prog});
}
}
}
sub printChannelEntry {
my ($channel) = @_;
print OUTPUT "<channel id=\"" . $channel->{'id'} . "\">\n";
print OUTPUT "\t<display-name>" . $channel->{'name'} . "</display-name>\n";
if (exists $channel->{'url'}) {
foreach my $url (@{$channel->{'url'}}) {
print OUTPUT "\t<url>$url</url>\n"
}
}
print OUTPUT "</channel>\n";
}
sub printProgrammeEntry {
my ($channelId, $programme) = @_;
print OUTPUT "<programme channel=\"$channelId\" start=\"" . $programme->{'start'} . '00" stop="' . $programme->{'end'} . "00\">\n";
print OUTPUT "\t<title>" . tidyText($programme->{'title'}) . "</title>\n";
if (exists $programme->{'episode title'}) {
print OUTPUT "\t<sub-title>" . tidyText($programme->{'episode title'}) . "</sub-title>\n";
}
if (exists $programme->{'episode number'}) {
foreach my $episodeNum (@{$programme->{'episode number'}}) {
my ($system, $num) = split(/:/, $episodeNum);
print OUTPUT "\t<episode-num system=\"$system\">$num</episode-num>\n";
}
}
if (exists $programme->{'episode description'}) {
print OUTPUT "\t<desc>" . tidyText($programme->{'episode description'}) . "</desc>\n";
}
if (exists $programme->{'description'} && $programme->{'description'} ne '') {
print OUTPUT "\t<desc>" . tidyText($programme->{'description'}) . "</desc>\n";
}
if (exists $programme->{'url'}) {
foreach my $url (@{$programme->{'url'}}) {
print OUTPUT "\t<url>" . tidyText($url) . "</url>\n"
}
}
if (exists $programme->{'host'} || exists $programme->{'producer'}) {
print OUTPUT "\t<credits>\n";
foreach my $host (@{$programme->{'host'}}) {
print OUTPUT "\t\t<presenter>" . tidyText($host) . "</presenter>\n";
}
foreach my $producer (@{$programme->{'producer'}}) {
print OUTPUT "\t\t<producer>" . tidyText($producer) . "</producer>\n";
}
print OUTPUT "\t</credits>\n";
}
if (exists $programme->{'category'}) {
foreach my $category (@{$programme->{'category'}}) {
print OUTPUT "\t<category>$category</category>\n";
}
}
if (exists $programme->{'premiere'}) {
print OUTPUT "\t<premiere>" . $programme->{'premiere'} . "</premiere>\n";
}
if (exists $programme->{'rating'}) {
foreach my $rating (@{$programme->{'rating'}}) {
my ($system, $value) = split(/:/, $rating);
print OUTPUT "\t<rating system=\"$system\">$value</rating>\n";
}
}
print OUTPUT "</programme>\n";
}