Automatically search and download arbitrary number of files

From AMule Project FAQ
Jump to: navigation, search

This short document will describe the setup of the aMule and provide a shell script which will allow you to download all of the files you need with almost no effort needed.

1. Have amule installed

Obviously, you're gonna need that one.

2. Have amule-utils installed

The shell script which does all the work for you uses the amulecmd utility to add ed2k links to aMule downloads.

3. Set up aMule, your firewall, whatever...

Find a way (if it's not working out of the box) to setup your aMule so that you can manually (the ordinary way) search for and download the files you need.

4. Setup aMule Remote Controls

a. Open Preferences dialog
b. In the left pane select "Remote Controls"
c. Check the "Accept external connections" checkbox
d. Choose (or leave unchanged) TCP port, and write down that number somewhere
e. Set password (and remember it) for remote connection in the Password field
f. OK
Now you should have everything you need to successfully use the script you are about to download.

5. Download this shell script

a. Open the text editor of your preference
b. Copy&paste the script bellow
c. Save the file as 'amuleget.sh' to your local directory or to /usr/bin to make it available to all users
d. Set appropriate read/write permissions (when needed) and allow execution
Note: In most cases it will be enough to do: chmod +x amuleget.sh
e. Here comes the script:
#!/bin/bash
if [ -z "$1" ] || [ "$1" = "--help" ]; then
       if [ -z "$1" ]; then
               echo "Not enough parameters!"
       fi
       echo -e "Usage: $0 [--help] [-p port] [-P password] file"
       echo -e "   --help\t\tShow this help text"
       echo -e "   -p port\t\tTCP port for aMule remote connection"
       echo -e "   -P password\t\tpassword for aMule remote connection"
       echo -e "   -v level\t\tverbosity level:"
       echo -e "           \t\t   0 - (default) suppress wget and amulecmd output"
       echo -e "           \t\t   1 - suppress wget output only"
       echo -e "           \t\t   2 - suppress amulecmd output only"
       echo -e "           \t\t   3 - show both wget and amulecmd outputs"
       echo -e "   file\t\t\tfile containing list of items to be downloaded"
       echo -e "\nTip 1"
       echo -e "To find out the values for the -p and -P parameters, in your aMule graphical client open Preferences -> Remote Controls and in External Connection Parameters part set (if unset or unknown) the password and write down the TCP port."
       echo -e "Tip 2"
       echo -e "If the scripts hangs at \"Creating client...\" the most probable causes are unchecked 'Accept external connections' checkbox, incorrect password, incorrect TCP port or aMule instance not running"
       echo -e "Tip 3"
       echo -e "Read Wiki at http://www.amule.org/wiki/index.php/Automatically_search_and_download_arbitrary_number_of_files"
       exit
fi
eval fileName=\$$#
if [ ! -f "$fileName" ]; then
       echo -e "File \"$fileName\" doesn't exist"
       exit
fi


echo    "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo -e "!!!!!!Before continuing make sure you have a running instance of aMule!!!!!!"
echo    "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"


#parse arguments
while getopts ":p:P:v:" options; do
       case $options in
               p ) aMulePort=$OPTARG;;
               P ) aMulePass=$OPTARG;;
               v ) verbose=$OPTARG;;
       esac
done
if [ -z "$aMulePort" ]; then
       echo -e "\nWhat is the TCP port for aMule remote connection"
       read aMulePort
fi


if [ -z "$aMulePass" ]; then
       echo -e "\nPlease, provide password for aMule remote connection"
       read -s aMulePass
fi
echo "Initiating aMule connection to server"
amulecmd -p $aMulePort -P $aMulePass -c connect
echo "Searching for ed2k links..."
#file descriptor
exec 5< $fileName
searchStringLeft="http://www.emugle.com/search.php?q="
searchStringRight="&t=All+categories&Submit=Search&f=0"
tmpFile=`mktemp /tmp/aMuleTmp.XXXXXX`
#process each line
until [ $done ]; do
       read <&5 searchItem
       if [ $? != 0 ]; then
               #end of file reached
               done=1
               continue
       fi
       if [ "$searchItem" = "" ]; then
               #empty line
               continue
       fi
       echo -e "    SEARCHING:\t$searchItem"
       searchItem=`echo $searchItem | sed -e 's/\ /+/g'`
       searchString="$searchStringLeft$searchItem$searchStringRight"
       if [ "$verbose" = "2" ] || [ "$verbose" = "3" ]; then
               wget "$searchString" -O $tmpFile
       else
               wget "$searchString" -o /dev/null -O $tmpFile
       fi
       firstResult=`grep -m 1 ed2k $tmpFile`
       #remove HTML tags and spaces
       ed2kLink=`echo $firstResult | sed -e 's/<[^>]*>//g'| sed -e 's/\ //g'`
       echo -e "    ADDING:\t$searchItem to aMule. Ed2k link:\n  $ed2kLink\n"
       if [ "$verbose" = "1" ] || [ "$verbose" = "3" ]; then
               amulecmd -p $aMulePort -P $aMulePass -c "add $ed2kLink"
       else
               amulecmd -p $aMulePort -P $aMulePass -c "add $ed2kLink" > /dev/null
       fi
done

rm -f $tmpFile

6. Create the file with a list of the search items

You should put each search item on it's own line and the keywords should be separated by spaces, for instance:
keywords for 1st search
keywords for 2nd search
keywords for 3rd search
...

7. And finally...

a. run aMule
b. amuleget.sh --help to check the usage
Examples:
1. script in a current directory, file with search items: /tmp/listFile
./amuleget.sh -p 5020 -P myWeakPassword /tmp/listFile
2. script in /usr/bin, file with search items in current directory, enter port and password through dialog
amuleget.sh listFile

8. Reporting the problems

You can always ask for the help on the http://forum.amule.org.

If I've made a typo or any other error which makes this wiki unusable for the average user, you can correct it yourself or try to post a question or report a problem at the original thread at the aMule forum: http://forum.amule.org/index.php?topic=12413.0
I will check it occasionally.

9. Enjoy the downloading...