source: trunk/gsdl/perllib/downloaders/WebDownload.pm@ 11783

Last change on this file since 11783 was 11783, checked in by kjdon, 18 years ago

Jefferey's new download modules

  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1###########################################################################
2#
3# WebDownload.pm -- base class for all the import plugins
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package WebDownload;
27
28eval {require bytes};
29
30# suppress the annoying "subroutine redefined" warning that various
31# plugins cause under perl 5.6
32$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
33
34use WgetDownload;
35
36sub BEGIN {
37 @WebDownload::ISA = ('WgetDownload');
38}
39
40use strict; # every perl program should have this!
41no strict 'refs'; # make an exception so we can use variables as filehandles
42
43my $arguments =
44 [ { 'name' => "url",
45 'disp' => "{WebDownload.url_disp}",
46 'desc' => "{WebDownload.url}",
47 'type' => "string",
48 'reqd' => "yes"},
49 { 'name' => "depth",
50 'disp' => "{WebDownload.depth_disp}",
51 'desc' => "{WebDownload.depth}",
52 'type' => "int",
53 'deft' => "0",
54 "range" => "0,",
55 'reqd' => "no"},
56 { 'name' => "below",
57 'disp' => "{WebDownload.below_disp}",
58 'desc' => "{WebDownload.below}",
59 'type' => "flag",
60 'reqd' => "no"},
61 { 'name' => "within",
62 'disp' => "{WebDownload.within_disp}",
63 'desc' => "{WebDownload.within}",
64 'type' => "flag",
65 'reqd' => "no"},
66 { 'name' => "html_only",
67 'disp' => "{WebDownload.html_only_disp}",
68 'desc' => "{WebDownload.html_only}",
69 'type' => "flag",
70 'reqd' => "no"}
71 ];
72
73my $options = { 'name' => "WebDownload",
74 'desc' => "{WebDownload.desc}",
75 'abstract' => "no",
76 'inherits' => "yes",
77 'args' => $arguments };
78
79
80my $self;
81
82sub new
83{
84 my ($class) = shift (@_);
85 my ($getlist,$inputargs,$hashArgOptLists) = @_;
86 push(@$getlist, $class);
87
88 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
89 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
90
91 my $self = (defined $hashArgOptLists)? new WgetDownload($getlist,$inputargs,$hashArgOptLists): new WgetDownload($getlist,$inputargs);
92
93 return bless $self, $class;
94}
95
96sub download
97{
98 my ($self) = shift (@_);
99 my ($hashGeneralOptions) = @_;
100
101 # TODO: the checking for Wget is still not complete, we need to
102 # check if the proxy has been set or not, and whether the
103 # connection has been established.
104 # Checking if the wget has been well setup
105 # &WgetDownload::checkWgetSetup($self,$hashGeneralOptions->{'gli_call'});
106
107 # Download options
108 my $strOptions = &generateOptionsString($self);
109 my $strWgetOptions = &WgetDownload::getWgetOptions($self);
110
111
112 # Setup the command for using wget
113 my $cmdWget = "-N -k -x -t 2 -P ".$hashGeneralOptions->{"cache_dir"}." $strWgetOptions $strOptions ".$self->{'url'};
114
115 print "**************".$cmdWget."\n";
116
117 # Download the web pages
118 # print "Strat download from $self->{'url'}...\n";
119
120 print STDERR "<<Undefined Maximum>>\n";
121 my $strResponse = &WgetDownload::useWget($cmdWget,1);
122 #if ($strResponse ne ""){print "$strResponse\n";}
123
124 # print "Finish download from $self->{'url'}...\n";
125
126 return 1;
127}
128
129sub generateOptionsString
130{
131 my ($self) = @_;
132 my $strOptions;
133
134 (defined $self) || &error("generateOptionsString","No \$self is defined!!\n");
135 (defined $self->{'depth'})|| &error("generateOptionsString","No depth is defined!!\n");
136
137
138 if($self->{'depth'} == 0)
139 {
140 $strOptions .= " ";
141 }
142 elsif($self->{'depth'} > 0)
143 {
144 $strOptions .= "-r -l ".$self->{'depth'}." ";
145 }
146 else
147 {
148 &error("setupOptions","Incorrect Depth is defined!!\n");
149 }
150
151 if($self->{'below'})
152 {
153 $strOptions .="-np ";
154 }
155
156 if($self->{'html_only'})
157 {
158 $strOptions .="-A .html,.htm,.shm,.shtml,.asp,.php,.cgi,*?*=* ";
159 }
160 else{
161
162 $strOptions .="-p ";
163 }
164
165 if (!$self->{'within'}){
166 $strOptions .="-H ";
167 }
168
169 return $strOptions;
170
171}
172
173sub url_information
174{
175 my ($self) = shift (@_);
176 if(!defined $self){ die "System Error: No \$self defined for url_information in WebDownload\n";}
177
178 my $strBaseCMD = "-q -O - \"$self->{'url'}\"";
179
180 my $strIdentifyText = &WgetDownload::useWget($strBaseCMD);
181
182 while ($strIdentifyText =~ m/^(.*)<title>(.*?)<\/title>(.*)$/s)
183 {
184 $strIdentifyText = $1.$3;
185 print STDERR "Page Title: $2\n";
186 }
187
188 while ($strIdentifyText =~ m/^(.*)<meta (.*?)>(.*)$/s)
189 {
190 $strIdentifyText = $1.$3;
191 my $strTempString = $2;
192 print STDERR "Meta Information:\n";
193 while($strTempString =~ m/(.*?)=[\"|\'](.*?)[\"|\'](.*?)$/s)
194 {
195 # Store the infromation in to variable, since next time when we do
196 # regular expression, we will lost all the $1, $2, $X....
197 $strTempString = $3;
198 my $strMetaName = $1;
199 my $strMetaContain = $2;
200
201 # Take out the extra space in the beginning of the string.
202 $strMetaName =~ s/^([" "])+//m;
203 $strMetaContain =~ s/^([" "])+//m;
204 print STDERR "\t$strMetaName: $strMetaContain\n";
205
206 }
207 print STDERR "\n";
208 }
209}
210
211
212sub error
213{
214 my ($strFunctionName,$strError) = @_;
215 {
216 print "Error occoured in WebDownload.pm\n".
217 "In Function:".$strFunctionName."\n".
218 "Error Message:".$strError."\n";
219 exit(-1);
220 }
221}
222
2231;
Note: See TracBrowser for help on using the repository browser.