howto: Download manager based on CUPS
 

Below is the basic decision (proof of concept) to build a download manager based on the printing system CUPS.

This example shows the principle of building such systems, ie, systems based on the processing queue assignments. These systems can be discussed below download manager, as well as automatic music player, that play music files in the queue sent by users, perhaps even taking into account the priorities and permissions of users and so on.

Example is based on GNU/Linux Debian Etch and CUPS version 1.2.7 of the distribution.

Steps:

1. create a new backend for CUPS (call it "cwget")

# vi /usr/lib/cups/backend/cwget

#!/bin/sh
 
if [ $# -eq 0 ]; then
        echo 'cups wget "Unknown" "Cups wget downloader"'
        exit 0
fi
 
#default download directory
DOWNLOAD_DIR=/var/tmp
 
umask 0
TMPFILE=/tmp/cwget$$
cat "$6" > $TMPFILE
/bin/chmod +r $TMPFILE
 
/usr/bin/sudo -H -u $2 /usr/local/sbin/cwget.sh "$DOWNLOAD_DIR" "$TMPFILE"
 
rm /tmp/cwget$$


do not forget to make it executable:

# chmod +x /usr/lib/cups/backend/cwget

2. As the processes run by the CUPS's user  "lp", configure sudo to run the script /usr/local/sbin/cwget.sh without a password:

# visudo

    lp      ALL=(ALL) NOPASSWD:/usr/local/sbin/cwget.sh

3. Here the script that download file on behalf of the user submitting the job (actually URL) to "print":

# vi /usr/local/sbin/cwget.sh

#!/bin/sh
DOWNLOAD_DIR=$1
cd
[ -f ".cupswgetrc" ] && . ".cupswgetrc"
mkdir -p "$DOWNLOAD_DIR"
cd "$DOWNLOAD_DIR"
/usr/bin/wget -nc -i "$2" >/dev/null 2>&1


do not forget to make it executable:

# chmod +x /usr/local/sbin/cwget.sh

4. In the home directory of the user create a configuration file, in fact, there was a script (this script will be executed with user rights, see step 3):

$ vi ~/.cupswgetrc

DOWNLOAD_DIR=/home/sergey/Downloads

#define proxy if necessary

export http_proxy='http://proxy.mydomain.com:3128'
export  ftp_proxy='http://proxy.mydomain.com:3128'

# your proxy require user authentication?

#export http_proxy='http://youruser:yourpassword@proxy.mydomain.com:3128'
#export  ftp_proxy='http://youruser:yourpassword@proxy.mydomain.com:3128'

 5. Now configure "printer" in CUPS or even "printers" and combine them in class to provide simultaneous download more than one job.

This can be done through the web interface of CUPS or manually editing the file /etc/cups/printers.conf

 (Click on the picture to see a big one)

# cat /etc/cups/printers.conf

# Printer configuration file for CUPS v1.2.7
# Written by cupsd on 2008-05-05 12:00
<Printer pcwget1>
Info pcwget1
DeviceURI cwget:/dev/null
State Idle
StateTime 1210003143
Accepting Yes
Shared Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy stop-printer
</Printer>
<Printer pcwget2>
Info pcwget2
DeviceURI cwget:/dev/null
State Idle
StateTime 1210003143
Accepting Yes
Shared Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy stop-printer
</Printer>
<Printer pcwget3>
Info pcwget3
DeviceURI cwget:/dev/null
State Idle
StateTime 1210003143
Accepting Yes
Shared Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy stop-printer
</Printer>
<Printer pcwget4>
Info pcwget4
DeviceURI cwget:/dev/null
State Idle
StateTime 1210003143
Accepting Yes
Shared Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy stop-printer
</Printer>

 

As has been said, you can create a class (pool) "printers" - this allows you to upload multiple tasks simultaneously.

(Click on the picture to see a big one)

# cat /etc/cups/classes.conf

# Class configuration file for CUPS v1.2.7
# Written by cupsd on 2008-05-05 12:00
<Class pcwget>
Info
Location
State Idle
StateTime 1210003233
Accepting Yes
Shared Yes
JobSheets none none
Printer pcwget1
Printer pcwget2
Printer pcwget3
Printer pcwget4
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy retry-job
</Class>


6. All remaining to send the job to download:

You can do this in different ways, for example, from the console

$ echo 'http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.25.1.tar.bz2' | lpr -Ppcwget

The file is downloaded in the directory  /home/sergey/Downloads

 

 

go home