summaryrefslogblamecommitdiffstats
path: root/skripti/365_prenesi_omejene_vsebine.sh
blob: 16f527da5b98081df87bf290164ed6aea5932093 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                                                                      



















                                                                                               






                                                                                                                                                                   





                                                                                                                                                                                                                   


                                                                                                      



                                                                                                                                                                               


                                                                                                                       
         
                       








                                                                                                                      
                                                                                                             


                     
                                                                                             


                        

                                                                

                                
#!/bin/bash
# program poženi enkrat na dan (cronjob) v direktoriju za arhiviranje
# v datoteko zadnji shrani ID zadnje preverjene vsebine
# ko preveri vso vsebino od zadnje preverjene vsebine, se zapre
# ko naleti na vsebino, ki poteče, jo prenese
# če datoteka zadnji ne obstaja, vzame ID zadnjega dnevnika
set -euo pipefail
page=`curl --fail-with-body https://365.rtvslo.si/oddaja/dnevnik/92` # outputa preveč shita za set -x
set -x
if mkdir lock
then
	echo $$ > lock/pid
else
	if [ -d /proc/`cat lock/pid` ]
	then
		echo ANOTHER INSTANCE IS ALREADY RUNNING
		echo if you are sure that this is not the case:
		echo rm -r `pwd`/lock
		exit 1
	fi
fi
echo $$ > lock/pid
p=`rev <<<$0 | cut -d/ -f1 | rev`
t=`mktemp -p "" $p.XXX`
trap "rm $t; rm -r lock" EXIT
dnevnik_id=`grep href=./arhiv/dnevnik <<<"$page" | cut -d\" -f2 | cut -d/ -f4 | head -n1`
client_id=`grep 'client-id="' <<<"$page" | head -n1 | sed -E 's/^.*client-id="([^"]*)".*$/\1/'`
find . -size -12M -type f -name '*.mp4' > $t
while read file
do # grep for specific audio codec ... if sample rate is 44100 and (tv broadcast is at 48000) and smaller than 12M, it's most likely the dummy/pravicepotekle video
	samplerate=`ffprobe "$file" 2>&1 | grep 0x6134706D | grep -Eo '[0-9]+ Hz' | cut -d\  -f1`
	id=`grep -Eo "\[[0-9]+\]\.mp" <<<"$file" | grep -o '[0-9]*'`
	if [ $samplerate -eq 48000 ]
	then
		continue
	fi
	api_response=`curl --fail-with-body https://api.rtvslo.si/ava/getRecordingDrm/$id?client_id=$client_id`
	if [ "`jq --raw-output .response.expirationDate <<<$api_response | head -c1`" = "3" ] || [ $((`date +%s`-86400*3-`date --date "$(jq --raw-output .response.expirationDate <<<$api_response)" +%s`)) -gt 0 ]
	then
		rm "$file" # sike, zgleda ne poteče! oziroma sike, je že potekel
		continue
	fi
	if [ $samplerate -eq 44100 ]
	then
		yt-dlp --no-continue http://365.rtvslo.si/arhiv/oddaja/$id # retry fu*ked up downloads
		if [ `ls *\[$id\].mp4 | wc -l` -gt 1 ]
		then
			rm "$file" # kdaj se zgodi, da RTV spremeni ime oddaje, tedaj imam staro ime, ki ima shranjen dummy posnetek 7,6 MiB, ki neprestano triggera reload ...
		fi
		continue
	fi
	echo -e "Subject: unknown samplerate in 365.sh\n\nVideo file: $file\nsamplerate: $samplerate\n" | sendmail root
done < $t
rm -f Error\ \[*\].mp4 
if [ ! -f zadnji ]
then
	echo $dnevnik_id > zadnji
fi
id_oddaje=`cat zadnji`
while :
do
	id_oddaje=$(($id_oddaje+1))
	api_response=`curl --fail-with-body https://api.rtvslo.si/ava/getRecordingDrm/$id_oddaje?client_id=$client_id`
	if [ "`jq --raw-output .response.title <<<$api_response`" = Error ] && [ $dnevnik_id -lt $id_oddaje ]
	then
		break
	fi
	if [ "`jq --raw-output .response.expirationDate <<<$api_response | head -c1`" = "3" ]
	then
		continue
	fi
	# echo $api_response > $id_oddaje.json
	yt-dlp http://365.rtvslo.si/arhiv/oddaja/$id_oddaje || :
	echo $id_oddaje > zadnji
done