偶然看到的一个脚本,可以自动下载B站收藏夹里的视频,稍微改了下拿来用了

github:https://github.com/Left024/BiliFavoritesDownloader

作者博客:https://left.pink/archives/3073

做了以下更改

  • 替换了自己部署的rsshub服务(可以控制缓存时间)
  • 如果视频标题有斜杠就转换为反斜杠(因为路径不能含有斜杠)
  • 修改cookies验证分辨率判断更改为1080p(因为没大会员)
  • 增加根据不同收藏夹下载到对应文件夹
  • 稍微按个人喜好修改了文件名和路径

原脚本备份:BiliFavoritesDownloader-main.zip

修改后bili.sh脚本

#!/bin/bash
you=/usr/local/bin/you-get
#telegram参数
telegram_bot_token=""
telegram_chat_id=""
#RSS 地址
rssURL="http://127.0.0.1:1200/bilibili/fav/$1/$2"
#脚本存放地址
scriptLocation="/home/downloads/bili/"
#视频存放地址
videoLocation="/home/downloads/bili/$3/"
#邮件地址
mailAddress=""
#抓取rss更新
content=$(wget $rssURL -q -O -)
#获得时间戳
subpubdate=${content#*<pubDate>}
pubdate=${subpubdate%%</pubDate>*}
#获得视频标题
content1=${content#*<item>}
content1=${content1//\//\\} #如果视频标题有斜杠就全部替换成反斜杠
subname=${content1#*\[CDATA\[}
name=${subname%%\]\]>*}
#如果时间戳记录文本不存在则创建(此处文件地址自行修改)
if [ ! -f "$scriptLocation$2"_date.txt ]; then
echo 313340 >"$scriptLocation$2"_date.txt
fi
#如果标题记录文本不存在则创建
if [ ! -f "$scriptLocation$2"_title.txt ]; then
echo 313340 >"$scriptLocation$2"_title.txt
fi
#获得之前下载过的视频标题
oldtitle=$(cat "$scriptLocation$2"_title.txt)
#获得上一个视频的时间戳(文件地址自行修改)
olddate=$(cat "$scriptLocation$2"_date.txt)
#此处为视频存储位置,自行修改
filename="$videoLocation$name"
#aaaaa="GMT"
result=$(echo $pubdate | grep "GMT")
result5=$(echo $oldtitle | grep "$name")
#echo $result
#判断当前时间戳和上次记录是否相同,不同则代表收藏列表更新
if [ "$pubdate" != "$olddate" ] && [ "$result" != "" ] && [ "$result5" = "" ]; then
#Cookies可用性检查
stat=$($you -i -l -c "$scriptLocation"cookies.txt https://www.bilibili.com/video/BV1fK4y1t7hj)
substat=${stat#*quality:}
data=${substat%%#*}
quality=${data%%size*}
if [[ $quality =~ "1080P" ]]; then
#清空 Bilibili 文件夹
rm -rf "$videoLocation"*
#获得视频下载链接
sublink=${subpubdate#*<link>}
link=${sublink%%</link>*}
av=${link#*video/}
#获得封面图下载链接和文件名称
subcontent=${content#*<img src=\"}
photolink=${subcontent%%\"*}
pname=${photolink#*archive/}
#下载封面图(图片存储位置应和视频一致)
wget -P "$videoLocation$name" $photolink
#记录时间戳
echo $pubdate >"$scriptLocation$2"_date.txt
#记录标题
echo $name >>"$scriptLocation$2"_title.txt
#获取视频清晰度以及大小信息
stat=$($you -i -l -c "$scriptLocation"cookies.txt $link)
#获取最高清晰度format
subformat=${stat#*--format=}
format=${subformat%%\ \[URL\]*}
#有几P视频
count=$(echo $stat | awk -F‘title‘ ‘{print NF-1}‘)
#echo $count
for ((i = 0; i < $count; i++)); do
stat=${stat#*title:}
title=${stat%%streams:*}
substat=${stat#*quality:}
data=${substat%%#*}
quality=${data%%size*}
size=${data#*size:}
title=$(echo $title)
quality=$(echo $quality)
size=$(echo $size)
#每一P的视频标题,清晰度,大小,发邮件用于检查下载是否正确进行
#message=${message}"Title: "${title}$‘\n‘"Quality: "${quality}$‘\n‘"Size: "${size}$‘\n\n‘ #邮件方式
message=${message}"Title:%20"${title}"%0AQuality:%20"${quality}"%0ASize:%20"${size}"%0A%0A" #telegram方式
done
#发送开始下载邮件(自行修改邮件地址)
#echo "$message" | mail -s "BFD:开始下载" $mailAddress
curl "https://api.telegram.org/bot$telegram_bot_token/sendMessage?chat_id=$telegram_chat_id&parse_mode=html&text=<b>BFD:开始下载</b>%0A%0A$message"
#下载视频到指定位置(视频存储位置自行修改;you-get下载B站经常会出错,所以添加了出错重试代码)
count=1
while true; do
$you --format=$format -l -c "$scriptLocation"cookies.txt -o "$videoLocation$name" $link >"$scriptLocation"z.txt #如果是邮件通知,删除 > z.txt
if [ $? -eq 0 ]; then
#下载完成
#重命名封面图
result1=$(echo $pname | grep "jpg")
if [ "$result1" != "" ]; then
mv "$videoLocation$name"/$pname "$videoLocation$name"/poster.jpg
else
mv "$videoLocation$name"/$pname "$videoLocation$name"/poster.png
fi
#xml转ass && 获取下载完的视频文件信息
for file in "$videoLocation$name"/*; do
if [ "${file##*.}" = "xml" ]; then
"${scriptLocation}"DanmakuFactory -o "${file%%.cmt.xml*}".ass -i "$file"
#删除源文件
#rm "$file"
elif [ "${file##*.}" = "mp4" ] || [ "${file##*.}" = "flv" ] || [ "${file##*.}" = "mkv" ]; then
videoname=${file#*"$name"\/}
videostat=$(du -h "$file")
videosize=${videostat%%\/*}
videosize=$(echo $videosize)
#videomessage=${videomessage}"Title: "${videoname}$‘\n‘"Size: "${videosize}$‘\n\n‘  #邮件方式
videomessage=${videomessage}"Title:%20"${videoname}"%0ASize:%20"${videosize}"%0A%0A" #telegram方式
fi
done
#发送下载完成邮件
#echo "$videomessage" | mail -s "BFD:下载完成" $mailAddress
curl "https://api.telegram.org/bot$telegram_bot_token/sendMessage?chat_id=$telegram_chat_id&parse_mode=html&text=<b>BFD:下载完成</b>%0A%0A$videomessage"
#上传至OneDrive
/usr/bin/rclone copy "$videoLocation" "od:/emby/media/bili/$3"
#发送通知
#echo "$title" | mail -s "BFD:上传完成" $mailAddress #邮件方式
curl "https://api.telegram.org/bot$telegram_bot_token/sendMessage?chat_id=$telegram_chat_id&parse_mode=html&text=<b>BFD:上传完成</b>%0A%0A$title"
break
else
if [ "$count" != "1" ]; then
count=$(($count + 1))
sleep 2
else
rm -rf "$videoLocation$name"
#发送通知
#echo "$name" | mail -s "BFD:下载失败" $mailAddress  #邮件
curl "https://api.telegram.org/bot$telegram_bot_token/sendMessage?chat_id=$telegram_chat_id&parse_mode=html&text=<b>BFD:下载失败</b>"
exit
fi
fi
done & #如果是邮件通知,删除 & 和下面的内容(删到wait,fi保留)
second="start"
secondResult=$(curl "https://api.telegram.org/bot$telegram_bot_token/sendMessage?chat_id=$telegram_chat_id&text=$second")
subSecondResult="${secondResult#*message_id\":}"
messageID=${subSecondResult%%,\"from*}
ccount=0
while true; do
sleep 2
text=$(tail -1 "$scriptLocation"z.txt)
#subtext="${text#*z.txt}"
#subtext=$(echo $subtext)
echo $text >"$scriptLocation"zz.txt
sed -i -e ‘s/\r/\n/g‘ "$scriptLocation"zz.txt
text=$(sed -n ‘$p‘ "$scriptLocation"zz.txt)
result=$(curl -g "https://api.telegram.org/bot$telegram_bot_token/editMessageText?chat_id=$telegram_chat_id&message_id=$messageID&text=$text")
aa="{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message\"}"
bb="{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: message text is empty\"}"
if [ "$result" == "$aa" ] || [ "$result" == "$bb" ]; then
#echo "break"
if [ "$ccount" != "10" ]; then
ccount=$(($ccount + 1))
else
break
fi
fi
done
wait
else
curl "https://api.telegram.org/bot$telegram_bot_token/sendMessage?chat_id=$telegram_chat_id&parse_mode=html&text=<b>BFD:Cookies 文件失效,请更新后重试</b>%0A%0A$videomessage"
fi
fi

增加一个run.sh脚本用来传递参数,每行可以对应不同的用户和收藏夹

#!/bin/bash
/home/downloads/bili/bili.sh "6784221" "1414803921" "精彩作画"

配置crontab自动运行

*/10 * * * * root /home/downloads/bili/run.sh >/dev/null 2>&1