Harry Rose

Home » Perl » Downloader

Downloader

If I'm honest, I hardly ever use this one, and its a bit of a pain to configure. I only really wrote this one because I wanted to practice my PERL and awk.

One of the scripts you execute is 'downloader', basically all this does is cat a text file into the awk script which is used to parse through the file and download the links.

The list file can be edited using downman. There isn't a download for this one as the scripts were basically written for this system. Here's the code though...

downloader

#!/bin/bash
 
    cat /home/harry/lists/*.list | downloader.awk

downloader.awk

#!/bin/gawk -f
 
BEGIN {
    BASEDIR = "/home/harry/Downloads/"
    FS = "    "
    USERNAME = ""
    PASSWORD = ""
    WAIT    = 20;
    NO = 0; #initialise anyway, just to be sure
}
/.*\t.*/{
            ACTUALDIR=BASEDIR$1
            if( ACTUALDIR ~/[^/]$/ )
            {
                ACTUALDIR = ACTUALDIR"/"
            }
            URL=$2
            system( "mkdir -p '"ACTUALDIR"'" );
            if(USERNAME != "")
            {
                USERNAME = "--user="USERNAME
            }
 
            if(PASSWORD != "" && USERNAME != "")
            {
                PASSWORD = "--password="PASSWORD
            }
            system( "cd '"ACTUALDIR"' && wget -c "USERNAME" "PASSWORD" "URL);
            system( " sleep " WAIT );
            NO ++;
    }
END {
    print NO, "files downloaded"
}

downman

#!/usr/bin/perl
    $listPath = "/home/harry/lists/";
    $listoption = -1;
    $downloadBase = "/home/harry/Downloads/";
    %listHelp = (
            "l"    =>    "Lists the entries in this download list.",
            "d"    =>    "Delete entries.",
            "a"    =>    "Add new URLs to download.",
            "h"    =>    "Displays this help menu.",
            "c"    =>    "Clear the entire list."
        );
    $selectedFile;
    while($listoption != "q" && $listoption != "Q")
    {
        opendir(LISTDIR, $listPath) || die ("Unable to open dir $listPath, $!");
        @files = grep(/.*\.list$/, readdir LISTDIR);
        closedir(LISTDIR);
        sort @files;
        $listoption=0;
        while( $listoption < 1 || $listoption >= @files || $listoption =~/\D/)
        {
            $i = 1;
            print "Which download list would you like to open?\n";
            foreach $file (@files)
            {
                print "\t$i) $file\n";
                $i++;
            }
            print"?> ";$listoption = <>;
            chomp $listoption;
            if($listoption eq "q" || $listoption eq "Q")
            {
                exit 0;
            }
        
        
            $listoption--; #all indexes in the menu are +1
            $selectedFile = "$listPath$files[$listoption]";
            print "Opening file $selectedFile\n";
 
            $fileOption = "";
            while( $fileOption ne "q" && $fileOption ne "Q" )
            {
                listHelp();
                print "$selectedFile > ";
                $fileOption = <>;
                chomp $fileOption;
                if($fileOption eq "q")
                {
                    break;
                }
                elsif($fileOption eq "l")
                {
                    print "The following items were found:\n";
                    listItems();
                }
                elsif($fileOption eq "a")
                {
                    addItems();
                }
                elsif($fileOption eq "c")
                {
                    clearList();
                }
                elsif($fileOption eq "d")
                {
                    deleteItems();
                }
                else
                {
                    print "'$fileOption' is an unknown option.\n";
                }
 
            }
        }
 
    }
 
    sub listHelp
    {
        sort $listHelp;
        foreach my $key( sort keys(%listHelp) )
        {
            print( "\t$key    $listHelp{$key}\n");
        }
    }
    
    sub deleteItems()
    {
        @items = listItems();
        $itemSelection = "";
        while($itemSelection ne "q")
        {
            print "Which items would you like to delete? [ c=commit changes, q=quit ] ";
            $itemSelection = <>;
            chomp $itemSelection;
            if(lc("$itemSelection") eq "q")
            {
                break;
            }
            elsif(lc("$itemSelection") eq "c")
            {
                open(LIST, ">$selectedFile") || die ("Unable to open list. $!");
                for($i=0; $i < @items; $i++)
                {
                    print LIST "$list[$i][0]    $list[$i][1]\n";
                }
                close(LIST);
                print "File written.";
            }
            elsif(lc("$itemSelection") eq "l")
            {
            
                for($i=0; $i < @items; $i++)
                        {
                              #items are arranged as follows
                               #destdir        <tab>   URL
                                      chomp $newitems[$i][0];
                                     chomp $newitems[$i][1];
                                    print "\t$i)    $newitems[$i][0]        $newitems[$i][1]\n";
                                }
                        }
            elsif($itemSelection =~/([\d]*)-([\d]*)/)
            {
                print ( "1 = $1, 2 = $2");
                $start = $1;
                $length = $2-$1;
            }
            elsif($itemSelection >= 0 && $itemSelection < @items && $itemSelection=~/^[\d]*$/)
            {
                $start = $itemSelection;
                $length = 1;
            }
            else
            {
                print ("skipping\n");
                next;
            }
            print ("Start = $start, length = $length\n");
            splice(@items,$start,$length);
        }
    }
 
    sub listItems
    {
        open(LIST,$selectedFile) || die("Unable to open list.$!");
        @items = <LIST>;
        
        for($i=0; $i < @items; $i++)
        {
            #items are arranged as follows
            #destdir    <tab>    URL
            @newitems[$i] = [split(/    /,$items[$i])];
            chomp $newitems[$i][0];
            chomp $newitems[$i][1];
            print "\t$i)    $newitems[$i][0]    $newitems[$i][1]\n";
        }
        close LIST;
        return @items;
    }
 
    sub addItems
    {
        print "Adding to $selectedFile.\n";
        print "Where should these files be stored? $downloadBase";
        $toDir = <>;
        chomp($toDir);
        $url = "";
        while($url ne "q")
        {
            print "Please enter a URL to download\n";
            $url = <>;
            chomp $url;
            if($url ne "q" && $url ne "Q")
            {
                open(DALIST, ">>$selectedFile") || die("Unable to open $selectedFile. $!");
                if(!print DALIST "$toDir    $url\n")
                {
                    warn "Unable to write to $selectedFile. $!";
                }
                close(DALIST);
            }
        }
    }
            
    sub clearList
    {
        $answer="";
        while( "$answer" ne "y" && "$answer" ne "n")
        {
            print "Are you sure you want to delete all entries in $selectedFile? [n/y]";
            $answer = lc(<>);
            chomp $answer;
        }
        if("$answer" eq "y")
        {
            open(LIST,">$selectedFile");
            print LIST "";
            print "List cleared\n";
        }
        else
        {
            print "Nothing was changed\n";
        }
    }