blib/lib/Net/Download/XMLScripted/XMLScripted.pm

filter: Statementstmt 18.1% 24 / 132 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 37.5% 9 / 24 Podpod n/a totaltotal 19.7% 33 / 167 CC 9 SCAR 18.1
CC
9
Cov
17%
download
20.0
toString
12.0
toStringHTML
12.0
CRAP 6.1
↑ Summary
1 #!/usr/bin/perl
2 # XMLScripted.pm
3 # Author: Singh T. Junior
4 # E-Mail: singhtjunior@gmail.com
5 # Date: 21 May 2006
6 # Chicago, IL
7  
8  
9 package Net::Download::XMLScripted::XMLScripted;
10  
11 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI::Heuristic;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use 5.008006;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 require Exporter;
21  
22 our $VERSION=0.10;
23  
24 our @ISA = qw(Exporter AutoLoader);
25  
26 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Items to export into callers namespace by default. Note: do not export
29 # names by default without a very good reason. Use EXPORT_OK instead.
30 # Do not simply export all your public functions/methods/constants.
31  
32 # This allows declaration use Net::Download::XMLScripted::XMLScripted ':all';
33 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
34 # will save memory.
35 our %EXPORT_TAGS = ( 'all' => [ qw(
36 ) ] );
37  
38 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
39  
40 our @EXPORT = qw(
41 );
42  
43 package Net::Download::XMLScripted::XMLScriptedElement;
44 ############################################################
45 ## methods to access per-object data ##
46 ############################################################
47 # initialize()
48 # Private Method
49 my $initialize_XMLScriptedElement = sub
50 {
51 my $self = shift;
52 $self->{urlName} = "";
53 $self->{fileName} = "";
54 $self->{dirName} = "";
55 $self->{fullFileName} = "";
56 $self->{statusLog} = ();
57 $self->{errorLog} = ();
58 };
59  
60 ############################################################
61 ## the object constructor ##
62 ############################################################
63 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
64 0 my $invocant = shift;
65 0 ▶ my $class = ref($invocant) || $invocant; # Object or class name
Condition0%0/3
ref $invocant || $invocant
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table0%0/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
66 0 my $self = {@_};
67 0 bless ($self, $class);
68 0 $self->$initialize_XMLScriptedElement();
69 0 return $self;
70 }
71  
72 ############################################################
73 ## methods to access per-object data ##
74 ############################################################
75  
76 # setUrlName()
77 0 ▶sub setUrlName {
Subroutine setUrlName: not called CC=1 SCAR=6.9
78 0 my $self = shift;
79 0 $self->{urlName} = shift;
80 }
81  
82 # getUrlName()
83 sub getUrlName()
84 0 ▶{
Subroutine getUrlName: not called CC=1 SCAR=6.9
85 0 my $self = shift;
86 0 return $self->{urlName};
87 }
88  
89 # setFileName()
90 0 ▶sub setFileName() {
Subroutine setFileName: not called CC=1 SCAR=6.9
91 0 my $self = shift;
92 0 $self->{fileName} = shift;
93 }
94  
95 # getFileName()
96 0 ▶sub getFileName() {
Subroutine getFileName: not called CC=1 SCAR=6.9
97 0 my $self = shift;
98 0 return $self->{fileName};
99 }
100  
101 # setDirName()
102 0 ▶sub setDirName() {
Subroutine setDirName: not called CC=1 SCAR=6.9
103 0 my $self = shift;
104 0 $self->{dirName} = shift;
105 }
106  
107 # getDirName()
108 0 ▶sub getDirName() {
Subroutine getDirName: not called CC=1 SCAR=6.9
109 0 my $self = shift;
110 0 return $self->{dirName};
111 }
112  
113 # setFullFileName()
114 sub setFullFileName()
115 0 ▶{
Subroutine setFullFileName: not called CC=1 SCAR=6.9
116 0 my $self = shift;
117 0 $self->{fullFileName} = shift;
118 }
119  
120 # getFullFileName()
121 sub getFullFileName()
122 0 ▶{
Subroutine getFullFileName: not called CC=1 SCAR=6.9
123 0 my $self = shift;
124 0 return $self->{fullFileName};
125 }
126  
127 # download()
128 sub download()
129 0 ▶{
Subroutine download: not called CC=4 SCAR=30.0
130 0 my $self = shift;
131 0 my $fullFileName = $self->getFullFileName();
132 0 my $urlName = $self->getUrlName();
133 #
134 0 print ">>>Downloading ".$urlName." to ".$fullFileName."\n";
135 #
136 0 ▶ if ( -e $fullFileName )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fullFileName) { } 0 0 0
137 {
138 0 $self->addStatusLog("File $fullFileName exists! Not going to be downloaded!");
139 0 print "...Not downloading because ".$fullFileName." exists!\n";
140 0 return;
141 }
142 else
143 {
144 0 my $cleanURL = URI::Heuristic::uf_urlstr($urlName);
145 0 $| = 1; # to flush next line
146 0 my $browser = LWP::UserAgent->new();
147 0 $browser->agent("Schmozilla/v9.14 Platinum"); # give it time, it'll get there
148 0 my $req = HTTP::Request->new(GET => $cleanURL);
149 0 $req->referer("http://wizard.yellowbrick.oz"); # perplex the log analysers
150
151 0 my $response = $browser->request($req);
152 0 ▶ if ( $response->is_success )
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
153 {
154 0 ▶ unless (open(FILEOUT,">".$fullFileName)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILEOUT, '>' . $fullFileName) 0 0 0
155 0 my @errorLog = @{$self->{errorLog}};
156 0 $self->addErrorLog("Could not open file $fullFileName. $!");
157 0 print "...Failure! Cound not open file ".$fullFileName."\n";
158 0 return;
159 }
160 0 binmode(FILEOUT);
161 0 my $content = $response->content();
162 0 print FILEOUT $content;
163 0 close FILEOUT;
164 0 my $bytes = length $content;
165 0 $self->addStatusLog("Success: File $fullFileName downloaded successfully! ".$bytes." Bytes\n");
166 0 print "...Success!\n";
167 0 return;
168 }
169 else
170 {
171 0 $self->addErrorLog("URL $urlName not found!");
172 0 $self->addErrorLog("".$response->status_line);
173 0 print "...Failure! ".$response->status_line."\n";
174 0 return;
175 }
176 }
177 }
178  
179 ##
180 sub addStatusLog()
181 0 ▶{
Subroutine addStatusLog: not called CC=1 SCAR=6.9
182 0 my $self = shift;
183 0 push(@{$self->{statusLog}},shift);
184 }
185  
186 ##
187 sub addErrorLog()
188 0 ▶{
Subroutine addErrorLog: not called CC=1 SCAR=6.9
189 0 my $self = shift;
190 0 push(@{$self->{errorLog}},shift);
191 }
192  
193  
194 ##
195 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
196 0 my $model = shift;
197 0 my $self = $model->new(%$model, @_);
198 0 return $self; # Previously blessed by ->new
199 }
200  
201 ###
202 0 ▶sub toString{
Subroutine toString: not called CC=3 SCAR=24.8
203 0 my $self = shift;
204 0 my $str = sprintf("URL: %s, Dir:%s, File: %s \nFullFileName: %s", $self->getUrlName(), $self->getDirName(), $self->getFileName(),$self->getFullFileName());
205  
206 # Status Log
207 0 my @statusLog = @{$self->{statusLog}};
208 0 $str = $str."\nStatus Log:".($#statusLog+1)."\n";
209 0 my $count = 1;
210 0 foreach my $statusLogElem ( @statusLog)
211 {
212 0 $str = $str."\t[$count] $statusLogElem\n";
213 0 $count = $count + 1;
214 }
215 # Error Log
216 0 my @errorLog = @{$self->{errorLog}};
217 0 $str = $str."Error Log:".($#errorLog+1)."\n";
218 0 $count = 1;
219 0 foreach my $errorLogElem (@errorLog)
220 {
221 0 $str = $str."\t[$count] $errorLogElem \n";
222 0 $count = $count + 1;
223 }
224
225 0 return $str;
226 }
227  
228 ###
229 0 ▶sub toStringHTML{
Subroutine toStringHTML: not called CC=3 SCAR=24.8
230 0 my $self = shift;
231 0 my $str = "";
232 0 $str = $str."<UL>\n";
233 0 $str = $str."<LI><B>URL: </B>".$self->getUrlName()." </LI>\n";
234 0 $str = $str."<LI><B>DIR: </B>".$self->getDirName()." </LI>\n";
235 0 $str = $str."<LI><B>FILE: </B>".$self->getFileName()." </LI>\n";
236 ##
237 0 my $statusLogRef = \@{$self->{statusLog}};
238 0 my @statusLog = @$statusLogRef;
239 0 $str = $str."<LI><B>Download Element Status Log [".($#statusLog + 1)."] Messages</LI></B>\n";
240 0 $str = $str."<OL>\n";
241 0 foreach my $statusLogElem ( @statusLog)
242 {
243 0 $str = $str."<LI style=\"color:green\"> $statusLogElem\n";
244 }
245 0 $str = $str."</OL>\n";
246 ##
247 0 my $errorLogRef = \@{$self->{errorLog}};
248 0 my @errorLog = @$errorLogRef;
249 0 $str = $str."<LI><B>Download Element Error Log [".($#errorLog + 1) ."] Messages</LI></B>\n";
250 0 $str = $str."<OL>\n";
251 0 foreach my $errorLogElem ( @errorLog)
252 {
253 0 $str = $str."<LI style=\"color:red\"> $errorLogElem\n";
254 }
255 0 $str = $str."</OL>\n";
256 0 $str = $str."</UL>\n";
257 0 return $str;
258 }
259  
260  
261 ###############################################################################
262 ###############################################################################
263 ###############################################################################
264 ###############################################################################
265 ###############################################################################
266 ###############################################################################
267 # Below is stub documentation for your module. You'd better edit it!
268  
269 =head1 NAME
270  
271 Net::Download::XMLScripted::XMLScripted - Perl XML scripted download program
272  
273 =head1 SYNOPSIS
274  
275 use Net::Download::XMLScripted::XMLScripted;
276
277 To Run from Command Line:
278
279 (1) perl XMLScripted.pm -inXMLFileName xmlFileName [-verbose]
280  
281 (2) perl XMLScripted.pm -inXMLFileName xmlFileName -beginDate YYYY-MM-DD [-endDate YYYY-MM-DD]
282  
283 (3) perl XMLScripted.pm -generateSampleXMLFile sampleXMLFileName
284  
285 (4) perl XMLScripted.pm -showTranslationRules
286  
287 (5) perl XMLScripted.pm -version
288  
289 =head1 DESCRIPTION
290  
291 This is a daily download program. The input is an XML file that has information
292 about URLs that need to be downloaded. It uses specific translation rules to
293 generate URL names that contain dates. It creates directories specified in
294 the input XML file and downloads the URLs. This module is ideal for cron jobs.
295  
296  
297 =head2 EXPORT
298  
299 None by default.
300  
301  
302  
303 =head1 SEE ALSO
304  
305 perl(1) LWP::UserAgent HTTP::Request HTTP::Response URI::Heuristic XML::Parser
306  
307  
308 =head1 AUTHOR
309  
310 Singh T. Junior, E<lt>tsingh@gmail.com<gt>
311  
312 =head1 COPYRIGHT AND LICENSE
313  
314 Copyright (C) 2006 by Singh T. Junior
315  
316 All Rights Reserved.
317  
318 This library is free software; you can redistribute it and/or modify
319 it under the same terms as Perl itself, either Perl version 5.8.6 or,
320 at your option, any later version of Perl 5 you may have available.
321  
322  
323 =cut
324  
325  
326  
327 package Net::Download::XMLScripted::XMLScripted;
328  
329 0 ▶use Switch;
Subroutine BEGIN: called CC=1 SCAR=2.6
330 use XML::Parser;
331 #use IO;
332 use Time::Local;
333 use 5.008006;
334  
335  
336 use strict;
337  
338  
339 use vars qw($x $y $tree %translationHash);
340  
341 ## Get Translated StringCore
342 ## Private method
343 ##
344 my $getTranslatedStringCore = sub
345 {
346 my $self = shift;
347 my $inputString = shift;
348 my $myHash = shift;
349 my %myTranslationHash = %{$myHash};
350 my $outputString;
351 my $inputPattern;
352
353 #
354 $outputString = sprintf("%s",$inputString);
355 while ( my ($key, $value) = each(%myTranslationHash) ) {
356 $inputPattern = "\\[".$key."\\]";
357 $outputString =~ s/$inputPattern/$value/g;
358 }
359 return $outputString;
360 };
361  
362 ## addStatusLog
363 ## Private Method
364 ##
365 my $addStatusLog = sub
366 {
367 my $self = shift;
368 push(@{$self->{statusLog}},shift);
369 };
370  
371 ## addErrorLog
372 ## Private Method
373 ##
374 my $addErrorLog = sub
375 {
376 my $self = shift;
377 push(@{$self->{errorLog}},shift);
378 };
379  
380  
381 ## Create TranslationHash Core
382 ## Private method
383 ##
384 my $createTranslationHashCore = sub
385 {
386 my $self = shift;
387 my $TIME = shift;
388 my @monthNames = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
389 my @dayNames = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
390 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
391 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($TIME);
392 $mon += 1;
393 $year += 1900;
394 $yday += 1;
395
396 my %translationHash = ();
397
398 # Year, Month, Day
399 $translationHash{'YYYY'} = $year;
400 $translationHash{'YY'} = $year-2000;
401 $translationHash{'MM'} = $mon;
402 $translationHash{'DD'} = $mday;
403 #
404 $translationHash{'YY_2'} = sprintf("%02d",$year-2000);
405 $translationHash{'MM_2'} = sprintf("%02d",$mon);
406 $translationHash{'DD_2'} = sprintf("%02d",$mday);
407 #
408 # Week day
409 if ( $wday == 0 )
410 {
411 $wday = 7;
412 }
413 $translationHash{'WD'} = $wday;
414 #
415 $translationHash{'DayOfWeek'} = $dayNames[$wday-1];
416
417 $translationHash{'DAYOFWEEK'} = uc $translationHash{'DayOfWeek'};
418 $translationHash{'dayofweek'} = lc $translationHash{'DayOfWeek'};
419 $translationHash{'DayOfWeek_Abr'} = substr $translationHash{'DayOfWeek'}, 0, 3;
420 $translationHash{'DAYOFWEEK_ABR'} = uc $translationHash{'DayOfWeek_Abr'};
421 $translationHash{'dayofweek_abr'} = lc $translationHash{'DayOfWeek_Abr'};
422 # Month
423 $translationHash{'MonthOfYear'} = $monthNames[$mon-1];
424
425 $translationHash{'MONTHOFYEAR'} = uc $translationHash{'MonthOfYear'};
426 $translationHash{'monthofyear'} = lc $translationHash{'MonthOfYear'};
427 $translationHash{'MonthOfYear_Abr'} = substr $translationHash{'MonthOfYear'}, 0, 3;
428 $translationHash{'MONTHOFYEAR_ABR'} = uc $translationHash{'MonthOfYear_Abr'};
429 $translationHash{'monthofyear_abr'} = lc $translationHash{'MonthOfYear_Abr'};
430 ##
431 $translationHash{'DayOfYear'} = $yday;
432 $translationHash{'DayOfYear_2'} = sprintf("%02d",$yday);
433 $translationHash{'DayOfYear_3'} = sprintf("%03d",$yday);
434 ##
435 $translationHash{'HH24'} = $hour;
436 $translationHash{'HH24_2'} = sprintf("%0d",$hour);
437 if ($hour < 12 )
438 {
439 $translationHash{'HH12'} = $hour;
440 $translationHash{'HH12_2'} = sprintf("%02d",$hour);
441 $translationHash{'AMPM'} = "AM";
442 $translationHash{'ampm'} = "am";
443 }
444 else
445 {
446 $translationHash{'HH12'} = $hour - 12;
447 $translationHash{'HH12_2'} = sprintf("%02d",$hour-12);
448 $translationHash{'AMPM'} = "PM";
449 $translationHash{'ampm'} = "pm";
450 }
451 ##
452 $translationHash{'mm'} = $min;
453 $translationHash{'mm_2'} = sprintf("%02d",$min);
454 ##
455 $translationHash{'ss'} = $sec;
456 $translationHash{'ss_2'} = sprintf("%02d",$sec);
457
458 ##
459 return %translationHash;
460 };
461  
462 ## makeElementDirectories
463 ## Private method
464 ##
465 my $makeElementDirectories = sub
466 {
467 my $self = shift;
468 my $fullDirName = $self->getFullDirectoryName();
469 unless ( -d $fullDirName )
470 {
471 return;
472 }
473 #
474 my @elementArray = @{$self->{elementArray}};
475 foreach my $elem ( @elementArray )
476 {
477 my $fullElementDirName = $self->getFullFileName($fullDirName, $elem->getDirName());
478 if ( -d $fullElementDirName )
479 {
480 $elem->addStatusLog("Directory ".$fullElementDirName." Exists!");
481 }
482 else
483 {
484 $elem->addStatusLog("Directory ".$elem->getDirName()." Does Not Exist!");
485 $elem->addStatusLog("Trying to create directory: ".$elem->getDirName());
486 unless ( mkdir($fullElementDirName, 0755) )
487 {
488 $elem->addStatusLog("Could not create directory: ".$elem->getDirName());
489 }
490 if ( -d $fullElementDirName )
491 {
492 $elem->addStatusLog("Created the directory successfully: ".$fullElementDirName);
493 }
494 }
495 }
496 };
497  
498 ## MakeDirectories
499 ## Private Method
500 ##
501 my $makeDirectories = sub
502 {
503 my $self = shift;
504 # Make sure the download path exists
505 if ( -d $self->getDownLoadPathName() )
506 {
507 $self->$addStatusLog("Download Path Name ".$self->getDownLoadPathName()." Exists!");
508 }
509 else
510 {
511 $self->$addErrorLog("Error: Download Path Name ".$self->getDownLoadPathName()." Does Not Exist!");
512 $self->$addStatusLog("Trying to create directory: ".$self->getDownLoadPathName());
513 #
514 unless ( mkdir($self->getDownLoadPathName(), 0755) )
515 {
516 $self->$addErrorLog("Error: Could not create directory: ".$self->getDownLoadPathName());
517 return;
518 }
519 #
520 $self->$addStatusLog("Created the directory successfully: ".$self->getDownLoadPathName());
521 }
522 #
523 my $fullDirName;
524 if ( -d $self->getDownLoadPathName() )
525 {
526 $fullDirName = $self->getFullDirectoryName();
527 if ( -d $fullDirName )
528 {
529 $self->$addStatusLog("Download Path to ".$self->getDownLoadDirName()." Exists!");
530 }
531 else
532 {
533 $self->$addErrorLog("Error: Download Dir Name ".$fullDirName." Does Not Exist!");
534 $self->$addStatusLog("Trying to create directory: ".$fullDirName);
535 unless ( mkdir ($fullDirName, 0755) )
536 {
537 $self->$addErrorLog("Error: Could not create directory: ".$fullDirName);
538 return;
539 }
540 $self->$addStatusLog("Created the directory successfully ".$self->getDownLoadDirName());
541 }
542 }
543 return;
544 };
545  
546 ############################################################
547 ## methods to access per-object data ##
548 ############################################################
549 # initialize()
550 # Private method
551 my $initialize_XMLScripted = sub {
552 my $self = shift;
553 $self->setDownLoadPathName("");
554 $self->setDownLoadDirName("");
555 $self->setStatusReportFileName("");
556 %translationHash = ();
557 $self->{statusLog} = ();
558 $self->{errorLog} = ();
559 $self->{elementArray} = ();
560 };
561  
562  
563 ############################################################
564 ## the object constructor ##
565 ############################################################
566 sub new {
567 my $invocant = shift;
568 my $class = ref($invocant) || $invocant; # Object or class name
569 my $self = {@_};
570 bless ($self, $class);
571 $self->$initialize_XMLScripted();
572 return $self;
573 }
574  
575  
576  
577 # setDownLoadPathName()
578 sub setDownLoadPathName() {
579 my $self = shift;
580 $self->{downLoadPathName} = shift;
581 }
582  
583 # getDownLoadPathName()
584 sub getDownLoadPathName()
585 {
586 my $self = shift;
587 return $self->{downLoadPathName};
588 }
589  
590 # setDownLoadDirName()
591 sub setDownLoadDirName() {
592 my $self = shift;
593 $self->{downLoadDirName} = shift;
594 }
595  
596 # getDownLoadDirName()
597 sub getDownLoadDirName()
598 {
599 my $self = shift;
600 return $self->{downLoadDirName};
601 }
602  
603 # setStatusReportFileName()
604 sub setStatusReportFileName() {
605 my $self = shift;
606 $self->{statusReportFileName} = shift;
607 }
608  
609 # getStatusReportFileName()
610 sub getStatusReportFileName()
611 {
612 my $self = shift;
613 return $self->{statusReportFileName};
614 }
615  
616  
617 # Generate full path name, and append the file separator character to
618 # the dirName if necessary
619 # getFullFileName(dirName, fileName)
620 sub getFullFileName()
621 {
622 my $self = shift;
623 my $dirName = "";
624 my $fileName = "";
625 $dirName = shift;
626 $fileName = shift;
627
628 if ( $self->getOperatingSystem() =~ m/LINUX/i )
629 {
630 # Check if file separator exists
631 if ( $dirName =~ m/\/$/ )
632 {
633 # Good, do nothing here.
634 }
635 else
636 {
637 # Append the file separator
638 $dirName = $dirName."/";
639 }
640 }
641 else # Windows
642 {
643 # Check if file separator exists
644 if ( $dirName =~ m/\\$/ )
645 {
646 # Good, do nothing here.
647 }
648 else
649 {
650 # Append the file separator
651 $dirName = $dirName."\\";
652 }
653 }
654 my $str = sprintf("%s%s",$dirName,$fileName);
655
656 return $str;
657 }
658  
659 =item sub getTranslationRules()
660  
661 The function getTranslationRules() return a string that contains translation grammar.
662  
663 =cut
664  
665 sub getTranslationRules()
666 {
667 my $str = "";
668 $str = "<!-- Convention:
669 #
670 # Translator for URLs and path names according to the Singh's rules
671 #
672 #
673 # Convention:
674 #
675 # YYYY - Year, Ex: 2006
676 # YY - Year, Ex: 1, 2, ...., 99
677 # MM - Month, 1, ..., 12
678 # DD - Day, 1, ..., 31
679 #
680 # YY_2 - Year with at 2 digits, 01, 02, ..., 99
681 # MM_2 - Month (always 2 digits), 01, 02, ..., 12
682 # DD_2 - Day (always 2 digits), 01, 02, ..., 31
683 #
684 # DayOfWeek - Monday, Tuesday, ..., Sunday
685 # DAYOFWEEK - MONDAY, TUESDAY, ..., SUNDAY
686 # dayofweek - monday, tuesday, ..., sunday
687 # DayOfWeek_Abr - Mon, Tue, ..., Sun
688 # DAYOFWEEK_ABR - MON, TUE, ..., SUN
689 # dayofweek_abr - mon, tue, ..., sun
690 # WD - Day of the week, 1, 2, 3, 4, 5, 6, 7
691 #
692 # MonthOfYear - January, February, ..., December
693 # MONTHOFYEAR - JANUARY, FEBRUARY, ..., DECEMBER
694 # monthofyear - january, february, ..., december
695 # MonthOfYear_Abr - Jan, Feb, Mar, Apr, ..., Dec
696 # MONTHOFYEAR_ABR - JAN, FEB, MAR, APR, ..., DEC
697 # monthofyear_abr - jan, feb, mar, apr, ..., dec
698 #
699 # DayOfYear - 1, 2, 3, ....., 365
700 # DayOfYear_2 - DayOfYear with at least 2 digits, 01, 02, ..., 365
701 # DayOfYear_3 - DayOfYear with at least 3 digits, 001, 002, ..., 365
702 #
703 # HH24 - Hour, 1, 2, 3, ..., 24
704 # HH12 - Hour, 1, 2, 3, ..., 12
705 # HH24_2 - Hour with at least 2 digits, 01, 02, ..., 24
706 # HH12_2 - Hour with at least 2 digits, 01, 02, ..., 12
707 # mm - minutes, 0, 1, 2, ..., 59
708 # mm_2 - minutes with at least 2 digits, 01, 02, ..., 59
709 # ss - seconds, 0, 1, 2, ..., 59
710 # ss_2 - seconds with at least 2 digits, 01, 02, ..., 59
711 #
712 # AMPM - AM or PM
713 # ampm - am or pm
714 #
715 # OFFSETS:
716 # -nD - n Day offset for example [DD-1D], [DD-3D]
717 # -nW - n Week offset for example [DD-1W], [DD-3W]
718 # -nM - n Month offset for example [DD-1M], [DD-3M]
719 # -nY - n Year offset for example [DD-1Y], [DD-3Y]
720 #
721 -->\n";
722 return $str;
723 }
724  
725 =item sub createTranslationHash()
726  
727 The function createTranslationHash() generates the hash that contains the translation rules.
728 It's input argument is an Epoch Time variable.
729  
730 Example: $cio->createTranslationHash(time);
731  
732 =cut
733  
734 sub createTranslationHash()
735 {
736 my $self = shift;
737 my $TIME = shift;
738 %translationHash = $self->$createTranslationHashCore($TIME);
739 }
740  
741  
742  
743  
744 ## Get Translated String
745 sub getTranslatedString()
746 {
747 my $self = shift;
748 my $inputString = shift;
749 my $outputString;
750 $outputString = $self->$getTranslatedStringCore($inputString, \%translationHash);
751
752 ## Look for offsets
753 while ( $outputString =~ m/\[(.)*([+|-])(\d)[DWMY]\]/ )
754 {
755 # More to go
756 while ( my ($key, $value) = each(%translationHash) )
757 {
758 # Day Offsets
759 if ( $outputString =~ m/\[$key([-|+])(\d+)D\]/ )
760 {
761 my $mySign = $1;
762 my $myDayOffset = $2;
763 my $myPattern;
764 if ( $mySign =~ m/-/ )
765 {
766 $myDayOffset = -1 * $myDayOffset;
767 if ( $myDayOffset != 0 )
768 {
769 $myPattern = sprintf("%s%dD",$key,$myDayOffset);
770 }
771 else
772 {
773 $myPattern = sprintf("%s-0D",$key);
774 }
775 }
776 else
777 {
778 $myPattern = sprintf("%s\\+%dD",$key,$myDayOffset);
779 }
780 my $myTime = time;
781 $myTime = $myTime + $myDayOffset * 24 * 60 * 60;
782 $outputString =~ s/\[$myPattern\]/\[$key\]/g;
783 my %offsetTranslationHash = $self->$createTranslationHashCore($myTime);
784 $outputString = $self->$getTranslatedStringCore($outputString, \%offsetTranslationHash);
785 }
786 # Week Offsets
787 if ( $outputString =~ m/\[$key([-|+])(\d+)W\]/ )
788 {
789 my $mySign = $1;
790 my $myWeekOffset = $2;
791 my $myPattern;
792 if ( $mySign =~ m/-/ )
793 {
794 $myWeekOffset = -1 * $myWeekOffset;
795 if ( $myWeekOffset != 0 )
796 {
797 $myPattern = sprintf("%s%dW",$key,$myWeekOffset);
798 }
799 else
800 {
801 $myPattern = sprintf("%s-0W",$key);
802 }
803 }
804 else
805 {
806 $myPattern = sprintf("%s\\+%dW",$key,$myWeekOffset);
807 }
808 my $myTime = time;
809 $myTime = $myTime + $myWeekOffset * 7 * 24 * 60 * 60;
810 $outputString =~ s/\[$myPattern\]/\[$key\]/g;
811 my %offsetTranslationHash = $self->createTranslationHashCore($myTime);
812 $outputString = $self->getTranslatedStringCore($outputString, \%offsetTranslationHash);
813 }
814 # Month Offsets
815 if ( $outputString =~ m/\[$key([-|+])(\d+)M\]/ )
816 {
817 my $mySign = $1;
818 my $myMonthOffset = $2;
819 my $myPattern;
820 if ( $mySign =~ m/-/ )
821 {
822 $myMonthOffset = -1 * $myMonthOffset;
823 if ( $myMonthOffset != 0 )
824 {
825 $myPattern = sprintf("%s%dM",$key,$myMonthOffset);
826 }
827 else
828 {
829 $myPattern = sprintf("%s-0M",$key);
830 }
831 }
832 else
833 {
834 $myPattern = sprintf("%s\\+%dM",$key,$myMonthOffset);
835 }
836 my $myTime = time;
837 $myTime = $myTime + $myMonthOffset * 30 * 24 * 60 * 60;
838 $outputString =~ s/\[$myPattern\]/\[$key\]/g;
839 my %offsetTranslationHash = $self->createTranslationHashCore($myTime);
840 $outputString = $self->getTranslatedStringCore($outputString, \%offsetTranslationHash);
841 }
842 # Year Offsets
843 if ( $outputString =~ m/\[$key([-|+])(\d+)Y\]/ )
844 {
845 my $mySign = $1;
846 my $myYearOffset = $2;
847 my $myPattern;
848 if ( $mySign =~ m/-/ )
849 {
850 $myYearOffset = -1 * $myYearOffset;
851 if ( $myYearOffset != 0 )
852 {
853 $myPattern = sprintf("%s%dY",$key,$myYearOffset);
854 }
855 else
856 {
857 $myPattern = sprintf("%s-0Y",$key);
858 }
859 }
860 else
861 {
862 $myPattern = sprintf("%s\\+%dY",$key,$myYearOffset);
863 }
864 my $myTime = time;
865 $myTime = $myTime + $myYearOffset * 365 * 24 * 60 * 60;
866 $outputString =~ s/\[$myPattern\]/\[$key\]/g;
867 my %offsetTranslationHash = $self->createTranslationHashCore($myTime);
868 $outputString = $self->getTranslatedStringCore($outputString, \%offsetTranslationHash);
869 }
870 }
871 }
872
873 return $outputString;
874 }
875  
876  
877  
878 ## Parse input xml file
879 sub parseXMLFile()
880 {
881 my $self = shift;
882 my $xmlfile = shift;
883 my $parser;
884 die "Can't find file \"$xmlfile\"" unless -f $xmlfile;
885  
886 # initialize parser object and parse the string
887 $parser = new XML::Parser(ErrorContext => 2, Style => 'Tree' );
888 $tree = $parser->parsefile($xmlfile);
889 # report any error that stopped parsing, or announce success
890 if ( $@ )
891 {
892 $@ =~ s/at \/.*?$//s; # remove module line number
893 #print STDERR "\nERROR in '$xmlfile':\n$@\n";
894 $self->$addErrorLog("Error in $xmlfile. $@");
895 return;
896 }
897 else
898 {
899 #print STDERR "'$xmlfile' is well-formed\n";
900 $self->$addStatusLog("The XML file $xmlfile is well-formed");
901 }
902 #
903  
904 my ($i, $j, $k);
905 no strict 'refs';
906 # Parse nodes at the first level
907 for $i ( 0 .. $#{$tree} ) {
908 for $j ( 0 .. $#{$tree->[$i]} ) {
909 # Download Path Name
910 if ( $tree->[$i][$j] eq "downLoadPathName" )
911 {
912 $self->setDownLoadPathName($self->getTranslatedString($self->trim($tree->[$i][$j+1][2])));
913 }
914 # Download Directory Name
915 elsif ( $tree->[$i][$j] =~ "downLoadDirName" )
916 {
917 $self->setDownLoadDirName($self->getTranslatedString($self->trim($tree->[$i][$j+1][2])));
918 }
919 # Status Report File Name
920 elsif ( $tree->[$i][$j] =~ "statusReportFileName" )
921 {
922 $self->setStatusReportFileName($self->getTranslatedString($self->trim($tree->[$i][$j+1][2])));
923 }
924 # Entry
925 elsif ( $tree->[$i][$j] =~ "entry" )
926 {
927 my $elemento = Net::Download::XMLScripted::XMLScriptedElement::->new();
928 for $k ( 0 .. $#{$tree->[$i][$j+1]} )
929 {
930 # URL
931 if ( $tree->[$i][$j+1][$k] eq "url" )
932 {
933 $elemento->setUrlName($self->getTranslatedString($self->trim($tree->[$i][$j+1][$k+1][2])));
934 }
935 # dirName
936 if ( $tree->[$i][$j+1][$k] eq "dirName" )
937 {
938 $elemento->setDirName($self->getTranslatedString($self->trim($tree->[$i][$j+1][$k+1][2])));
939 }
940 # fileName
941 if ( $tree->[$i][$j+1][$k] eq "fileName" )
942 {
943 $elemento->setFileName($self->getTranslatedString($self->trim($tree->[$i][$j+1][$k+1][2])));
944 }
945 }
946 #
947 push @{$self->{elementArray}}, $elemento;
948 }
949 }
950 }
951 $self->generateFullFileNames();
952 }
953  
954 ##
955 sub generateFullFileNames()
956 {
957 my $self = shift;
958 my @elementArray = @{$self->{elementArray}};
959 #
960 foreach my $elem ( @elementArray )
961 {
962 my $str = $self->getFullFileName($self->getDownLoadPathName(),$self->getDownLoadDirName());
963 $str = $self->getFullFileName($str, $elem->getDirName());
964 $str = $self->getFullFileName($str, $elem->getFileName());
965 $elem->setFullFileName($str);
966 }
967 }
968  
969 ###
970 sub toString{
971 my $self = shift;
972 my @elementArray = @{$self->{elementArray}};
973 my $str = "XMLScripted\n";
974 $str = $str.sprintf("DownLoadPath: %s, DownLoadDir: %s ",$self->getDownLoadPathName(), $self->getDownLoadDirName());
975 $str = $str."\n\n";
976 $str = $str."Number of Elements in Array: ".($#elementArray+1);
977 $str = $str."\n\n";
978 my $count = 1;
979
980 # Element array
981 my $object;
982 foreach $object( @elementArray )
983 {
984 next unless ref $object;
985 $str = $str. sprintf("[%d] %s\n",$count, $object->toString());
986 $count = $count + 1;
987 }
988
989 # Status Log
990 my @statusLog = @{$self->{statusLog}};
991 $str = $str."\nXMLScripted Status Log: ".($#statusLog+1)."\n";
992 $count = 1;
993 foreach my $statusLogElem ( @statusLog)
994 {
995 $str = $str."\t[$count] $statusLogElem\n";
996 $count = $count + 1;
997 }
998
999 # Error Log
1000 my @errorLog = @{$self->{errorLog}};
1001 $str = $str."XMLScripted Error Log: ".($#errorLog+1)."\n";
1002 $count = 1;
1003 foreach my $errorLogElem (@errorLog)
1004 {
1005 $str = $str."\t[$count] $errorLogElem \n";
1006 $count = $count + 1;
1007 }
1008 return $str;
1009 }
1010  
1011 ###
1012 sub toStringHTML{
1013 my $self = shift;
1014 my $str;
1015 $str = $str. "<HTML>\n";
1016 $str = $str. "<HEAD>\n";
1017 $str = $str. "<TITLE>";
1018 $str = $str. "XMLScripted Status Report";
1019 $str = $str. "</TITLE>\n";
1020 $str = $str. "</HEAD>\n";
1021 $str = $str. "<BODY>\n";
1022 $str = $str. "<CENTER><H1>";
1023 $str = $str. "XMLScripted Status Report";
1024 $str = $str. "</H1></CENTER>\n";
1025 $str = $str. "<CENTER>".$self->getDateTimeStamp()."</CENTER>\n";
1026 $str = $str. "<UL>\n";
1027 $str = $str. "<LI>";
1028 $str = $str. "<B>Download Path Name: </B> $self->{downLoadPathName}\n";
1029 $str = $str. "</LI>\n";
1030 $str = $str. "<LI>";
1031 $str = $str. "<B>Download Directory Name: </B> $self->{downLoadDirName}\n";
1032 $str = $str. "</LI>\n";
1033 $str = $str. "<LI>";
1034 $str = $str. "<B>Status Report File Name: </B>".$self->getFullFileName($self->getFullDirectoryName(),$self->getStatusReportFileName())."\n";
1035 $str = $str. "</LI>\n";
1036 $str = $str. "<LI>";
1037 $str = $str. "<B>Number of Download Elements: </B>".$self->getNumberOfElements()."\n";
1038 $str = $str. "</LI>\n";
1039 $str = $str. "</UL>\n";
1040 # Status Log
1041 my $statusLogRef = \@{$self->{statusLog}};
1042 my @statusLog = @$statusLogRef;
1043 $str = $str. "<H2>XMLScripted Status Log [".($#statusLog+1)."] Messages</H2>\n";
1044 $str = $str. "<OL>\n";
1045 foreach my $statusLogElem ( @statusLog)
1046 {
1047 $str = $str."<LI style=\"color:green\"> $statusLogElem\n";
1048 }
1049 $str = $str. "</OL>\n";
1050 # Error Log
1051 my $errorLogRef = \@{$self->{errorLog}};
1052 my @errorLog = @$errorLogRef;
1053 $str = $str. "<H2>XMLScripted Error Log [".($#errorLog+1)."] Messages</H2>\n";
1054 $str = $str. "<OL>\n";
1055 foreach my $errorLogElem ( @errorLog)
1056 {
1057 $str = $str."<LI style=\"color:red\"> $errorLogElem\n";
1058 }
1059 $str = $str. "</OL>\n";
1060 $str = $str. "<CENTER><H1>";
1061 $str = $str. "XMLScripted Download Elements Status Report";
1062 $str = $str. "</H1></CENTER>\n";
1063 # Element array
1064 my $object;
1065 my $count = 1;
1066 foreach $object( @{$self->{elementArray}} )
1067 {
1068 next unless ref $object;
1069 $str = $str."<H2>$count. Download Element</H2>\n";
1070 $str = $str. sprintf("%s\n", $object->toStringHTML());
1071 $count = $count + 1;
1072 }
1073 $str = $str. "</BODY>\n";
1074 $str = $str. "</HTML>\n";
1075 }
1076  
1077 ##
1078 sub getNumberOfElements()
1079 {
1080 my $self = shift;
1081 my $count = 0;
1082 foreach my $object( @{$self->{elementArray}} )
1083 {
1084 $count = $count + 1;
1085 }
1086 return $count;
1087 }
1088  
1089 ##
1090 sub getDateTimeStamp()
1091 {
1092 my $self = shift;
1093 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
1094 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();
1095 my $str = sprintf( "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec);
1096 return $str;
1097 }
1098  
1099 ##
1100 sub getDateStamp()
1101 {
1102 my $self = shift;
1103 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
1104 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime();
1105 my $str = sprintf( "%4d-%02d-%02d",$year+1900,$mon+1,$mday);
1106 return $str;
1107 }
1108  
1109  
1110  
1111 ##
1112 sub clone {
1113 my $model = shift;
1114 my $self = $model->new(%$model, @_);
1115 return $self; # Previously blessed by ->new
1116 }
1117  
1118  
1119 ## Print Translation Hash
1120 sub printTranslationHash()
1121 {
1122 print "Translation Hash:\n";
1123 $~ = 'FORMAT_TRANS';
1124 $x = "Key";
1125 $y = "Value";
1126 write();
1127 while ( my ($key, $value) = each(%translationHash) ) {
1128 $x = "[".$key."]";
1129 $y = $value;
1130 write();
1131 }
1132 }
1133  
1134 ## Get Translation Hash
1135 sub getTranslationHashToString()
1136 {
1137 my $str = "#Translation Hash:\n";
1138 while ( my ($key, $value) = each(%translationHash) ) {
1139 $str = $str.sprintf("#\t[%s] \t%s\n", $key, $value);
1140 }
1141 return $str;
1142 }
1143  
1144 =item sub download()
1145  
1146 The function dowload() performs the download operations. This includes creation of required directories.
1147  
1148 =cut
1149  
1150 sub download()
1151 {
1152 my $self = shift;
1153 $self->$makeDirectories();
1154 $self->$makeElementDirectories();
1155 my @elementArray = @{$self->{elementArray}};
1156 foreach my $elem ( @elementArray )
1157 {
1158 $elem->download();
1159 }
1160 }
1161  
1162 =item sub getFullDirectoryName()
1163  
1164 This function returns full Directory name given a main path and a subdirectory.
1165  
1166 =cut
1167  
1168 sub getFullDirectoryName()
1169 {
1170 my $self = shift;
1171 return $self->getFullFileName($self->getDownLoadPathName(), $self->getDownLoadDirName());
1172 }
1173  
1174 ##
1175 sub getEpochTimeFromDate()
1176 {
1177 my $self = shift;
1178 my $date = shift;
1179 my ($yy, $mm, $dd);
1180 #
1181 ($yy,$mm, $dd) = ( $date =~ /(\d+)-(\d+)-(\d+)/);
1182 $mm = $mm-1;
1183 $yy = $yy-1900;
1184 my $TIME = timelocal((localtime)[0,1,2],$dd, $mm, $yy);
1185 return $TIME;
1186 }
1187  
1188 ##
1189 sub getDateFromEpochTime()
1190 {
1191 my $self = shift;
1192 my $myTime = shift;
1193 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
1194 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($myTime);
1195 my $str = sprintf( "%04d-%02d-%02d",$year+1900,$mon+1,$mday);
1196 return $str;
1197 }
1198  
1199 =item sub writeStatusReport()
1200  
1201 This function creates status report in the download directory.
1202  
1203 =cut
1204  
1205 sub writeStatusReport()
1206 {
1207 my $self = shift;
1208
1209 unless ( -d $self->getFullDirectoryName() )
1210 {
1211 $self->addErrorLog("Error: Could not create Status Report because directory ".$self->getFullDirectoryName());
1212 return;
1213 }
1214 my $fullStatusFileName = $self->getFullStatusReportFileName();
1215 unless ( open(STATUS, ">$fullStatusFileName") )
1216 {
1217 $self->addErrorLog("Error: Could not create Status Report. Not able to create $fullStatusFileName. $!");
1218 return;
1219 }
1220 print STATUS $self->toStringHTML();
1221 close STATUS;
1222
1223 }
1224  
1225 ##
1226 sub getFullStatusReportFileName()
1227 {
1228 my $self = shift;
1229 my $fullStatusFileName = $self->getFullFileName($self->getFullDirectoryName(),$self->getStatusReportFileName());
1230 return $fullStatusFileName;
1231 }
1232  
1233 =item sub getOperatingSystem()
1234  
1235 The getOperatingSystem() function returns the type of Operating System.
1236  
1237 =cut
1238  
1239 sub getOperatingSystem()
1240 {
1241 my $OS = '';
1242 unless ($OS) {
1243 unless ($OS = $^O) {
1244 require Config;
1245 $OS = $Config::Config{'osname'};
1246 }
1247 }
1248 if ( $OS =~ m/Win/i ) { $OS = 'WINDOWS'; }
1249 elsif ( $OS =~ m/vms/i) { $OS = 'VMS'; }
1250 elsif ( $OS =~ m/^MacOS$/i) { $OS = 'MACINTOSH'; }
1251 elsif ( $OS =~ m/os2/i) { $OS = 'OS2'; }
1252 else { $OS = 'LINUX'; }
1253 return $OS;
1254 }
1255  
1256 ## trim
1257 ## Public method
1258 sub trim {
1259 my $self = shift;
1260 my @out = @_;
1261 for (@out) {
1262 s/^\s+//; # discard leading whitespace
1263 s/\s+$//; # discard trailing whitespace
1264 }
1265 return wantarray ? @out : $out[0];
1266 }
1267  
1268 ##
1269 format FORMAT_TRANS=
1270 @<<<<<<<<<<<<<<<<<<<< ... @<<<<<<<<<<<<<<<<<<<
1271 $x, $y
1272 .
1273  
1274 =item sub printUsage()
1275  
1276 This program prints the usage of mainXMLScripted.
1277  
1278 =cut
1279 ##
1280 sub printUsage()
1281 {
1282 my $self = shift;
1283 print "Usage: perl XMLScripted.pm -inXMLFileName xmlFileName [-verbose]\n";
1284 print "Usage: perl XMLScripted.pm -inXMLFileName xmlFileName -beginDate YYYY-MM-DD [-endDate YYYY-MM-DD]\n";
1285 print "Usage: perl XMLScripted.pm -generateSampleXMLFile sampleXMLFileName\n";
1286 print "Usage: perl XMLScripted.pm -showTranslationRules\n";
1287 print "Usage: perl XMLScripted.pm -version\n";
1288 }
1289  
1290  
1291 =item sub getVersion()
1292  
1293 Returns current version of the program.
1294  
1295 =cut
1296 ## getVersion()
1297 sub getVersion()
1298 {
1299 my $self = shift;
1300 my $str = "";
1301 $str = "".$VERSION;
1302 return $str;
1303 }
1304  
1305 =item sub generateSampleXMLFile()
1306  
1307 Generates sample XML file that can be modified and used as input.
1308  
1309 =cut
1310 ## generateSampleXMLFile()
1311 sub generateSampleXMLFile()
1312 {
1313 my $self = shift;
1314 my $fileName = shift;
1315 print "Generating Sample XML File :".$fileName."\n";
1316 open(FILE, ">$fileName") or die "Could not open ".$fileName." $!\n ";
1317 print FILE <<EOF;
1318 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
1319 <!DOCTYPE xmlscripted [
1320 <!ELEMENT xmlscripted (downLoadPathName, downLoadDirName, statusReportFileName, entry+)>
1321 <!ELEMENT downLoadPathName (#PCDATA)>
1322 <!ELEMENT downLoadDirName (#PCDATA)>
1323 <!ELEMENT statusReportFileName (#PCDATA)>
1324 <!ELEMENT entry (url, dirName, fileName)>
1325 <!ELEMENT url (#PCDATA)>
1326 <!ELEMENT dirName (#PCDATA)>
1327 <!ELEMENT fileName (#PCDATA)>
1328 ]>
1329 <!-- SAMPLE INPUT FILE FOR DAILY DOWNLOAD PROGRAM -->
1330 EOF
1331 print FILE $self->getTranslationRules();
1332 print FILE<<EOF;
1333 <xmlscripted>
1334  
1335 <!-- Download Path Name -->
1336 <downLoadPathName>/tmp/download.dir/</downLoadPathName>
1337  
1338 <!-- Download Directory Name -->
1339 <downLoadDirName>[YYYY]-[MM_2]-[DD_2].dir</downLoadDirName>
1340  
1341 <!-- Status Report HTML File Name -->
1342 <statusReportFileName>Status.html</statusReportFileName>
1343  
1344 <!-- Entry -->
1345 <entry>
1346 <url>http://www.suntimes.com/cgi-bin/print.cgi?getReferrer=http://www.suntimes.com/output/horoscopes/cst-nws-holly[MM][DD].html</url>
1347 <dirName>horoscope</dirName>
1348 <fileName>horoscope.html</fileName>
1349 </entry>
1350  
1351 <!-- Entry -->
1352 <entry>
1353 <url>http://www.suntimes.com/cgi-bin/print.cgi?getReferrer=http://www.suntimes.com/output/lottery/cst-nws-lot[DD_2].html</url>
1354 <dirName>lottery</dirName>
1355 <fileName>lottery.html</fileName>
1356 </entry>
1357  
1358 <!-- Entry -->
1359 <entry>
1360 <url>http://transcripts.cnn.com/TRANSCRIPTS/[YYYY].[MM_2].[DD_2].html</url>
1361 <dirName>news</dirName>
1362 <fileName>news.html</fileName>
1363 </entry>
1364  
1365  
1366 </xmlscripted>
1367 EOF
1368 close FILE;
1369 return;
1370 }
1371  
1372 =item sub runEngine()
1373  
1374 This routine is responsible for performing the actual task. It's arguments are
1375 input xmlFileName, beginDate, and endDate. The format of date is YYYY-MM-DD.
1376 Ex: runEngine("/tmp/input.xml", "2007-07-04", "2007-07-04");
1377  
1378 =cut
1379 ## runEngine()
1380 sub runEngine()
1381 {
1382 my $self = shift;
1383 my $xmlFile = shift;
1384 my $beginDate = shift;
1385 my $endDate = shift;
1386 print "The input XML File: ".$xmlFile."\n";
1387
1388 my $BEGINTIME = $self->getEpochTimeFromDate($beginDate);
1389 my $ENDTIME = $self->getEpochTimeFromDate($endDate);
1390 #
1391 my $interval = 1 * 24 * 60 * 60; ## one day in seconds
1392 #
1393 my $myTime = $BEGINTIME;
1394 #
1395 while ( $myTime <= $ENDTIME )
1396 {
1397 my $myDate = $self->getDateFromEpochTime($myTime);
1398 print "Working on ".$myDate."\n";
1399 $self->createTranslationHash($myTime);
1400 $self->parseXMLFile($xmlFile);
1401 $self->download();
1402 $self->writeStatusReport();
1403 print ">>>Status Report created at : ".$self->getFullStatusReportFileName()."\n";
1404 $myTime = $myTime + $interval;
1405 }
1406 }
1407  
1408  
1409 =item sub run()
1410  
1411 This routine is called by the instance of the XMLScripted object and it
1412 runs the program.
1413  
1414 =over 4
1415  
1416 =item * # Create XMLScripted Object
1417  
1418 =item * my $cio = Net::Download::XMLScripted::XMLScripted::->new();
1419  
1420 =item * # Run it
1421  
1422 =item * $cio->run();
1423  
1424 =back
1425  
1426 =cut
1427 sub run()
1428 {
1429 my $self = shift;
1430 my $numArgs = $#ARGV + 1;
1431 print "Welcome to XMLScripted Program!\n";
1432 #
1433 if ( $numArgs == 0 )
1434 {
1435 $self->printUsage();
1436 print "XMLScripted Program Ended!\n";
1437 exit(1);
1438 }
1439  
1440 #
1441 if ( $numArgs == 1 )
1442 {
1443 if ( $ARGV[0] =~ /-showTranslationRules/ )
1444 {
1445 print $self->getTranslationRules()."\n";
1446 print "XMLScripted Program Ended!\n";
1447 exit(1);
1448 }
1449 elsif ( $ARGV[0] =~ /-version/ )
1450 {
1451 print $self->getVersion()."\n";
1452 print "XMLScripted Program Ended!\n";
1453 exit(1);
1454 }
1455 else
1456 {
1457 $self->printUsage();
1458 print "XMLScripted Program Ended!\n";
1459 exit(1);
1460 }
1461 }
1462  
1463 #
1464 if ( $numArgs == 2 || $numArgs == 3 )
1465 {
1466 if ( $ARGV[0] =~ /-inXMLFileName/ )
1467 {
1468 my $xmlFile = $ARGV[1];
1469 my $myTime = time;
1470 my $myDate = $self->getDateFromEpochTime($myTime);
1471 my $beginDate = $myDate;
1472 my $endDate = $myDate;
1473
1474 $self->runEngine($xmlFile, $beginDate, $endDate);
1475
1476 if ( $numArgs == 3 )
1477 {
1478 if ($ARGV[2] =~ /-verbose/ )
1479 {
1480 print $self->toString()."\n";
1481 }
1482 }
1483 print "XMLScripted Program Ended!\n";
1484 exit(1);
1485 }
1486 elsif ( $ARGV[0] =~/-generateSampleXMLFile/ )
1487 {
1488 #
1489 my $sampleXMLFile = $ARGV[1];
1490 $self->generateSampleXMLFile($sampleXMLFile);
1491 print "XMLScripted Program Ended!\n";
1492 exit(1);
1493 }
1494 else
1495 {
1496 $self->printUsage();
1497 print "XMLScripted Program Ended!\n";
1498 exit(1);
1499 }
1500 }
1501  
1502 #
1503 if ( $numArgs == 4 )
1504 {
1505 if ( $ARGV[0] =~/inXMLFileName/ && $ARGV[2] =~ /-beginDate/ )
1506 {
1507 my $xmlFile = $ARGV[1];
1508 my $beginDate = $ARGV[3];
1509 my ($mday, $mon, $year);
1510 ($mday, $mon, $year) = (localtime)[3,4,5];
1511 my $endDate = sprintf("%04d-%02d-%02d",$year+1900,($mon+1),$mday);
1512
1513 $self->runEngine($xmlFile, $beginDate, $endDate);
1514 }
1515 else
1516 {
1517 $self->printUsage();
1518 print "XMLScripted Program Ended!\n";
1519 exit(1);
1520 }
1521 }
1522 #
1523 if ($numArgs == 6 )
1524 {
1525 if ( $ARGV[0] =~/inXMLFileName/ && $ARGV[2] =~ /-beginDate/ && $ARGV[4] =~ /-endDate/ )
1526 {
1527 my $xmlFile = $ARGV[1];
1528 my $beginDate = $ARGV[3];
1529 my $endDate = $ARGV[5];
1530
1531 $self->runEngine($xmlFile, $beginDate, $endDate);
1532 }
1533 else
1534 {
1535 $self->printUsage();
1536 print "XMLScripted Program Ended!\n";
1537 exit(1);
1538 }
1539 }
1540
1541 }
1542  
1543 ###############################################################################
1544 ###############################################################################
1545 ###############################################################################
1546 ###############################################################################
1547 ###############################################################################
1548 ###############################################################################
1549 # main program
1550  
1551 use strict;
1552 #
1553 ## Create XMLScripted Object
1554 my $xmlScripted = Net::Download::XMLScripted::XMLScripted::->new();
1555 if ( $#ARGV > -1 )
1556 {
1557 $xmlScripted->run();
1558 exit(1);
1559 }
1560 #
1561  
1562 1; # Let's require or use succeed
1563 __END__
↑ Summary