Last active
August 8, 2018 10:48
-
-
Save tlan16/103a6eca7394fb6d5615182d461748ee to your computer and use it in GitHub Desktop.
automatically download via ykdl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
echo "$(date)" | |
PATH="/home/frank/.local/bin:/usr/local/bin:$PATH" | |
# get arguments | |
INFO_ONLY=0 | |
while getopts ":o:i" opt; do | |
case ${opt} in | |
o) DIR="$OPTARG" | |
;; | |
i) INFO_ONLY=1 | |
;; | |
esac | |
done | |
if [ ${INFO_ONLY} -ne 1 ] && [ -z "$DIR" ]; then | |
echo "USAGE: -o <output directory>" | |
exit 1 | |
fi | |
# check dependencies | |
JQ="$(command -v jq)" | |
if ! [ -x "${JQ}" ]; then | |
echo 'Error: jq is not installed.' >&2 | |
exit 1 | |
fi | |
YKDL="$(command -v ykdl)" | |
if ! [ -x "${YKDL}" ]; then | |
echo 'Error: ykdl is not installed.' >&2 | |
exit 1 | |
fi | |
WGET="$(command -v wget)" | |
if ! [ -x "${WGET}" ]; then | |
echo 'Error: wget is not installed.' >&2 | |
exit 1 | |
fi | |
if [ "${BASH_VERSINFO}" -lt 3 ]; then | |
echo 'Error: Bash version above 3 is required.' >&2 | |
exit 1 | |
fi | |
URLS=( \ | |
'https://www.quanmin.tv/6969' \ | |
'https://www.quanmin.tv/1069' \ | |
'http://www.huya.com/xuanmo' \ | |
'https://www.huya.com/14090756' \ | |
'https://live.bilibili.com/569' \ | |
) | |
getInfoJson() | |
{ | |
INFO_JSON=`"$YKDL" -J "$URL" 2> /dev/null` | |
} | |
getRealUrl() | |
{ | |
REAL_URL=`echo ${INFO_JSON} 2> /dev/null | ${JQ} --raw-output 'first(first(.streams[]).src[])'` | |
} | |
initPrint() | |
{ | |
header="\n %-50s %10s\n" | |
format=" %-50s %10s\n" | |
width=64 | |
divider===================================== | |
divider=$divider$divider | |
printf "$header" "URL" "STATUS" | |
printf "%$width.${width}s\n" "$divider" | |
} | |
isLive() | |
{ | |
LIVE=0 | |
getInfoJson | |
if [ -n "${INFO_JSON}" ]; then | |
getRealUrl | |
# check url exist | |
if [ -n "${REAL_URL}" ]; then | |
if [[ $URL = *"bilibili"* ]]; then # some url is not hitable | |
LIVE=1 | |
else | |
if ${WGET} --spider ${REAL_URL} 2>/dev/null ; then | |
LIVE=1 | |
fi | |
fi | |
fi | |
fi | |
} | |
initPrint | |
for URL in "${URLS[@]}" | |
do | |
isLive | |
if [ ${LIVE} -eq 1 ]; then | |
IS_RUNNING=`ps -ef | grep ${URL} | grep -v "grep" | wc -l` | |
if [ ${IS_RUNNING} -eq 0 ]; then | |
if [ ${INFO_ONLY} -eq 1 ]; then | |
printf "$format" ${URL} "live" | |
else | |
${YKDL} -o ${DIR} ${URL} >> /tmp/ykdl_auto.log & | |
printf "$format" ${URL} "start download" | |
fi | |
else | |
printf "$format" ${URL} "already downloading" | |
fi | |
else printf "$format" ${URL} "offline" | |
fi | |
done | |
echo "==============END=$(date)==============" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Intension
Requirements
For dependencies required by ykdl, please refer to their git page.
Cronjob example
* * * * * /home/frank/download/ykdl_auto.sh -o /home/frank/download >> /tmp/ykdl_auto.log 2>&1
/home/frank/download
with your prefered download path/home/frank/download/ykdl_auto.sh
with the script full pathTested on
FAQ
ykdl
has known issue with Python 2, which is mainly due to the poor multilanguage support of python 2. But this could be due to a non-utf8 system setting.In case you get unicode error thrown by python, please try uninstall ykdl
pip3 uninstall ykdl
, fix your system language packsudo apt install language-pack-zh-hans language-pack-zh-hant
and reinstall ykdlpip3 install ykdl
.Change Log
updated jq selection method to get the first element in streams instead of hardcoded key.
TODO