mirror of
https://github.com/acedanger/shell.git
synced 2026-03-24 19:11:48 -07:00
feat: Integrate Plex API for library scanning, metadata refresh, and media analysis
This commit is contained in:
@@ -166,6 +166,77 @@ get_plex_token() {
|
|||||||
echo "$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
|
# 📡 List library sections via the Plex API
|
||||||
# Output format: "key|title|type" per line (e.g. "1|Movies|movie")
|
# Output format: "key|title|type" per line (e.g. "1|Movies|movie")
|
||||||
api_list_sections() {
|
api_list_sections() {
|
||||||
@@ -273,7 +344,7 @@ scan_library() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
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}"
|
print_status "${CHECKMARK}" "Library section $section_id scan completed" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -292,7 +363,7 @@ scan_library() {
|
|||||||
[[ -n "$id" ]] || continue
|
[[ -n "$id" ]] || continue
|
||||||
print_status "${INFO}" "Scanning section $id..." "${YELLOW}"
|
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}"
|
print_status "${CHECKMARK}" "Section $id scanned successfully" "${GREEN}"
|
||||||
else
|
else
|
||||||
print_status "${CROSS}" "Failed to scan section $id" "${RED}"
|
print_status "${CROSS}" "Failed to scan section $id" "${RED}"
|
||||||
@@ -319,11 +390,6 @@ refresh_library() {
|
|||||||
local section_id="$1"
|
local section_id="$1"
|
||||||
local force="${2:-false}"
|
local force="${2:-false}"
|
||||||
|
|
||||||
local force_flag=""
|
|
||||||
if [[ "$force" == "true" ]]; then
|
|
||||||
force_flag="--force"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$section_id" ]]; then
|
if [[ -n "$section_id" ]]; then
|
||||||
print_status "${RECYCLE}" "Refreshing metadata for library section $section_id..." "${BLUE}"
|
print_status "${RECYCLE}" "Refreshing metadata for library section $section_id..." "${BLUE}"
|
||||||
|
|
||||||
@@ -331,7 +397,7 @@ refresh_library() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
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}"
|
print_status "${CHECKMARK}" "Library section $section_id metadata refreshed" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -350,7 +416,7 @@ refresh_library() {
|
|||||||
[[ -n "$id" ]] || continue
|
[[ -n "$id" ]] || continue
|
||||||
print_status "${INFO}" "Refreshing section $id..." "${YELLOW}"
|
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}"
|
print_status "${CHECKMARK}" "Section $id refreshed successfully" "${GREEN}"
|
||||||
else
|
else
|
||||||
print_status "${CROSS}" "Failed to refresh section $id" "${RED}"
|
print_status "${CROSS}" "Failed to refresh section $id" "${RED}"
|
||||||
@@ -377,11 +443,6 @@ analyze_library() {
|
|||||||
local section_id="$1"
|
local section_id="$1"
|
||||||
local deep="${2:-false}"
|
local deep="${2:-false}"
|
||||||
|
|
||||||
local analyze_flag="--analyze"
|
|
||||||
if [[ "$deep" == "true" ]]; then
|
|
||||||
analyze_flag="--analyze-deeply"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$section_id" ]]; then
|
if [[ -n "$section_id" ]]; then
|
||||||
print_status "${SEARCH}" "Analyzing media in library section $section_id..." "${BLUE}"
|
print_status "${SEARCH}" "Analyzing media in library section $section_id..." "${BLUE}"
|
||||||
|
|
||||||
@@ -389,7 +450,7 @@ analyze_library() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
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}"
|
print_status "${CHECKMARK}" "Library section $section_id analysis completed" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -408,7 +469,7 @@ analyze_library() {
|
|||||||
[[ -n "$id" ]] || continue
|
[[ -n "$id" ]] || continue
|
||||||
print_status "${INFO}" "Analyzing section $id..." "${YELLOW}"
|
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}"
|
print_status "${CHECKMARK}" "Section $id analyzed successfully" "${GREEN}"
|
||||||
else
|
else
|
||||||
print_status "${CROSS}" "Failed to analyze section $id" "${RED}"
|
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 -e "${DIM}Select an operation to perform:${RESET}"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# First, check if Plex is running and scanner is available
|
# First, check if Plex is running
|
||||||
if ! check_plex_service; then
|
if ! check_plex_service; then
|
||||||
return 2
|
return 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! find_scanner; then
|
|
||||||
return 3
|
|
||||||
fi
|
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
echo -e "${BOLD}Available Operations:${RESET}"
|
echo -e "${BOLD}Available Operations:${RESET}"
|
||||||
echo -e "${GREEN}1)${RESET} List all libraries"
|
echo -e "${GREEN}1)${RESET} List all libraries"
|
||||||
@@ -660,6 +717,9 @@ interactive_mode() {
|
|||||||
;;
|
;;
|
||||||
5)
|
5)
|
||||||
echo ""
|
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 "${BOLD}Thumbnail Generation Options:${RESET}"
|
||||||
echo -e "${GREEN}1)${RESET} Generate for all libraries"
|
echo -e "${GREEN}1)${RESET} Generate for all libraries"
|
||||||
echo -e "${GREEN}2)${RESET} Generate for specific library"
|
echo -e "${GREEN}2)${RESET} Generate for specific library"
|
||||||
@@ -677,11 +737,16 @@ interactive_mode() {
|
|||||||
print_status "${CROSS}" "Invalid choice" "${RED}"
|
print_status "${CROSS}" "Invalid choice" "${RED}"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
6)
|
6)
|
||||||
echo ""
|
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
|
read -p "$(echo -e "${BOLD}Enter section ID to show tree:${RESET} ")" section_id
|
||||||
show_library_tree "$section_id"
|
show_library_tree "$section_id"
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
q|Q)
|
q|Q)
|
||||||
print_status "${INFO}" "Goodbye!" "${CYAN}"
|
print_status "${INFO}" "Goodbye!" "${CYAN}"
|
||||||
@@ -738,10 +803,6 @@ main() {
|
|||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! find_scanner; then
|
|
||||||
exit 3
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Handle commands
|
# Handle commands
|
||||||
case "${1,,}" in
|
case "${1,,}" in
|
||||||
"list")
|
"list")
|
||||||
@@ -762,10 +823,12 @@ main() {
|
|||||||
analyze_library "$section_id" "$deep"
|
analyze_library "$section_id" "$deep"
|
||||||
;;
|
;;
|
||||||
"generate"|"thumbnails")
|
"generate"|"thumbnails")
|
||||||
|
if ! find_scanner; then exit 3; fi
|
||||||
local section_id="${2:-}"
|
local section_id="${2:-}"
|
||||||
generate_thumbnails "$section_id"
|
generate_thumbnails "$section_id"
|
||||||
;;
|
;;
|
||||||
"tree")
|
"tree")
|
||||||
|
if ! find_scanner; then exit 3; fi
|
||||||
local section_id="$2"
|
local section_id="$2"
|
||||||
if [[ -z "$section_id" ]]; then
|
if [[ -z "$section_id" ]]; then
|
||||||
print_status "${CROSS}" "Section ID required for tree command" "${RED}"
|
print_status "${CROSS}" "Section ID required for tree command" "${RED}"
|
||||||
|
|||||||
Reference in New Issue
Block a user