-
Notifications
You must be signed in to change notification settings - Fork 2
/
plex_stats.sh
143 lines (123 loc) · 5.59 KB
/
plex_stats.sh
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env bash
# Generate Plex Library stats
#### Variables ####
#Change to where you want the logfile to go.
logfile="$HOME/logs/plexstats.log"
#Change this to your plex database location.
#db="/opt/plex/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db"
db="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db"
#Change this to the name of your docker. It must match exactly. If not using then just leave the default.
docker="plex"
#Change to the location of your media scanner. This is the default one. It will work for docker and linux.
scanner="/usr/lib/plexmediaserver/Plex Media Scanner"
####End Variables####
list_section_id() {
echo "**************************************"
echo "** List of section ID's to utilize. **"
echo "**************************************"
if [[ $(docker ps --filter "name=^/$docker$" --format '{{.Names}}') == "$docker" ]]; then
docker exec -it $docker "$scanner" --list
else
"$scanner" --list
fi
}
get_array() {
if [[ $(docker ps --filter "name=^/$docker$" --format '{{.Names}}') == "$docker" ]]; then
readarray -t d_section < <(docker exec -it $docker "$scanner" --list | sed 's/:.*//')
else
readarray -t section < <( "$scanner" --list | sed 's/:.*//' )
fi
}
choice() {
echo "**************************************************************************************************" | tee -a $logfile
echo "** Please enter the number of the section id, as seen above, that you want to check intros for. **" | tee -a $logfile
echo "**************************************************************************************************" | tee -a $logfile
if [ -n "$d_section" ]; then
select id in "${d_section[@]}"; do
[[ -n $id ]] || { echo "Invalid choice. Please try again." >&2; continue; }
break
done
else
select id in "${section[@]}"; do
[[ -n $id ]] || { echo "Invalid choice. Please try again." >&2; continue; }
break
done
fi
}
library_stats() {
echo "Date: $(date "+%d.%m.%Y %T")" | tee -a $logfile
echo "" | tee -a $logfile
echo "************************" | tee -a $logfile
echo "** Plex Library Stats **" | tee -a $logfile
echo "************************" | tee -a $logfile
echo "" | tee -a $logfile
echo "Media items in Libraries" | tee -a $logfile
echo "" | tee -a $logfile
query="SELECT Library, Items \
FROM ( SELECT name AS Library, \
COUNT(duration) AS Items \
FROM media_items m \
LEFT JOIN library_sections l ON l.id = m.library_section_id \
WHERE library_section_id > 0 GROUP BY name );"
sqlite3 -header -line "$db" "$query" | tee -a $logfile
echo " " | tee -a
echo "" | tee -a $logfile
}
sql_library() {
query="SELECT count(*) FROM media_items"
result=$(sqlite3 -header -line "$db" "$query")
echo "Library Total = ${result:11} files in library" | tee -a $logfile
echo "" | tee -a $logfile
}
sql_intro() {
echo "--Skip intro stats:--"
echo ""
query="SELECT count(*) FROM media_parts mp JOIN media_items mi on mi.id = mp.media_item_id WHERE mi.library_section_id = $id and mp.extra_data like '%intros=%';"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} files analyzed for skip intros." | tee -a $logfile
query="SELECT count(*) FROM media_parts mp JOIN media_items mi on mi.id = mp.media_item_id WHERE mi.library_section_id = $id and mp.extra_data not like '%intros=%';"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} files that have not been analyzed for skip intros." | tee -a $logfile
query="SELECT count(*) FROM media_parts mp JOIN media_items mi on mi.id = mp.media_item_id WHERE mi.library_section_id = $id and mp.extra_data like '%intros=%%7B%';"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} files that actually have skip intros." | tee -a $logfile
echo ""
}
sql_analyze() {
echo "--Analyze stats:--" | tee -a $logfile
echo "" | tee -a $logfile
query="SELECT count(*) FROM media_items WHERE bitrate is null"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} files missing analyzation info" | tee -a $logfile
query="SELECT count(*) FROM metadata_items meta \
JOIN media_items media on media.metadata_item_id = meta.id \
JOIN media_parts part on part.media_item_id = media.id \
WHERE part.extra_data not like '%deepAnalysisVersion=2%' \
and meta.metadata_type in (1, 4, 12) and part.file != '';"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} files missing deep analyzation info." | tee -a $logfile
echo "" | tee -a $logfile
}
sql_deleted() {
echo "--Deleted stats:--" | tee -a $logfile
echo "" | tee -a $logfile
query="SELECT count(*) FROM media_parts WHERE deleted_at is not null"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} media_parts marked as deleted" | tee -a $logfile
query="SELECT count(*) FROM metadata_items WHERE deleted_at is not null"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} metadata_items marked as deleted" | tee -a $logfile
query="SELECT count(*) FROM directories WHERE deleted_at is not null"
result=$(sqlite3 -header -line "$db" "$query")
echo "${result:11} directories marked as deleted" | tee -a $logfile
echo "" | tee -a $logfile
}
list_section_id
get_array
choice
library_stats
sql_library
sql_intro
sql_analyze
sql_deleted
exit