綾小路龍之介の素人思考

ソース[2008年 11月 25日 火曜日 19:50:49 JST]

とりあえず下のような感じ。保存してつかってくれい。使い方は上に書いたぞよ。スクリプトの版はこのセクションのタイトルに書いたぞよ。

#!/bin/bash -vx
# Function to get filesize
function FileSize ()
{
  if [ -f "$1" ] ; then
    SIZE=`ls -ld "$1" | awk '{print $5}'`
    echo $SIZE
  else
    echo 0
  fi
}
# Function to output an error message and terminate
function ErrorMessage ()
{
  echo -e "Error: $1"
  exit 1
}
function usage() {
        local CMDNAME=`basename "$0"`
        echo "Usage ${CMDNAME} -u \"URI\" " 1>&2
}
USER_AGENT='Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)'
URI="FALSE"
URIVALUE=
OPT=
while getopts "u:" OPT
do
        case ${OPT} in
                "u" )   URI="TRUE"
                        URIVALUE="${OPTARG}" ;;
                * ) usage
                        exit 1 ;;
        esac
done
if [ "${URI}" != "TRUE" ] ; then
        usage
        exit 2
fi
CURL='curl -sSv --retry 10'
CURL='curl -sSv --retry 10 -0 -C -'
CURL='curl -sSv --retry 10 -0'
#WGET='wget -t 10 -Sd --keep-session-cookies'
ITERATION=10
URL1=''
FILE=''
i=0
while [ -z "${URL1}" -o -z "${FILE}" ];
do
        ${CURL} -A "${USER_AGENT}" -o tmp2.$$ -c cookie2.$$ ${URIVALUE}
        cat tmp2.$$ | grep -e 'param.*embed' > tmp0.$$
        cat tmp0.$$ | cut -d "&" -f3 > tmp0.$$
        cat tmp0.$$ | cut -d "=" -f2 > tmp0.$$
        cat tmp0.$$ | sed 's/%3A/:/g;s/%2F/\//g;s/%3F/?/g;s/%26/\&/g;s/%3D/=/g' > tmp0.$$
        URL1=$(cat tmp0.$$)
        cat tmp0.$$ | cut -d "&" -f5 > tmp0.$$
        cat tmp0.$$ | cut -d "=" -f2 > tmp0.$$
        cat tmp0.$$ | sed 's/%3A/:/g;s/%2F/\//g;s/%3F/?/g;s/%26/\&/g;s/%3D/=/g' > tmp0.$$
        FILE=$(cat tmp0.$$)
        if [ ${i} -lt ${ITERATION} ];
        then
                i=`expr $i + 1`
                sleep 1
        else
                rm tmp2.$$ cookie2.$$ tmp0.$$ tmp3.$$ cookie3.$$
                exit 1
        fi
done
echo "# Successfully set URL1 of path to file including path to flash video and FILE meaning filename"
echo $URL1
echo $FILE
if [ -s "${FILE}" ];
then
        rm tmp2.$$ cookie2.$$ tmp0.$$ tmp3.$$ cookie3.$$
        exit 1
else
        rm -f ${FILE}
fi
URL=''
i=0
while [ -z "${URL}" ];
do
        ${CURL} -A "${USER_AGENT}" -o tmp3.$$ -c cookie3.$$ ${URL1}
        cat tmp3.$$ | cut -d "&" -f1 > tmp0.$$
        cat tmp0.$$ | cut -d "=" -f2 > tmp0.$$
        cat tmp0.$$ | sed 's/%3A/:/g;s/%2F/\//g;s/%3F/?/g;s/%26/\&/g;s/%3D/=/g' > tmp0.$$
        URL=$(cat tmp0.$$)
        cat tmp3.$$
        if [ ${i} -lt ${ITERATION} ];
        then
                i=`expr $i + 1`
                sleep 1
        else
                rm tmp2.$$ cookie2.$$ tmp0.$$ tmp3.$$ cookie3.$$
                exit 1
        fi
done
echo "# Successfully set URL to flash video"
echo $URL
FILESIZE=''
i=0
while [ -z "${FILESIZE}" ];
do
        ${CURL} -A "${USER_AGENT}" -b cookie3.$$ ${URL} -I 2> /dev/null | sed -e "s/^$//" > "z_${FILE}.header"
        FILESIZE="`grep -i '^\(Content-Length: \)\?[0-9]\+' "z_${FILE}.header" | sed 's/^\(Content-Length: \)\?\([0-9]\+\)/\2/i'`"
        if [ ${i} -lt ${ITERATION} ];
        then
                i=`expr $i + 1`
                sleep 1
        else
                rm tmp2.$$ cookie2.$$ tmp0.$$ tmp3.$$ cookie3.$$
                exit 1
        fi
done
echo "# Successfully set FILESIZE of flash video"
echo ${FILESIZE}
NUM=10
FILESIZE="`echo ${FILESIZE} | sed 's/\r//g;s/\n//g;'`"
WIDTH_BYTE=`expr ${FILESIZE} / ${NUM}`
START_BYTE=0
END_BYTE=${WIDTH_BYTE}
SLEEP_SEC=1;
i=0;
while [ ${i} -lt ${NUM} ];
do
        TEMP_FILE="${FILE}.$i"
        START_BYTE="`expr ${WIDTH_BYTE} '*' ${i}`"
        if [ ${i} -lt `expr ${NUM} - 1` ];
        then
                END_BYTE="`expr ${START_BYTE} + ${WIDTH_BYTE} - 1`"
        else
                END_BYTE=`expr ${FILESIZE} - 1`
        fi
        FILE_BYTE=`expr ${END_BYTE} - ${START_BYTE} + 1`
        TEMP_FILE_BYTE=0;
        (
        while [ ${FILE_BYTE} -ne ${TEMP_FILE_BYTE} ];
        do
                echo "$i / ${NUM} : byte range ${START_BYTE}-${END_BYTE} ${FILE_BYTE}"
                ${CURL} -A "${USER_AGENT}" --range ${START_BYTE}-${END_BYTE} -o ${TEMP_FILE} -b cookie3.$$ ${URL}
                TEMP_FILE_BYTE="`FileSize "${TEMP_FILE}"`"
                sleep ${SLEEP_SEC}
        done
        echo "$i / ${NUM} : byte range ${START_BYTE}-${END_BYTE} ${FILE_BYTE} end"
        )&
        i=`expr $i + 1`
done
echo "Waiting for cURLs to finish..."
wait
echo
echo "Assembling ${FILE}... "
i=0;
while [ ${i} -lt ${NUM} ];
do
        TEMP_FILE="${FILE}.$i"
        echo "cat ${TEMP_FILE} >> ${FILE}"
        cat ${TEMP_FILE} >> ${FILE}
        echo rm ${TEMP_FILE}
        rm ${TEMP_FILE}
        i=`expr $i + 1`
done
rm tmp2.$$ cookie2.$$ tmp0.$$ tmp3.$$ cookie3.$$
exit

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2006-03-31T17:38:46+09:00
  2. Modified: 2006-03-31T10:26:52+09:00
  3. Generated: 2017-09-13T23:10:55+09:00