#!/bin/bash #TODO: move this somewhere more apropriate #jq_to_array(jq_expression) function _jq_to_array() { declare -ga "_tmp=($(jq -r "$1" | sed 's/[][`~!@#$%^&*():;<>.,?\|{}=+-]/\\&/g;'"s@\\\'@\\'@g" ))" } function format_numbers() { rev | sed "s#[[:digit:]]\{3\}#&,#g" | rev | sed -E 's/^,//' } if [[ ! -d "temp" ]]; then mkdir "temp" fi declare -A strings #if [[ ! "$(which haruhi-dl)" ]]; then # 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 if [[ "${get_data[v]}" ]]; then if [[ "${get_data[v]}" == '18l' || "${get_data[v]}" == '5eFdt6Y_34E' ]]; then strings[uploader]='MyMusicGroup' strings[title]='█▬█ █ ▀█▀ Jeden Osiem L - Jak Zapomnieć (Oficjalny Teledysk)' 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", "acodec": "vorbis" }]' subs='[]' if [[ "${r[host]}" == "${cfg[flash_host]}" ]]; then url='http://f.sakamoto.pl/videoplayback_.flv' fi if [[ ${cfg[template]} == 'default' ]]; then meta[title]="${strings[title]}" source templates/head.sh fi else cd temp video=$(yt-dlp --all-subs --write-pages --cookies "${cfg[_cookies]}" -J "http://youtube.com/watch?v=${get_data[v]}") if [[ $video == '' ]]; then # okay who did this random_hash=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 1) video=$(yt-dlp --all-subs --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 if [[ "${cfg[regionlock_proxy]}" == true ]]; then # retrying w/ proxy! video=$(yt-dlp --all-subs --write-pages --cookies ${cfg[_cookies]} --proxy "${cfg[regionlock_proxy_url]}" -J "http://youtube.com/watch?v=${get_data[v]}" 2> output$random_hash.log) fi output=$(cat output$random_hash.log) rm output$random_hash.log 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 cd .. source templates/head.sh source "${cfg[namespace]}/templates/header.shs" render strings "${cfg[namespace]}/templates/themes/${cfg[template]}/watch_error.t" return fi fi fi cd .. strings[channel_id]=$(jq -r '.channel_url' <<< "$video" | sed -s 's/.*\/channel\///') strings[uploader]=$(jq -r '.uploader' <<< "$video") strings[title]=$(jq -r '.title' <<< "$video") if [[ ${cfg[template]} == 'default' ]]; then meta[title]="${strings[title]}" meta[description]="$(jq -r '.description' <<< "$video")" meta[unsafe]=" "'' source templates/head.sh fi formats=$(jq -c '[.formats[] | select(.protocol == "https") | {"format_id":.format_id,"url":.url,"ext":.ext,"width":.width,"height":.height,"format_note":.format_note,"acodec":.acodec,"vcodec":.vcodec,"abr":.abr,"fps":.fps}]' <<< "$video") 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 if [[ "$url" == '' ]]; then if [[ ${urls[1]} != '' ]]; then url=${urls[1]} else url=${urls[0]} fi fi declare -A player player[url]=$(xxd -p <<< "$url" | sed -E 's/.{2}/%&/g' | tr -d '\n') player_render="$(render player "${cfg[namespace]}/templates/components/player_flash.t")" else declare -A player player[@formats]="$formats" player[@subs]="$subs" player_render="$(render player "${cfg[namespace]}/templates/components/player.t")" fi strings[upload_date]=$(date -d "$(echo $video | jq -r '.upload_date' | sed -E 's/..../&-/;s/....-../&-/')" "+%d %B %Y") strings[view_count]=$(jq -r '.view_count' <<< "$video" | format_numbers) like_count=$(jq -r '.like_count' <<< "$video" | format_numbers) if [[ $like_count == 'null' ]]; then strings[like_count]="Likes are turned off" else strings[like_count]="$like_count" fi strings[sub]='' strings[id]="${get_data[v]}" # if [[ ${cookies[sh_session]} ]]; then if [[ $(grep $(session_get_username "${cookies[sh_session]}") storage/faves | grep "${get_data[v]}") == '' ]]; then strings[?fav]=true else strings[?unfav]=true fi if [[ $(grep $(session_get_username "${cookies[sh_session]}") storage/subscribed | grep "${strings[channel_id]}") == '' ]]; then strings[?sub]=true else strings[?unsub]=true fi else strings[?login]=true strings[login_r]=$(sed -s 's/?/___/' <<< "${r[url]}") fi strings[description]=$(jq -r '.description' <<< "$video") # 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]}")" vid="$(cat "temp/${written_id}_https_-_"*"watch"*".dump")" vid_json="$(tr -d '\n' <<< "$vid" | grep -Poh "ytInitialData.*?" | sed 's/ytInitialData = //;s/<\/script>//')" strings[recommended]=$() _jq_to_array '.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results[] | .compactAutoplayRenderer.contents[0].compactVideoRenderer, .compactVideoRenderer | if .videoId != null then [.videoId, .title.simpleText] else empty end | @sh' <<< "$vid_json" nested_declare rec declare -A rec_ for (( x=0; x<${#_tmp[@]}; x=x+2 )); do rec_[id]="${_tmp[$x]}" rec_[title]="${_tmp[$((x+1))]}" nested_add rec rec_ done strings[_recommended]="rec" strings[playlist]="" if [[ "${get_data[playlist]}" ]]; then playlist=$(yt-dlp --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 if [[ "${video_id[$i]}" == "${get_data[v]}" ]]; then next="${video_id[$((i+1))]}" break fi done [[ "$next" == '' ]] && next='dQw4w9WgXcQ' strings[playlist_id]="$(html_encode "${get_data[playlist]}")" strings[next]="$next" nested_declare list declare -A list_ for (( i=0; i<${#video_id[@]}; i++ )); do list_[id]="${video_id[$i]}" list_[title]="${video_title[$i]}" list_[playlist_id]="$(html_encode "${get_data[playlist]}")" nested_add list list_ done strings[_playlist]="list" 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 source "${cfg[namespace]}/templates/header.shs" echo "$player_render" render strings "${cfg[namespace]}/templates/themes/${cfg[template]}/watch.t" else source templates/head.sh echo "pls add ?v param i'm still WiP" fi