Skip to content

Instantly share code, notes, and snippets.

@tlan16
Last active August 8, 2018 10:48
Show Gist options
  • Save tlan16/103a6eca7394fb6d5615182d461748ee to your computer and use it in GitHub Desktop.
Save tlan16/103a6eca7394fb6d5615182d461748ee to your computer and use it in GitHub Desktop.
automatically download via ykdl
#!/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)=============="
@tlan16
Copy link
Author

tlan16 commented Feb 11, 2018

Intension

  • Automatically download live streaming when available.
  • Unattended download by cron job

Requirements

  • ykdl
    For dependencies required by ykdl, please refer to their git page.
  • jq
  • wget
  • bash version higher than 3

Cronjob example

chmod +x ykdl_auto.sh
touch /tmp/ykdl_auto.log

* * * * * /home/frank/download/ykdl_auto.sh -o /home/frank/download >> /tmp/ykdl_auto.log 2>&1

  • replace /home/frank/download with your prefered download path
  • replace /home/frank/download/ykdl_auto.sh with the script full path
  • logging is optional

Tested on

  • Ubuntu 17.10 on Google cloud
  • Centos 7 on Ali Cloud
  • Python 3

FAQ

  • python 2
    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.
  • unicode issue
    In case you get unicode error thrown by python, please try uninstall ykdl pip3 uninstall ykdl, fix your system language pack sudo apt install language-pack-zh-hans language-pack-zh-hant and reinstall ykdl pip3 install ykdl.

Change Log

  • 12 March 2018
    updated jq selection method to get the first element in streams instead of hardcoded key.

TODO

  • Remove the hardcoded list of streamers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment