-
Notifications
You must be signed in to change notification settings - Fork 0
/
KodiUpdateMoviesNoSQL.sh
executable file
·94 lines (78 loc) · 2.77 KB
/
KodiUpdateMoviesNoSQL.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
#!/bin/bash
# Function to build the Kodi connection URL
kodi_connect_url() {
local protocol="http" # You can modify this if needed (e.g., https)
local ip="$1"
local port="$2"
echo "$protocol://${ip}:${port}/jsonrpc"
}
# Function to build the Kodi authorization header
kodi_auth_header() {
local username="$1"
local password="$2"
local encoded_credentials=$(echo -n "$username:$password" | base64)
echo "Authorization: Basic $encoded_credentials"
}
# Process command-line arguments
while getopts ":u:p:i:P:" opt; do
case $opt in
u) kodi_username="$OPTARG" ;;
p) kodi_password="$OPTARG" ;;
i) kodi_ip="$OPTARG" ;;
P) kodi_port="$OPTARG" ;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1 ;;
esac
done
# Remove processed options from $@
shift $((OPTIND-1))
# Check if required arguments are provided
if [[ -z "$kodi_username" || -z "$kodi_password" || -z "$kodi_ip" || -z "$kodi_port" ]]; then
echo "Error: Missing required arguments. Please provide -u username, -p password, -i IP address, and -P port."
exit 1
fi
kodi_url=$(kodi_connect_url "$kodi_ip" "$kodi_port")
auth_header=$(kodi_auth_header "$kodi_username" "$kodi_password")
# Function to build the JSON request GetMovies
GetMovies() {
echo '{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": {}, "id": 1}'
}
# Function to build the JSON request RefreshMovie
RefreshMovie() {
local movie_id="$1"
echo '{"jsonrpc": "2.0", "method": "VideoLibrary.RefreshMovie", "params": {"movieid": '$movie_id', "ignorenfo": true}, "id": 1}'
}
# Get the JSON request
json_req=$(GetMovies)
# Send request using curl and capture the response
response=$(curl -X POST -s -H 'Content-Type: application/json' -H "$auth_header" "$kodi_url" -d "$json_req")
# Check if curl was successful (exit code 0 indicates success)
if [ $? -ne 0 ]; then
echo "Error: Failed to retrieve movies from Kodi."
exit 1
fi
# Parse the response using jq
# Extract movie IDs using error handling (assuming jq might fail)
movie_ids=$(echo "$response" | jq -r '.result.movies[].movieid' || true)
if [ $? -eq 0 ]; then
echo "Movie IDs retrieved successfully."
else
echo "Error: Unable to parse movie IDs from response."
fi
# Declare an empty array to store movie IDs
declare -a movie_id_array
# Loop through each movie ID and add it to the array
for movie_id in $movie_ids; do
movie_id_array+=("$movie_id")
done
# Print the lower and upper IDs
echo "The lower ID is: ${movie_id_array[0]}"
echo "The upper ID is: ${movie_id_array[${#movie_id_array[@]} - 1]}"
echo "Refreshing movies"
for movie_id in "${movie_id_array[@]}"
do
json_req=$(RefreshMovie $movie_id)
echo "Refreshing movie record" $movie_id
curl -X POST -s -H 'Content-Type: application/json' -H "$auth_header" "$kodi_url" -d "$json_req"
echo " "
echo " "
done