moviecataloger/fetchyts.sh

378 lines
10 KiB
Bash
Raw Normal View History

2023-12-28 18:59:50 +02:00
#!/bin/bash
tmdbkey=""
# Needs wkhtmltopdf
# apt install wkhtmltopdf
# ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdf
# Needs qrencode
# apt install qrencode
# TMDB API
# wget "https://api.themoviedb.org/3/movie/tt0448134?external_source=imdb_id&api_key=apikeygoeshere"
# wget "https://api.themoviedb.org/3/find/tt0448134?external_source=imdb_id&api_key=apikeygoeshere"
#curl --request GET --url 'https://api.themoviedb.org/3/movie/1272/content_rating?language=en-US' --header 'Authorization: Bearer tokengoeshere' --header 'accept: application/json'
#curl --request GET --url https://api.themoviedb.org/3/certification/movie/1272 --header 'Authorization: Bearer tokengoeshere' --header 'accept: application/json'
#This one returns the Audience Restriction data under certification.
#curl --request GET --url https://api.themoviedb.org/3/movie/1272/release_dates --header 'Authorization: Bearer tokengoeshere' --header 'accept: application/json'
#Use this to get a legend of the certifications
#curl --request GET --url https://api.themoviedb.org/3/certification/movie/list --header 'Authorization: Bearer tokengoeshere' --header 'accept: application/json'
if [[ ! -f ./json/lang.json ]]
then
echo "Language JSON Data not available... Downloading now..."
wget -O "./json/lang.temp" "https://api.themoviedb.org/3/configuration/languages?api_key=$tmdbkey"
echo "{" > ./json/lang.json
size=$( jq '. | length' "./json/lang.temp" )
stop=$(( size-1 ))
for ((n=0; n<$size; n++))
do
temp="$( jq ".[$n].iso_639_1" ./json/lang.temp ): {"
echo " $temp" >> ./json/lang.json
temp="\"anglo\": $( jq ".[$n].english_name" ./json/lang.temp )"
echo " $temp," >> ./json/lang.json
temp="\"native\": $( jq ".[$n].name" ./json/lang.temp )"
echo " $temp" >> ./json/lang.json
if [[ $n == $stop ]]
then
echo " }" >> ./json/lang.json
else
echo " }," >> ./json/lang.json
fi
done
echo "}" >> ./json/lang.json
rm ./json/lang.temp
fi
if [[ -z $1 ]]
then
echo "Usage:"
echo
echo "$ ./fetchyts.sh {imdbcode}"
echo
echo "{imdbcode} is the code starting with tt found in the title's IMDB page URL."
exit
fi
if [[ ! -f ./json/$1.yts.json ]]
then
echo
echo "Downloading YTS JSON Data..."
wget -O "./json/$1.yts.temp" "https://yts.mx/api/v2/movie_details.json?imdb_id=$1&with_images=true&with_cast=true"
jq '.' "./json/$1.yts.temp" > "./json/$1.yts.json"
rm "./json/$1.yts.temp"
else
echo
echo "YTS JSON Data exists for this title."
fi
echo
echo "Verifying Metadata..."
exists=$( jq '.data.movie.id' ./json/$1.yts.json )
if [[ $exists == 0 ]]
then
echo
echo "Title does not exist on YTS!"
echo "Removing JSON file and adding to error.log"
rm "./json/$1.yts.json"
echo $1 >> error.log
exit
fi
if [[ ! -f ./json/$1.tmdb.json ]]
then
echo
echo "Downloading TMDB JSON Data..."
wget -O "./json/$1.tmdb.temp" "https://api.themoviedb.org/3/movie/$1?external_source=imdb_id&api_key=$tmdbkey"
wget -O "./json/$1.cert.temp" "https://api.themoviedb.org/3/movie/$1/release_dates?external_source=imdb_id&api_key=$tmdbkey"
wget -O "./json/$1.vids.temp" "https://api.themoviedb.org/3/movie/$1/videos?external_source=imdb_id&api_key=$tmdbkey"
size=$( jq '.' "./json/$1.tmdb.temp" | wc -l )
((size--))
((size--))
jq '.' "./json/$1.tmdb.temp" | head -n $size > "./json/$1.tmdb.json"
temp=$( jq '.' "./json/$1.tmdb.temp" | tail -n 2 | head -1 )
echo " $temp," >> "./json/$1.tmdb.json"
echo ' "cert": {' >> "./json/$1.tmdb.json"
size=$( jq '.results | length' "./json/$1.cert.temp" )
stop=$(( size-1 ))
for ((n=0; n<$size; n++))
do
temp="$( jq ".results[$n].iso_3166_1" "./json/$1.cert.temp" ): $( jq ".results[$n].release_dates[0].certification" "./json/$1.cert.temp" )"
if [[ $n == $stop ]]
then
echo " $temp" >> "./json/$1.tmdb.json"
else
echo " $temp," >> "./json/$1.tmdb.json"
fi
done
echo " }" >> "./json/$1.tmdb.json"
echo "}" >> "./json/$1.tmdb.json"
rm "./json/$1.tmdb.temp"
rm "./json/$1.cert.temp"
fi
echo
echo "Extracting Metadata..."
title=$( jq '.data.movie.title_long' ./json/$1.yts.json )
runtime=$( jq '.data.movie.runtime' ./json/$1.yts.json )
rating=$( jq '.data.movie.rating' ./json/$1.yts.json )
description=$( jq '.data.movie.description_full' ./json/$1.yts.json )
poster=$( jq '.data.movie.large_cover_image' ./json/$1.yts.json )
torrentcount=$( jq '.data.movie.torrents | length' ./json/$1.yts.json )
trailer=$( jq '.data.movie.yt_trailer_code' ./json/$1.yts.json )
language=$( jq '.data.movie.language' ./json/$1.yts.json )
restriction=$( jq '.data.movie.mpa_rating' ./json/$1.yts.json )
for ((n=0; n<$torrentcount; n++))
do
torrentquality[$n]=$( jq ".data.movie.torrents[$n].quality" ./json/$1.yts.json )
#torrentseeds[$n]=$( jq ".data.movie.torrents[$n].seeds" ./json/$1.yts.json )
torrenturl[$n]=$( jq ".data.movie.torrents[$n].url" ./json/$1.yts.json )
torrenttype[$n]=$( jq ".data.movie.torrents[$n].type" ./json/$1.yts.json )
done
actor=$( jq ".data.movie.cast[].name" ./json/$1.yts.json )
genre=$( jq '.data.movie.genres' ./json/$1.yts.json )
if [[ $restriction == '""' ]]
then
echo "No MPA Rating!"
restriction="Not Rated"
else
size=${#restriction}
((size--))
((size--))
temp=${restriction:1:$size}
restriction=$temp
fi
#check if it is porn (needs to run fetchimdbdata.sh in the imdb folder)
if [[ -e "./imdb/title.basics.tsv" ]]
then
imdbstring=$( cat ./imdb/title.basics.tsv | grep "$1 " | head -1 )
IFS=" "
imdbarray=( $imdbstring )
isadult=${imdbarray[4]}
IFS=" "
fi
if [[ $isadult == 1 ]]
then
temp="$restriction [ADULTS ONLY]"
restriction=$temp
echo "Title is for ADULTS ONLY!"
fi
size=${#description}
((size--))
((size--))
temp=${description:1:$size}
description=$temp
size=${#title}
((size--))
((size--))
temp=${title:1:$size}
title=$temp
if [[ $trailer == '""' ]]
then
echo "Trailer not found!"
trailerexist=0
else
size=${#trailer}
((size--))
((size--))
temp=${trailer:1:$size}
trailer="https://www.youtube.com/watch?v=$temp"
trailerexist=1
fi
size=${#language}
((size--))
((size--))
temp=${language:1:$size}
language=$( cat languages.data | grep "($temp)" )
size=${#actor}
for (( n=0; n<$size; n++ ))
do
temp=${actor:$n:1}
if [[ $temp == '"' ]]
then
if [[ $x == 1 ]]
then
temp=","
actors="$actors$temp"
x=0
else
x=1
fi
else
actors="$actors$temp"
fi
done
size=${#actors}
((size--))
temp=${actors:0:$size}
actors=$temp
size=${#genre}
((size--))
((size--))
((size--))
((size--))
temp=${genre:2:$size}
genre=$temp
x=0
size=${#genre}
for (( n=0; n<$size; n++ ))
do
temp=${genre:$n:1}
if [[ $temp == '"' ]]
then
x=0
else
genres="$genres$temp"
fi
done
size=${#poster}
((size--))
((size--))
temp=${poster:1:$size}
poster=$temp
echo "Movie: $title"
echo
echo "Restriction: $restriction"
echo
echo "Runtime: $runtime minutes"
echo
echo "IMDB Rating: $rating/10"
echo
echo "$description"
echo
echo "Starring: "$actors
echo
echo "Genres: "$genres
echo
echo "Poster: "$poster
echo
if [[ $trailerexist == 1 ]]
then
echo "Trailer: $trailer"
echo
fi
echo "Language: $language"
echo
echo
echo
if [ ! -e "./html/posters/$1.jpg" ]
then
echo "Downloading Poster..."
wget -O "./html/posters/$1.jpg" $poster
else
echo "Already have Poster: $1.jpg"
fi
if [[ $trailerexist == 1 ]]
then
if [ ! -e "./html/qrcodes/$1.png" ]
then
echo "Generating QR Code for the Trailer..."
qrencode -s 6 -l H -o "./html/qrcodes/$1.png" "$trailer"
else
echo "Already have QR Code for the Trailer: $1.png"
fi
else
echo "Skipping Trailer..."
fi
if [[ ! -e ./html/$1.html ]]
then
echo
echo "Generating $1.html..."
if [[ $trailerexist == 1 ]]
then
echo "<html><title>$title</title><body><table><tr><td width="10%">&nbsp</td><td><h1>$title</h1></td><td><u>Code</u>: $1</td></tr><tr><td></td><td><u>Language</u>: $language</td><td><u>Audience Restriction</u>: $restriction</td></tr><tr><td></td><td><u>Runtime</u>: $runtime minutes</td><td><u>IMDB Rating</u>: $rating/10</td></tr><tr><td></td><td valign="top"><u>Genres</u>: "$genres"<br><u>Starring</u>: "$actors"<br><br><br><table><tr><td width="95%">$description</td><td width="5%">&nbsp</td></tr></table></td><td><img src="posters/$1.jpg" width="400px" height="600px"><hr><center>Scan to watch a preview...</center><center><img src="qrcodes/$1.png" width="300px" height="300px"></center><center>Data rates may apply.</center></td></tr></table><hr><center><b>THE CONTENTS OF THIS DOCUMENT ARE NOT GUARANTEED. ALL DATA IS OBTAINED FROM PUBLICALLY MAINTAINED DATABASES. ERRORS MAY EXIST.</b><center><hr></body></html>" > ./html/$1.html
else
echo "<html><title>$title</title><body><table><tr><td width="10%"></td><td><h1>$title</h1></td><td><u>Code</u>: $1</td></tr><tr><td></td><td><u>Language</u>: $language</td><td><u>Audience Restriction</u>: $restriction</td></tr><td></td><tr><td><u>Runtime</u>: $runtime minutes</td><td><u>IMDB Rating</u>: $rating/10</td></tr><tr><td></td><td valign="top"><u>Genres</u>: "$genres"<br><u>Starring</u>: "$actors"<br><br><br><table><tr><td width="95%">$description</td><td width="5%">&nbsp</td></tr></table></td><td><img src="posters/$1.jpg" width="400px" height="600px"></td></tr></table><hr><center><b>THE CONTENTS OF THIS DOCUMENT ARE NOT GUARANTEED. ALL DATA IS OBTAINED FROM PUBLICALLY MAINTAINED DATABASES. ERRORS MAY EXIST.</b><center><hr></body></html>" > ./html/$1.html
fi
else
echo
echo "$1.html already exists..."
fi
if [[ ! -e ./pdfs/$1.pdf ]]
then
echo
echo "Generating $1.pdf..."
html2pdf --allow ./html ./html/$1.html ./pdfs/$1.pdf
else
echo
echo "$1.pdf already exists..."
fi
echo
echo "Saving Torrent Files..."
for (( n=0; n<$torrentcount; n++ ))
do
size=${#torrenturl[$n]}
((size--))
((size--))
temp=${torrenturl[$n]:1:$size}
torrenturl[$n]=$temp
size=${#torrentquality[$n]}
((size--))
((size--))
temp=${torrentquality[$n]:1:$size}
torrentquality[$n]=$temp
size=${#torrenttype[$n]}
((size--))
((size--))
temp=${torrenttype[$n]:1:$size}
torrenttype[$n]=$temp
echo
echo "URL: ${torrenturl[$n]}"
echo "Quality: ${torrentquality[$n]}"
echo "Type: ${torrenttype[$n]}"
echo
if [[ ! -e "./torrents/$title.$1.${torrentquality[$n]}.${torrenttype[$n]}.torrent" ]]
then
wget -O "./torrents/$title.$1.${torrentquality[$n]}.${torrenttype[$n]}.torrent" ${torrenturl[$n]}
else
echo "$title.$1.${torrentquality[$n]}.${torrenttype[$n]}.torrent already exists..."
fi
done
echo
echo "Done!"
echo