-
Notifications
You must be signed in to change notification settings - Fork 0
/
datadirect_client.pl
411 lines (326 loc) · 12.3 KB
/
datadirect_client.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/perl
#
# Personal ReplayGuide SQL Converter
# by Lee Thompson <[email protected]>
#
# Requirements:
# DataDirect Service
#
#
#------------------------------------------------------------------------------------
# This file is part of Personal ReplayGuide (C) 2003 by Lee Thompson
#
# Personal ReplayGuide is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Personal ReplayGuide 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Personal ReplayGuide; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#-----------------------------------------------------------------------------------
#
# Zap2It DataDirect Service
#
# Use of DataDirect with Personal ReplayGuide is free, although you will need to
# fill out a quarterly survey for Zap2It (Tribune Media Services).
#
# To sign up for the service, please visit http://datadirect.zap2it.com and click
# to use the DataDirect service for free. It's fairly short and largely
# related to PVRs and television anyway.
#
# The DataDirect Personal ReplayGuide/ReplaySchedule Certificate Code is:
# DGYM-ZKZM-CBUT
#
# All users and developers for Personal ReplayGuide may use this certificate code.
# (Including replaySchedule).
#
# NOTE TO DEVELOPERS: DO NOT use the certificate code within this document for non
# Personal ReplayGuide projects. Instead please write to [email protected] and request
# your own certificate code.
#
#-----------------------------------------------------------------------------------
# NOTE: If you select multiple lineups that have the same feeds (like two SCIFIPs)
# the lowest channel number will appear.
#-----------------------------------------------------------------------------------
use POSIX qw( strftime getcwd );
use English;
use Time::Local;
use SOAP::Lite;
use Unicode::String;
use bytes;
Unicode::String->stringify_as( 'latin1' );
require LWP::Simple;
require LWP::UserAgent;
require HTTP::Request;
require HTTP::Headers;
my $_version = "Personal ReplayGuide|DataDirect Client|1|0|24|Lee Thompson";
#------------------------------------------------------------------------------------
# Determine Current Directroy
#------------------------------------------------------------------------------------
$current_dir = getcwd;
$current_dir .= "/";
#------------------------------------------------------------------------------------
# Get script pathname, unfortunately on Apache on Win32 $0 is not reliable since
# for some reason it gets changed to an 8.3 pathname.
#
# IIS and Apache both provide long filename pathnames in environment variables so
# if it's an 8.3 we look there. If we don't find it, we just tough it out.
#------------------------------------------------------------------------------------
$script_pathname = $0;
if ($0 =~ /\~/) {
if (length($ENV{SCRIPT_FILENAME}) > 0 ) {
$script_pathname = $ENV{SCRIPT_FILENAME};
}
if (length($ENV{PATH_TRANSLATED}) > 0 ) {
$script_pathname = $ENV{PATH_TRANSLATED};
}
}
#------------------------------------------------------------------------------------
# Get the path that the script is in
#------------------------------------------------------------------------------------
(my $path,my $basename) = $script_pathname =~ m|^(.*[/\\])([^/\\]+?)$|;
#------------------------------------------------------------------------------------
# PRG_HOME is an override environment variable
#------------------------------------------------------------------------------------
if (length($ENV{PRG_HOME}) > 0) {
$path = $ENV{PRG_HOME};
}
#------------------------------------------------------------------------------------
# Normalize the path
#------------------------------------------------------------------------------------
$path =~ s/\\/\//g;
#------------------------------------------------------------------------------------
# If the current working directory is not where we need to be, change paths.
#------------------------------------------------------------------------------------
$changed_path = -1;
if (($current_dir ne $path) && ($path ne "")) {
if (chdir $path) {
$changed_path = 1;
}else{
$changed_path = 0;
}
}
#------------------------------------------------------------------------------------
# Load Libraries
#------------------------------------------------------------------------------------
require 'rg_common.pl'; # Load common functions
require 'rg_config.pl'; # Load config functions
#------------------------------------------------------------------------------------
# Set up module identification
#------------------------------------------------------------------------------------
my $module_name = "";
(my $debug_package, my $debug_filename, my $debug_line, my $debug_subroutine, my $debug_hasargs, my $debug_wantarray, my $debug_evaltext, my $debug_isrequire) = caller(0);
if (length($debug_evaltext) > 0) {
$module_name = $debug_evaltext;
}else{
if ($path eq "") {
$basename = $script_pathname;
}
$module_name = $basename;
}
$prg_module{$module_name} = $_version;
#-----------------------------------------------------------------------------------
# Set Defaults and Read Config
#-----------------------------------------------------------------------------------
$retcode = 0;
$htmlmode = 0;
$webservice = "http://docs.tms.tribune.com/tech/tmsdatadirect/zap2it/xtvd.wsdl";
$username = "username"; # Username for DataDirect
$password = "password"; # Password for DataDirect
$days = 3; # Default Days to Download
$verbose = 1; # If this is zero, run silently
$cnf_xmlfile = "./na.xml"; # Output File
$configfile = "datadirect.conf"; # This is optional
$configstatus = getConfig($configfile); # Read Configuration
#-----------------------------------------------------------------------------------
# Set Constants
#-----------------------------------------------------------------------------------
$null = "";
(my $parent,my $desc,my $major,my $minor,my $build,my $authors) = parseModuleData($prg_module{$module_name});
$program_title = $parent;
$program_module = $desc;
$program_author = buildMultiWordList($authors);
$program_version = "$major.$minor";
$program_build = $build;
#----------------------------------------------------------------------------------
# Check to see if this is being run from the console
#----------------------------------------------------------------------------------
$RemoteAddress = $ENV{'REMOTE_ADDR'};
if ($RemoteAddress eq $null) {
$RemoteAddress = "127.0.0.1";
$remotemode = 0;
}else{
$remotemode = 1;
}
#-----------------------------------------------------------------------------------
# Display Header
#-----------------------------------------------------------------------------------
writeDebug("********************************************************");
writeDebug("$program_title\:$program_module v$program_version (Build $program_build)");
writeDebug("Running as $script_pathname with PID $$");
writeDebug("Remote Address: $RemoteAddress");
if ($verbose) {
writeDebug("Console Output: Enabled");
}else{
writeDebug("Console Output: Disabled");
}
identifyLoadedModules();
if ($debug) {
&writeOutput("Debug Messages are ON");
}
#-------------------------------------------------------------------
# Check to see if the IP Address in $RemoteAddress has access to RTV
#-------------------------------------------------------------------
if (hasAccess($RemoteAddress)) {
$remoteaccess = 0; # Disabled
}else{
$remoteaccess = 0;
}
if (($remotemode) && (!$remoteaccess)) {
&InitializeDisplay;
abend("$program_module runs in console mode only");
}else{
if ($remotemode) {
&InitializeDisplay;
}
}
#-------------------------------------------------------------------
$now = strftime( "%Y-%m-%d", localtime ) . " " . strftime( "%H:%M:%S", localtime );
$started = time;
$starttime_seconds = as_epoch_seconds(strftime( "%Y%m%d%", gmtime ) . "000000" );
$endtime_seconds = $starttime_seconds + ((($days * 24) * 60) * 60);
$starttime_ts = as_time_string($starttime_seconds);
$endtime_ts = as_time_string($endtime_seconds);
$starttime = substr($starttime_ts,0,4) . "-" . substr($starttime_ts,4,2) . "-" . substr($starttime_ts,6,2) . "T" . substr($starttime_ts,8,2) . ":" . substr($starttime_ts,10,2) . ":" . substr($starttime_ts,12,2) . "Z";
$endtime = substr($endtime_ts,0,4) . "-" . substr($endtime_ts,4,2) . "-" . substr($endtime_ts,6,2) . "T" . substr($endtime_ts,8,2) . ":" . substr($endtime_ts,10,2) . ":" . substr($endtime_ts,12,2) . "Z";
writeDebug("Job started at $now");
writeDebug("Requesting up to $days days of data from DataDirect");
if ($debug) {
writeDebug("DEBUG: Range is $starttime - $endtime");
}
if ($debug) {
writeDebug("DEBUG: Account: $username Password: $password");
}
writeDebug("Building SOAP Object for $webservice");
$soapenv = SOAP::Lite
-> service($webservice)
-> outputxml('true')
-> on_fault( sub {
my($soap,$res)=@_;
writeDebug("FATAL ERROR - SOAP call failed: " .
(ref $res ? $res->faultstring : $soap->transport->status));
exit 1;
} );
writeDebug("SOAP Object Completed");
writeDebug("Starting Data Download");
$xtvddoc = $soapenv
-> proxy('http://notused/', options => {compress_threshold => 10000}, timeout => 420)
-> download("<startTime>$starttime</startTime><endTime>$endtime</endTime>");
writeDebug("Data Download Complete");
$accessdenied = 0;
$message = "";
writeDebug("Converting to UTF8");
$xtvddoc = Unicode::String::utf8( $xtvddoc );
writeDebug("Conversion Complete");
writeDebug("Checking for Errors");
if ($xtvddoc =~ "<HTML><HEAD><TITLE>401") {
writeDebug("FATAL ERROR - DataDirect->Access Denied");
$accessdenied = 1;
$xtvddoc = "";
}
if ($xtvddoc =~ "<HTML><HEAD><TITLE>500") {
writeDebug("FATAL ERROR - DataDirect->Server Error");
$message = $xtvddoc;
$message =~ s/.*<PRE>([^<]*)\W.*/$1/;
if ($message eq $xtvddoc) {
$message = "";
}
$xtvddoc = "";
}
if ($xtvddoc =~ "<HTML><HEAD><TITLE>") {
writeDebug("FATAL ERROR - DataDirect: Unknown Error");
$xtvddoc = "";
}
writeDebug("Check Complete");
writeDebug("Looking for Data Block");
if ($xtvddoc =~ m#(<xtvd\s.*</xtvd>)#s) {
writeDebug("Found Data Block");
writeDebug("Writing Data");
if (open(HOUTPUT,">$cnf_xmlfile")) {
binmode HOUTPUT;
print HOUTPUT $xtvddoc;
close HOUTPUT;
writeDebug("Wrote data successfully to \"$cnf_xmlfile\".");
}else{
writeDebug("Attempt to write data to \"$cnf_xmlfile\" failed.");
}
$exitflag = 0;
if(open(HINPUT,"<$cnf_xmlfile")) {
do {
$line = readline HINPUT;
chomp($line);
if ($line =~ /message/) {
$message = $line;
$message =~ s/.*<message>([^<]*)\W.*/$1/;
if ($message eq $line) {
$message = "";
}
}
if ($line =~ /faultstring/) {
$message = $line;
$message =~ s/.*<faultstring>([^<]*)\W.*/$1/;
if ($message eq $line) {
$message = "";
}
$exitflag = 1;
}
if ($line =~ /<\/messages>/) {
$exitflag = 1;
}
if ($line =~ /<messages\/>/) {
$message = "";
$exitflag = 1;
}
} until ($exitflag > 0);
close HINPUT;
if (length($message) > 0) {
writeDebug("From DataDirect: $message");
$message = "";
}else{
writeDebug("No alerts or messages from DataDirect");
$retcode = 1;
}
}
}else{
writeDebug("No Data Block");
if (!$accessdenied) {
if (length($message) > 0) {
writeDebug("$message");
}else{
writeDebug("Error: $xtvddoc");
}
}else{
writeDebug("Access was denied");
}
}
$finished = time;
$runtime = $finished - $started;
$now = strftime( "%Y-%m-%d", localtime ) . " " . strftime( "%H:%M:%S", localtime );
writeDebug("Job finished at $now ($runtime seconds)");
&ShowHTTPFooter;
writeDebug("********************************************************");
exit($retcode);
#----------------------------------------------------
sub SOAP::Transport::HTTP::Client::get_basic_credentials {
#
# Callback for HTTP Login Info
#
# ------------------------------------------------------------------------------
return "$username" => "$password";
}