feat: Integrate Plex API for library scanning, metadata refresh, and media analysis

This commit is contained in:
Peter Wood
2026-03-07 11:37:49 -05:00
parent dc8d35f593
commit 9196879b6c

View File

@@ -166,6 +166,77 @@ get_plex_token() {
echo "$token"
}
# 📡 Trigger a library scan via the Plex API (replaces deprecated --scan)
# Usage: api_scan_section <section_id>
api_scan_section() {
local section_id="$1"
local token
if ! token=$(get_plex_token); then
log_verbose "Cannot scan: no Plex token available"
return 1
fi
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" \
-X GET "${PLEX_API_BASE}/library/sections/${section_id}/refresh?X-Plex-Token=${token}")
if [[ "$http_code" =~ ^2 ]]; then
return 0
else
log_verbose "API scan failed for section $section_id (HTTP $http_code)"
return 1
fi
}
# 📡 Trigger a metadata refresh via the Plex API (replaces deprecated --refresh)
# Usage: api_refresh_section <section_id> [force]
api_refresh_section() {
local section_id="$1"
local force="${2:-false}"
local token
if ! token=$(get_plex_token); then
log_verbose "Cannot refresh: no Plex token available"
return 1
fi
local url="${PLEX_API_BASE}/library/sections/${section_id}/refresh?X-Plex-Token=${token}"
if [[ "$force" == "true" ]]; then
url+="&force=1"
fi
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$url")
if [[ "$http_code" =~ ^2 ]]; then
return 0
else
log_verbose "API refresh failed for section $section_id (HTTP $http_code)"
return 1
fi
}
# 📡 Trigger media analysis via the Plex API (replaces deprecated --analyze)
# Usage: api_analyze_section <section_id>
api_analyze_section() {
local section_id="$1"
local token
if ! token=$(get_plex_token); then
log_verbose "Cannot analyze: no Plex token available"
return 1
fi
local http_code
http_code=$(curl -s -o /dev/null -w "%{http_code}" \
-X PUT "${PLEX_API_BASE}/library/sections/${section_id}/analyze?X-Plex-Token=${token}")
if [[ "$http_code" =~ ^2 ]]; then
return 0
else
log_verbose "API analyze failed for section $section_id (HTTP $http_code)"
return 1
fi
}
# 📡 List library sections via the Plex API
# Output format: "key|title|type" per line (e.g. "1|Movies|movie")
api_list_sections() {
@@ -273,7 +344,7 @@ scan_library() {
return 4
fi
if run_scanner --scan --section "$section_id" ${VERBOSE:+--verbose}; then
if api_scan_section "$section_id"; then
print_status "${CHECKMARK}" "Library section $section_id scan completed" "${GREEN}"
return 0
else
@@ -292,7 +363,7 @@ scan_library() {
[[ -n "$id" ]] || continue
print_status "${INFO}" "Scanning section $id..." "${YELLOW}"
if run_scanner --scan --section "$id" ${VERBOSE:+--verbose}; then
if api_scan_section "$id"; then
print_status "${CHECKMARK}" "Section $id scanned successfully" "${GREEN}"
else
print_status "${CROSS}" "Failed to scan section $id" "${RED}"
@@ -319,11 +390,6 @@ refresh_library() {
local section_id="$1"
local force="${2:-false}"
local force_flag=""
if [[ "$force" == "true" ]]; then
force_flag="--force"
fi
if [[ -n "$section_id" ]]; then
print_status "${RECYCLE}" "Refreshing metadata for library section $section_id..." "${BLUE}"
@@ -331,7 +397,7 @@ refresh_library() {
return 4
fi
if run_scanner --refresh $force_flag --section "$section_id" ${VERBOSE:+--verbose}; then
if api_refresh_section "$section_id" "$force"; then
print_status "${CHECKMARK}" "Library section $section_id metadata refreshed" "${GREEN}"
return 0
else
@@ -350,7 +416,7 @@ refresh_library() {
[[ -n "$id" ]] || continue
print_status "${INFO}" "Refreshing section $id..." "${YELLOW}"
if run_scanner --refresh $force_flag --section "$id" ${VERBOSE:+--verbose}; then
if api_refresh_section "$id" "$force"; then
print_status "${CHECKMARK}" "Section $id refreshed successfully" "${GREEN}"
else
print_status "${CROSS}" "Failed to refresh section $id" "${RED}"
@@ -377,11 +443,6 @@ analyze_library() {
local section_id="$1"
local deep="${2:-false}"
local analyze_flag="--analyze"
if [[ "$deep" == "true" ]]; then
analyze_flag="--analyze-deeply"
fi
if [[ -n "$section_id" ]]; then
print_status "${SEARCH}" "Analyzing media in library section $section_id..." "${BLUE}"
@@ -389,7 +450,7 @@ analyze_library() {
return 4
fi
if run_scanner $analyze_flag --section "$section_id" ${VERBOSE:+--verbose}; then
if api_analyze_section "$section_id"; then
print_status "${CHECKMARK}" "Library section $section_id analysis completed" "${GREEN}"
return 0
else
@@ -408,7 +469,7 @@ analyze_library() {
[[ -n "$id" ]] || continue
print_status "${INFO}" "Analyzing section $id..." "${YELLOW}"
if run_scanner $analyze_flag --section "$id" ${VERBOSE:+--verbose}; then
if api_analyze_section "$id"; then
print_status "${CHECKMARK}" "Section $id analyzed successfully" "${GREEN}"
else
print_status "${CROSS}" "Failed to analyze section $id" "${RED}"
@@ -553,15 +614,11 @@ interactive_mode() {
echo -e "${DIM}Select an operation to perform:${RESET}"
echo ""
# First, check if Plex is running and scanner is available
# First, check if Plex is running
if ! check_plex_service; then
return 2
fi
if ! find_scanner; then
return 3
fi
while true; do
echo -e "${BOLD}Available Operations:${RESET}"
echo -e "${GREEN}1)${RESET} List all libraries"
@@ -660,6 +717,9 @@ interactive_mode() {
;;
5)
echo ""
if ! find_scanner; then
print_status "${CROSS}" "Scanner binary required for thumbnail generation" "${RED}"
else
echo -e "${BOLD}Thumbnail Generation Options:${RESET}"
echo -e "${GREEN}1)${RESET} Generate for all libraries"
echo -e "${GREEN}2)${RESET} Generate for specific library"
@@ -677,11 +737,16 @@ interactive_mode() {
print_status "${CROSS}" "Invalid choice" "${RED}"
;;
esac
fi
;;
6)
echo ""
if ! find_scanner; then
print_status "${CROSS}" "Scanner binary required for tree display" "${RED}"
else
read -p "$(echo -e "${BOLD}Enter section ID to show tree:${RESET} ")" section_id
show_library_tree "$section_id"
fi
;;
q|Q)
print_status "${INFO}" "Goodbye!" "${CYAN}"
@@ -738,10 +803,6 @@ main() {
exit 2
fi
if ! find_scanner; then
exit 3
fi
# Handle commands
case "${1,,}" in
"list")
@@ -762,10 +823,12 @@ main() {
analyze_library "$section_id" "$deep"
;;
"generate"|"thumbnails")
if ! find_scanner; then exit 3; fi
local section_id="${2:-}"
generate_thumbnails "$section_id"
;;
"tree")
if ! find_scanner; then exit 3; fi
local section_id="$2"
if [[ -z "$section_id" ]]; then
print_status "${CROSS}" "Section ID required for tree command" "${RED}"