updated git and svn scripts
[xrzperl.git] / listlinks
blob0d759df50c9bfcf7b460216a1ba8dbd59a532e92
1 #!/usr/bin/perl -w
2 #Autor : xiaoranzzz@myplace
3 #Date : 2008.08.07 06:31AM
4 use strict;
5 my $appname="listlinks.pl";
6 my $appdesc="listlinks of URL, optionally run COMMANDS against it";
7 my $appver="0.1";
9 sub usage {
10 print "$appname v$appver\n\t- $appdesc\n","Usage:\n","\t";
11 print "$0 [options] URL commands";
12 print "\n";
13 print "Options:\n";
14 print "\t-d\tDebug mode\n";
15 print "\t-n\tNo recursive mode\n";
16 print "\t-h\tDisplay this text\n";
17 exit 0;
20 my ($nr,$DEBUG,$url,@downloader);
22 for my $arg (@ARGV) {
23 if($arg eq "-h" || $arg eq "-H") {
24 &usage;
25 exit 0;
27 elsif($arg eq "-n") {
28 $nr=1;
30 elsif($arg eq "-d") {
31 $DEBUG=1;
33 elsif(!$url) {
34 $url=$arg;
36 else {
37 push @downloader,$arg;
40 &usage unless(@ARGV);
41 my @lister;
42 if($DEBUG) {
43 @lister=("wget","-v");
45 else {
46 @lister=("wget","-q");
49 use File::Temp qw/tempfile/;
50 sub listftp {
51 my $ftp_url=shift;
52 return unless($ftp_url);
53 my %result;
54 my (undef,$fn) = tempfile();
55 print STDERR "Listing $ftp_url...\n";
56 if(system(@lister,"-O",$fn,$ftp_url) == 0) {
57 # print STDERR "\t[OK]\n";
59 else {
60 die("\n$!\n");
62 open FI,"<",$fn;
63 while(<FI>) {
64 if(m#<a href="([^"]+)"#i) {
65 my $url=$1;
66 my $type= ($url =~ /[\/\\]$/) ? "directory" : "file";
67 push @{$result{$type}},$url;
70 close FI;
71 return %result;
74 sub getfile($) {
75 my $url=shift;
76 return undef unless($url);
77 if(@downloader) {
78 if($DEBUG) {
79 print STDERR "#",join(" ",@downloader)," $url","\n";
81 return(system(@downloader,$url)==0);
83 else {
84 print("$url\n");
85 return 1;
87 return 1;
90 sub getfolder($) {
91 my $fdurl=shift;
92 my %result = listftp($fdurl);
93 my @dirs = @{$result{directory}} if($result{directory});
94 my @files = @{$result{file}} if($result{file});
95 foreach(@files) {
96 &getfile($_);
98 return 1 if($nr);
99 foreach(@dirs) {
100 &getfolder($_);
104 if($url) {
105 &getfolder($url);
107 else {
108 &usage;
109 exit 0;