From 446e51e619a06d8c05d8525528032cfa72ea144b Mon Sep 17 00:00:00 2001
From: Chris Coley <chris@codingallnight.com>
Date: Mon, 13 May 2019 06:06:56 +0000
Subject: [PATCH] Add a function to search for plex titles by ID or title

---
 bash_aliases | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/bash_aliases b/bash_aliases
index 7951987..c0e5e72 100644
--- a/bash_aliases
+++ b/bash_aliases
@@ -117,3 +117,33 @@ function color {
     done
     echo -e "\e[0m";
 }
+
+# Prints the Title and Uploaded Art Path for any media item that match the
+# query parameter.
+#
+# Accepts a title's "Rating Key" also know as "Metadata Key", or a string to
+# match against the title. The string must be quoted if it's multiple words.
+alias _plex-search='Search for Plex media by ID or title. Requires SUDO'
+function plex-search {
+    dbString='file:///var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db?mode=ro'
+    if [[ $1 =~ ^[0-9]+$ ]]; then
+        # Return an exact match by metadata_items.id
+        sudo sqlite3 "$dbString" "SELECT quote(title), quote('Metadata/' || CASE section_type WHEN 1 THEN 'Movies' WHEN 2 THEN 'TV Shows' ELSE 'Unknown' END || '/' || substr(hash,1,1) || '/' || substr(hash,2) || '.bundle/Uploads') FROM metadata_items JOIN library_sections ON library_sections.id = metadata_items.library_section_id WHERE metadata_items.id='$1' ORDER BY section_type ASC"
+    else
+        # Return a LIKE match against metadata_items.title, but only for Movies and TV Episodes.
+        #
+        # Here are the known metadata_types:
+        # 1: Movie
+        # 2: TV Show
+        # 3: TV Season
+        # 4: TV Episode
+        # 8: Music Artist
+        # 9: Music Album
+        # 10: Music Track
+        # 12: Movie Extra
+        # 15: Playlist
+        # 18: Collection
+        # 42: Plex Internal Stuff
+        sudo sqlite3 "$dbString" "SELECT quote(title), quote('Metadata/' || CASE section_type WHEN 1 THEN 'Movies' WHEN 2 THEN 'TV Shows' ELSE 'Unknown' END || '/' || substr(hash,1,1) || '/' || substr(hash,2) || '.bundle/Uploads') FROM metadata_items JOIN library_sections ON library_sections.id = metadata_items.library_section_id WHERE metadata_items.title LIKE '%$1%' AND metadata_type IN (1,4) ORDER BY section_type ASC"
+    fi
+}
-- 
GitLab