forked from linaspurinis/trakt.plex.scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing_trailers.sh
More file actions
executable file
·93 lines (83 loc) · 3.67 KB
/
Copy pathmissing_trailers.sh
File metadata and controls
executable file
·93 lines (83 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# downloads all missing trailers - it goes through all your movies and matchs them up with an entry
# in plex, grabs the imdb id from plex, and then parses the trailer url from the imdb site, then passes
# that to youtube-dl to download the trailer, it skips entries that dont have a matching imdb entry
# or if the trailer already exists
# must have 'sqlite3' and 'youtube-dl' packages (apt-get install sqlite3 youtube-dl)
# set 'mpath' and 'pms' accordingly
# Read config variables
. $(dirname $(readlink -f $0))/missing_trailers.cfg
function guid_from_filename() {
sql="${1//\'/\'\'}"
sqlite3 "${pms}Plug-in Support/Databases/com.plexapp.plugins.library.db" \
"select title, year, guid from media_parts, media_items, metadata_items where media_parts.media_item_id=media_items.id and
media_items.metadata_item_id=metadata_items.id and media_parts.file='$sql';";
}
function dirhash_from_guid() {
echo -n "$1" | sha1sum | cut -d" " -f1 | sed -e 's/^\(.\{1\}\)/\1\//' -e 's/ .*//';
}
function imdb_xml_from_guid() {
# Download only if no Extras file present (or extras = 1)
if [ ! -f "${pms}Metadata/Movies/$(dirhash_from_guid "$1").bundle/Contents/com.plexapp.agents.imdb/extras.xml" ] || [ ${extras} -eq 1 ]; then
cat "${pms}Metadata/Movies/$(dirhash_from_guid "$1").bundle/Contents/com.plexapp.agents.imdb/Info.xml";
fi
}
function imdb_from_guid() {
imdb_xml_from_guid "$1" | grep 'Movie id' | awk -F 'Movie id' '{print $2}' | cut -d\" -f2 |grep -v "^$";
}
function videos_from_yt_search() {
curl -s -G "https://www.googleapis.com/youtube/v3/search" --data-urlencode "part=snippet" --data-urlencode "q=${1}+movie+trailer" --data-urlencode "key=${ytapi}" | \
grep videoId | cut -d\" -f4;
}
IFS="
";
echo -n "Building Movies List..."; files="$(find "${mpath}" -type f |grep -vi -e "\-trailer\.*" -e "@eaDir" \
-e "\.ass$" -e "\.srt$" -e "\.idx$" -e "\.sub$" -e "\.ssa$" -e "\.alt$" -e "\.smi$" -e "\.txt$" -e "\.rar$" \
-e "\.nfo$" -e "\.jpg$" -e "\.png$" -e "\.jpeg$")"; echo done;
for filename in $files; do
#echo "Working on $filename";
cd "$(dirname "${filename}")";
#pwd
justfile="$(basename "${filename}")";
mname="$(echo "${PWD}" | sed s#"^.*/"#""#g)";
#echo "Just File: $justfile";
#echo "Movie Name: $mname";
if [ ! -e "$(dirname "${filename}")/${mname}-trailer."* ]; then
IFS='|' read -r -a sqlarray <<< "$(guid_from_filename "${filename}")";
#echo "Got GUID ${guid}"
mfulltitle="${sqlarray[0]} (${sqlarray[1]})"
guid="${sqlarray[2]}"
#echo "Title ${mfulltitle}"
[ -z "${guid}" ] && continue
imdbi="$(imdb_from_guid "${guid}")";
#echo "Got IMDB ID ${imdbi}"
[ -z "${imdbi}" ] && continue
echo "No extras.xml found, will download the trailer for ${mfulltitle}..."
url_from_trailers_hd=`python /home/trakt-list/trailer_url.py "${sqlarray[0]}"`
#echo ${url_from_trailers_hd}
if [ ${url_from_trailers_hd} == "None" ]; then
echo "Trailers-HD none..."
else
youtube-dl -o "${mname}-trailer.%(ext)s" "${url_from_trailers_hd}";
if [ $? -eq 0 ]; then
echo "youtube-dl from HD-trailers success"
continue
fi
fi
imdbv="$(videos_from_yt_search "${mfulltitle}")";
#echo "$imdbv"
IFS=$'\n' imdbvs=($imdbv)
for (( i=0; i<${#imdbvs[@]}; i++ ))
do
youtubeurl="https://www.youtube.com/watch?v=${imdbvs[$i]}"
echo "Got Youtube URL ${youtubeurl}"
echo "youtube-dl --proxy \"192.168.1.182:8080\" -o \"${mname}-trailer.%(ext)s\" \"${youtubeurl}\"";
youtube-dl --proxy "192.168.1.182:8080" -o "${mname}-trailer.%(ext)s" "${youtubeurl}";
if [ $? -eq 0 ]; then
echo "youtube-dl success"
break
fi
done
fi;
done;
echo Done...