-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.sh
83 lines (69 loc) · 2.78 KB
/
sort.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
#!/bin/bash
# Load values from the .env file
ENV_FILE="$(dirname "${BASH_SOURCE[0]}")/.env"
if [[ -f "$ENV_FILE" ]]; then
source "$ENV_FILE"
else
echo "Error: .env file not found. Please create a .env file with the required variables." >&2
exit 1
fi
LOG_FILE="$(dirname "${BASH_SOURCE[0]}")/script_log.txt"
function log_info() {
local message=$1
echo "$(date +'%Y-%m-%d %H:%M:%S') [INFO]: $message" >> "$LOG_FILE"
}
function log_error() {
local message=$1
echo "$(date +'%Y-%m-%d %H:%M:%S') [ERROR]: $message" >> "$LOG_FILE"
}
function get_latest_movie_id() {
local url="${RADARR_URL}/movie"
local headers=("X-Api-Key: ${RADARR_API_KEY}")
local response=$(curl -s -H "${headers[@]}" "${url}")
if [[ $? -eq 0 ]]; then
local movie_id=$(echo "${response}" | jq -r '.[-1].id')
echo "${movie_id}"
else
echo ""
fi
}
function check_movie_genre() {
local movie_id=$1
local url="${RADARR_URL}/movie/${movie_id}"
local headers=("X-Api-Key: ${RADARR_API_KEY}")
local response=$(curl -s -H "${headers[@]}" "${url}")
if [[ $? -eq 0 ]]; then
echo "Response for movie_id=${movie_id}:"
echo "${response}" | tee -a "$LOG_FILE"
local genres=$(echo "${response}" | jq -r '.genres[] | ascii_downcase')
echo "Genres for movie_id=${movie_id}:"
echo "${genres}" | tee -a "$LOG_FILE"
for genre in $genres; do
# Convert genre to uppercase
genre=${genre^^}
# Check if the genre is defined in the .env file
local genre_variable="GENRE_${genre}"
local dir_variable="DIR_${genre}"
if [[ -v "${genre_variable}" && -v "${dir_variable}" ]]; then
echo "Movie ID ${movie_id} has the genre: ${genre}"
# Move the movie to the corresponding genre directory
local destination_directory="${!dir_variable}"
if [[ ! -d "$destination_directory" ]]; then
mkdir -p "$destination_directory"
log_info "Created directory: $destination_directory"
fi
# Move the movie to the corresponding directory
if change_movie_path "$movie_id" "$destination_directory"; then
log_info "Movie ID: ${movie_id} moved to $destination_directory successfully."
else
log_error "Failed to move Movie ID: ${movie_id} to $destination_directory."
fi
return
fi
done
echo "Movie ID ${movie_id} does not have a specified genre in the .env file."
else
echo "Error: Unable to fetch movie information for movie_id=${movie_id}" >&2 | tee -a "$LOG_FILE"
echo "${response}" >&2 | tee -a "$LOG_FILE"
fi
}