bashtube/webroot/watch.shs

215 lines
9 KiB
Plaintext
Raw Normal View History

#!/bin/bash
if [[ ! -d "temp" ]]; then
mkdir "temp"
fi
2021-02-28 17:43:40 +01:00
declare -A strings
2021-03-30 23:23:13 +02:00
if [[ ! "$(which haruhi-dl)" ]]; then
2021-03-15 19:02:33 +01:00
strings[error]='Could not load video'
source templates/head.sh
source "${cfg[namespace]}/templates/header.shs"
render strings "${cfg[namespace]}/templates/themes/${cfg[template]}/watch_error.t"
return
fi
2021-02-18 01:18:06 +01:00
if [[ "${get_data[v]}" ]]; then
if [[ "${get_data[v]}" == '18l' || "${get_data[v]}" == '5eFdt6Y_34E' ]]; then
2021-02-28 17:43:40 +01:00
strings[uploader]='MyMusicGroup'
strings[title]='█▬█ █ ▀█▀ Jeden Osiem L - Jak Zapomnieć (Oficjalny Teledysk)'
2021-05-26 19:01:37 +02:00
formats='[{
"format_id": "44",
"url": "https://sakamoto.pl/tmp/videoplayback_.webm",
"asr": 44100,
"format_note": "480p",
"fps": 25,
"height": 480,
"tbr": 867,
"width": 640,
"ext": "webm",
"vcodec": "vp8",
2021-07-01 18:54:23 +02:00
"acodec": "vorbis"
2021-05-26 19:01:37 +02:00
}]'
2021-03-01 23:53:15 +01:00
if [[ "${r[host]}" == "${cfg[flash_host]}" ]]; then
2021-06-18 16:05:15 +02:00
url='http://f.sakamoto.pl/videoplayback_.flv'
2021-03-01 23:53:15 +01:00
fi
2021-02-28 17:43:40 +01:00
if [[ ${cfg[template]} == 'default' ]]; then
meta[title]="${strings[title]}"
source templates/head.sh
fi
else
2021-07-01 20:05:09 +02:00
cd temp
2021-08-02 23:36:32 +02:00
video=$(haruhi-dl --all-subs --ie-key Youtube --write-pages --cookies "${cfg[_cookies]}" -J "http://youtube.com/watch?v=${get_data[v]}")
if [[ $video == '' ]]; then # okay who did this
2021-03-15 19:02:33 +01:00
random_hash=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 1)
2021-08-02 23:36:32 +02:00
video=$(haruhi-dl --all-subs --ie-key Youtube --write-pages --cookies ${cfg[_cookies]} -J "http://youtube.com/watch?v=${get_data[v]}" 2> output$random_hash.log)
if [[ $video == '' ]]; then # seriously WHO DID THIS
2021-06-20 23:14:13 +02:00
if [[ "${cfg[regionlock_proxy]}" == true ]]; then
# retrying w/ proxy!
2021-08-02 23:36:32 +02:00
video=$(haruhi-dl --all-subs --ie-key Youtube --write-pages --cookies ${cfg[_cookies]} --proxy "${cfg[regionlock_proxy_url]}" -J "http://youtube.com/watch?v=${get_data[v]}" 2> output$random_hash.log)
2021-06-20 23:14:13 +02:00
fi
2021-07-01 20:05:09 +02:00
output=$(cat output$random_hash.log)
rm output$random_hash.log
2021-06-20 23:14:13 +02:00
if [[ $video == '' ]]; then
if [[ $output == 'ERROR: Video unavailable' ]]; then
strings[error]='Video unavailable'
elif [[ $output == *'ERROR: Incomplete YouTube ID'* ]]; then
strings[error]='Incomplete YouTube ID'
else
strings[error]='Unexpected error has occured'
fi
2021-07-01 20:05:09 +02:00
cd ..
2021-06-20 23:14:13 +02:00
source templates/head.sh
source "${cfg[namespace]}/templates/header.shs"
render strings "${cfg[namespace]}/templates/themes/${cfg[template]}/watch_error.t"
return
2021-03-15 19:02:33 +01:00
fi
fi
fi
2021-07-01 20:05:09 +02:00
cd ..
#echo "http://youtube.com/watch?v=${get_data[v]}" > /dev/stderr
2021-02-28 17:43:40 +01:00
strings[channel_id]=$(jq -r '.channel_url' <<< "$video" | sed -s 's/http:\/\/www.youtube.com\/channel\///')
strings[uploader]=$(jq -r '.uploader' <<< "$video")
strings[title]=$(jq -r '.title' <<< "$video")
if [[ ${cfg[template]} == 'default' ]]; then
meta[title]="${strings[title]}"
2021-06-09 01:56:07 +02:00
meta[description]="$(jq -r '.description' <<< "$video")"
meta[unsafe]="
2021-06-23 00:37:19 +02:00
<meta property='og:image' content='https://i.ytimg.com/vi/$(html_encode "${get_data[v]}")/hqdefault.jpg'>
2021-06-09 01:56:07 +02:00
<meta property='og:type' content='video.other'>
<meta property='video:duration' content='$(jq -r '.duration' <<< "$video")'>
2021-07-01 18:54:23 +02:00
"'<script type="application/ld+json">'"$(jq -c '{
2021-06-09 01:56:07 +02:00
"@context": "https://schema.org",
"@type": "VideoObject",
"name": .title,
"alternateName": .alt_title,
"description": .description,
"thumbnail": .thumbnails[-1].url,
"duration": ("PT" + (.duration | tostring) + "S"),
2021-06-23 00:37:19 +02:00
}' <<< "$video" | sed 's/</\&#60;/g;s/>/\&#62;/g;')"'</script>'
2021-02-28 17:43:40 +01:00
source templates/head.sh
fi
2021-07-01 18:54:23 +02:00
formats=$(jq -c '[.formats[] | select(.protocol == "https") | {"format_id":.format_id,"url":.url,"ext":.ext,"width":.width,"height":.height,"acodec":.acodec,"vcodec":.vcodec,"abr":.abr,"fps":.fps}]' <<< "$video")
2021-08-02 23:36:32 +02:00
sub_array=()
for i in $(jq -r '.subtitles | keys[]' <<< "$video"); do
sub_array+=("$(jq '.subtitles.'"$i"'[] | select(.ext == "vtt").url | {"url": ., "lang":"'"$i"'"}' <<< "$video")")
done
subs=$(jq -s '.' <<< "${sub_array[@]}")
fi
if [[ "${r[host]}" == "${cfg[flash_host]}" ]]; then
IFS=$'\n'
urls=($(jq -r '.formats[] | select(.format_id == "22" or .format_id == "18").url' <<< "$video"))
unset IFS
2021-06-18 16:05:15 +02:00
if [[ "$url" == '' ]]; then
if [[ ${urls[1]} != '' ]]; then
url=${urls[1]}
else
url=${urls[0]}
fi
fi
2021-03-01 23:28:53 +01:00
strings[player]=$(source "${cfg[namespace]}/webroot/player_flash.shs")
else
strings[player]=$(source "${cfg[namespace]}/webroot/player.shs")
fi
2021-02-28 17:43:40 +01:00
strings[upload_date]=$(date -d "$(echo $video | jq -r '.upload_date' | sed -E 's/..../&-/;s/....-../&-/')" "+%d %B %Y")
2021-06-01 00:33:41 +02:00
strings[view_count]=$(echo $video | jq -r '.view_count' | xargs printf "%'d\n")
like_count=$(echo $video | jq -r '.like_count' | xargs printf "%'d\n")
dislike_count=$(echo $video | jq -r '.dislike_count' | xargs printf "%'d\n")
if [[ ${cfg[template]} == 'default' ]]; then
if [[ $like_count == 'null' && $dislike_count == 'null' ]]; then
strings[like_dislike_count]="Likes are turned off"
else
strings[like_dislike_count]="<b>$like_count</b> likes, <b>$dislike_count</b> dislikes."
fi
fi
2021-03-01 23:28:53 +01:00
strings[sub]=''
#<video id='player' src='$url' controls></video>
2021-02-28 17:43:40 +01:00
if [[ ${cookies[sh_session]} ]]; then
if [[ $(grep $(session_get_username "${cookies[sh_session]}") storage/faves | grep "${get_data[v]}") == '' ]]; then
if [[ ${cfg[template]} == 'default' ]]; then
strings[fav]="<a href='fav.shs?v=$(sed -s 's/<//g;' <<< "${get_data[v]}")' target='_blank'>Add to favourites</a><br>"
fi
else
if [[ ${cfg[template]} == 'default' ]]; then
strings[fav]="<a href='unfav.shs?v=$(sed -s 's/<//g;' <<< "${get_data[v]}")' target='_blank'>Remove from favourites</a><br>"
fi
fi
2021-02-28 18:17:35 +01:00
if [[ $(grep $(session_get_username "${cookies[sh_session]}") storage/subscribed | grep "${strings[channel_id]}") == '' ]]; then
2021-02-28 17:43:40 +01:00
if [[ ${cfg[template]} == 'default' ]]; then
2021-02-28 18:17:35 +01:00
strings[sub]="<a href='subscribe.shs?id=${strings[channel_id]}' target='_blank'>Subscribe to ${strings[uploader]}!</a><br>"
2021-02-28 17:43:40 +01:00
fi
else
if [[ ${cfg[template]} == 'default' ]]; then
2021-02-28 18:17:35 +01:00
strings[sub]="<a href='unsubscribe.shs?id=${strings[channel_id]}' target='_blank'>Unsubscribe from ${strings[uploader]}</a><br>"
fi
2021-02-28 17:43:40 +01:00
fi
else
if [[ ${cfg[template]} == 'default' ]]; then
2021-02-28 18:17:35 +01:00
strings[fav]="<b><a href='/login.shs?r=$(sed -s 's/?/___/' <<< "${r[url]}")'>Log in</a> to add this video to your favourites and/or subscribe to this channel!</b><br>"
2021-02-28 17:43:40 +01:00
fi
fi
2021-03-01 23:28:53 +01:00
strings[description]=$(jq -r '.description' <<< "$video" | sed -E 's/$/<br>/g')
# youtube-dl modifies the filename when writing with --write-pages
# params restricted=True, is_id=False to https://github.com/ytdl-org/youtube-dl/blob/70d0d4f9beba0e5b6d95ee50ad62ae7ab5be9be1/youtube_dl/utils.py#L2079
# this accurately replicates the algorithm without as much complexity
written_id="$(sed -E -e 's/__+/_/g' \
-e 's/^_*(-_)?//' \
-e 's/^-/_/' \
<<< "${get_data[v]}")"
echo "Written id $written_id"
vid="$(cat "temp/${written_id}_https_-_"*"watch"*".dump")"
2021-02-18 03:50:41 +01:00
vid_json="$(tr -d '\n' <<< "$vid" | grep -Poh "ytInitialData.*?</script>" | sed 's/ytInitialData = //;s/<\/script>//')"
2021-01-28 22:56:24 +01:00
2021-02-28 17:43:40 +01:00
strings[recommended]=$(jq -r '.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results[] | .compactAutoplayRenderer.contents[0].compactVideoRenderer, .compactVideoRenderer | if .videoId != null then "<div><a href='"'"'/watch?v="+.videoId+"'"'"'><img src='"'"'https://i.ytimg.com/vi/"+.videoId+"/mqdefault.jpg'"'"'>"+.title.simpleText+"</a></div>" else empty end' <<< "$vid_json")
2021-07-20 22:57:02 +02:00
strings[comments_list]="<div class='comments'></div><script src='/js/watch.js'></script>"
2020-12-29 07:42:23 +01:00
2021-02-28 17:43:40 +01:00
strings[playlist]=""
2021-02-18 01:18:37 +01:00
if [[ "${get_data[playlist]}" ]]; then
playlist=$(haruhi-dl --ie-key YoutubePlaylist --cookies ${cfg[_cookies]} -j --flat-playlist "https://www.youtube.com/playlist?list=${get_data[playlist]}")
IFS=$'\n'
video_title=($(jq -r '.title' <<< $playlist))
video_id=($(jq -r '.id' <<< $playlist))
for (( i=0; i<${#video_id[@]}; i++ )); do
2021-02-18 01:18:37 +01:00
if [[ "${video_id[$i]}" == "${get_data[v]}" ]]; then
next="${video_id[$((i+1))]}"
break
fi
done
2021-02-18 01:18:37 +01:00
[[ "$next" == '' ]] && next='dQw4w9WgXcQ'
2021-04-30 12:36:20 +02:00
strings[playlist]+="<script type='text/javascript'>window.addEventListener('DOMContentLoaded', (event) => { var video = document.querySelector('video'); video.addEventListener('ended', function() { window.location = 'watch?v=$next&playlist=$(html_encode ${get_data[playlist]})';});});</script><h1>Playlist</h1>"
2021-02-28 17:43:40 +01:00
for (( i=0; i<${#video_id[@]}; i++ )); do
2021-07-01 22:36:17 +02:00
strings[playlist]+="<a href='watch?v=${video_id[$i]}&playlist=$(html_encode ${get_data[playlist]})'><img src='//i.ytimg.com/vi/${video_id[$i]}/mqdefault.jpg'><br>${video_title[$i]}</a><br>"
done
fi
if [[ ! "${strings[recommended]}" ]]; then
strings[recommended]='An error occurred during fetching recommended videos'
fi
if [[ ! "${strings[comments_list]}" ]]; then
strings[comments_list]='An error ocurred during fetching comments.'
fi
2021-03-01 23:53:15 +01:00
source "${cfg[namespace]}/templates/header.shs"
2021-02-28 17:43:40 +01:00
render_unsafe strings "${cfg[namespace]}/templates/themes/${cfg[template]}/watch.t"
else
source templates/head.sh
echo "pls add ?v param i'm still WiP"
fi