使用CURL来自动登录并下载东西

要利用curl,关键就是要首先登录,获得cookie,然后才能下载文件。这样就可以利用CURL做很多自动化的脚本或是程序了。关键点是这么几个:

1. 要分析网站的登录form,看需要提供哪些信息。然后组织curl命令行。
2. curl -d表示一个post field。
3. curl -b 指定一个cookie,-c 将得到的cookie写入一个文件。
4. BASH中,Arithmetic expression要用((...)),其他的用[...]
5. BASH中,&&, || 可以用来连接多个条件表达式

以自动下载TorrentGUI, HD001和CHDBits的种子文件为例,脚本是这样的:

#!/bin/bash
# Customize your login info here

TGUSERNAME=xxxxxxx
TGPASSWORD
=xxxxxxxxxxx
HD001USERNAME
=xxxxxxxx
HD001PASSWORD
=xxxxxxxxxxx
CHDUSERNAME
=xxxxxxxxxxx
CHDPASSWORD
=xxxxxxxxxxx

# Download root directory, DON'T ADD a SLASH at the end of string
DOWNROOTDIR=~/rtorrent/torrents

# take care of the arguments
if [ $# -lt 1 ]; then
    echo "Usage: $0 <tg|hd001|chd> <torrent1 url> <category1> <file rename 1> ... <torrentN url> <categoryN> <file rename N>"
    
exit 1
fi

site
=$1
if [ "$site" != "tg" ] && [ "$site" != "hd001" ] && [ "$site" != "chd" ]; then
    echo 
"Site must be tg|hd001|chd, Check the command usage."
    
exit 1
fi
# Ignore site arg, let's face <torrent url>, <category> and <file rename>
shift

# Check arguments
declare -i args_num
let args_num
=$#
if ((args_num==0)) || ((args_num%3 != 0)) ; then
    echo 
"Torrent URL, category and file rename infos are incorrect. Check the script usage."
    
exit 1
fi

# Check whether curl is ready
if [ -z $(which curl) ]; then
    echo 
"Can't find curl. Install it and try again..."
    
exit 1
fi

# OK, let's roll
if [ "$site" == "tg" ]; then
    echo 
"====== Login torrentgui... ======"
    curl 
-d forward="" -d jumpurl="http://torrentgui.com/bbs/index.php" -d step=2 -d lgt=0 -d pwuser=${TGUSERNAME} -d pwpwd=${TGPASSWORD} -d hideid=0 -d cktime=3600 -c tg.cookie "http://torrentgui.com/bbs/login.php?"
    echo 
"====== Try to download torrent file... ======"

    
while [ $# -gt 0 ]; do
        echo "====== Handle torrent file $3... ======"
        curl 
-b tg.cookie "$1" > ${DOWNROOTDIR}/${2}/[TorrentGUI]${3}.torrent
        
shift 3
    done

    rm 
-f tg.cookie
fi

if [ "$site" == "hd001" ]; then
    echo 
"====== Login hd001... ======"
    curl 
-d username=${HD001USERNAME} -d password=${HD001PASSWORD} -c hd001.cookie "http://www.hd001.org/takelogin.php"
    echo 
"====== Try to download torrent file... ======"

    
while [ $# -gt 0 ]; do
        echo "====== Handle torrent file $3... ======"
        curl 
-b hd001.cookie "$1" > ${DOWNROOTDIR}/${2}/[HD001]${3}.torrent
        
shift 3
    done

    rm 
-f hd001.cookie
fi

if [ "$site" == "chd" ]; then
    echo 
"====== Login CHDBits... ======"
    curl 
-d username=${CHDUSERNAME} -d password=${CHDPASSWORD} -c chdbits.cookie "https://chdbits.org/takelogin.php"
    echo 
"====== Try to download torrent file... ======"

    
while [ $# -gt 0 ]; do
        echo "====== Handle torrent file $3... ======"
        curl 
-b chdbits.cookie "$1" > ${DOWNROOTDIR}/${2}/[CHD]${3}.torrent
        
shift 3
    done

    rm 
-f chdbits.cookie
fi

echo 
"Done, Bye."
 一点遗憾的是,没找到CURL的option,来自动给出下载文件的文件名。所以脚本中要求使用者自己给下载的文件取个名字。想起来CURL应该是有这样的功能的,哪位要是知道留个comment下来哦。

BTW,请支持PT。

原文地址:https://www.cnblogs.com/super119/p/1904406.html