-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathxseed.sh
executable file
·262 lines (234 loc) · 8.83 KB
/
xseed.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env bash
### UPDATED FOR SEASON PACK FROM USENET SUPPORT IN
### CROSS SEED v6 ONLY!! v5 IS NOT SUPPORTED FOR USENET
### SEASON PACKS AND WILL ALWAYS FAIL TO FIND MATCHES
### TO ENABLE THIS FEATURE YOU _MUST_ SWITCH TO THE
### ON IMPORT COMPLETE EVENT TYPE IN YOUR SONARR SETTINGS
# Load environment variables from .env file if it exists
# in the same directory as this bash script
VERSION='4.0.0'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_PATH="$SCRIPT_DIR/.env"
OLD_IFS="$IFS"
# Function to log messages
log_message() {
local log_type="$1"
local message="$2"
local log_line
log_line="$(date '+%Y-%m-%d %H:%M:%S') [$log_type] $message"
if [ -f "$LOG_FILE" ]; then
echo "$log_line" | tee -a "$LOG_FILE"
else
echo "$log_line"
fi
}
log_message "INFO" "xseed.sh script started $VERSION"
EVAR=false
if [ -f "$ENV_PATH" ]; then
# shellcheck source=.env
log_message "INFO" "Loading environment variables from $ENV_PATH file"
# shellcheck disable=SC1090 # shellcheck sucks
if source "$ENV_PATH"; then
log_message "INFO" "Environment variables loaded successfully"
EVAR=true
else
log_message "ERROR" "Error loading environment variables" >&2
exit 2
fi
else
log_message "DEBUG" ".env file not found in script directory ($ENV_PATH)"
fi
### START OF CONFIGURATION SECTION
### START OF CONFIGURATION SECTION
# Use environment variables with descriptive default values
# If you are not using environmental variables set your client
# in the ELSE section of the following if-else statement
if [[ -n "$TORRENT_CLIENTS" || -n "$USENET_CLIENTS" ]]; then
# Convert from env to arrays
IFS=','
read -r -a TORRENT_CLIENTS <<<"$TORRENT_CLIENTS"
read -r -a USENET_CLIENTS <<<"$USENET_CLIENTS"
else
# If you are not using environmental variables set your client here
# format for up to as many clients as you need
# Multiple clients: (${TORRENT_CLIENTS:-"Qbit","Qbit2"})
# Single Client: (${TORRENT_CLIENTS:-"Qbit"})
TORRENT_CLIENTS=("Qbit")
USENET_CLIENTS=("SABnzbd")
fi
XSEED_HOST=${XSEED_HOST:-crossseed}
XSEED_PORT=${XSEED_PORT:-8080}
LOG_FILE=${LOG_FILE:-/config/xseed.log}
LOGID_FILE=${LOGID_FILE:-/config/xseed-id.log}
XSEED_APIKEY=${XSEED_APIKEY:-}
### END OF CONFIGURATION SECTION
# Restore original IFS
IFS="$OLD_IFS"
log_message "DEBUG" "Using '.env' file for config?: $EVAR"
log_message "INFO" "Using Configuration:"
log_message "INFO" "TORRENT_CLIENTS=$(printf '"%s" ' "${TORRENT_CLIENTS[@]}")"
log_message "INFO" "USENET_CLIENTS=$(printf '"%s" ' "${USENET_CLIENTS[@]}")"
log_message "INFO" "XSEED_HOST=$XSEED_HOST"
log_message "INFO" "XSEED_PORT=$XSEED_PORT"
log_message "INFO" "LOG_FILE=$LOG_FILE"
log_message "INFO" "LOGID_FILE=$LOGID_FILE"
# Function to check if a client is in the allowed list
is_valid_client() {
local client="$1"
local client_type="$2"
case $client_type in
"torrent")
for allowed_client in "${TORRENT_CLIENTS[@]}"; do
if [[ "$client" == "$allowed_client" ]]; then
return 0
fi
done
;;
"usenet")
for allowed_client in "${USENET_CLIENTS[@]}"; do
if [[ "$client" == "$allowed_client" ]]; then
return 0
fi
done
;;
esac
return 1
}
# Function to send a request to Cross Seed API
cross_seed_request() {
local endpoint="$1"
local data="$2"
local headers=(-X POST "http://$XSEED_HOST:$XSEED_PORT/api/$endpoint" --data-urlencode "$data")
if [ -n "$XSEED_APIKEY" ]; then
headers+=(-H "X-Api-Key: $XSEED_APIKEY")
fi
response=$(curl --silent --output /dev/null --write-out "%{http_code}" "${headers[@]}")
if [ "$response" == "000" ]; then
log_message "ERROR" "Failed to connect to $XSEED_HOST:$XSEED_PORT. Timeout or connection refused."
fi
echo "$response"
}
# Detect application and set environment
detect_application() {
app="unknown"
if [ -n "$radarr_eventtype" ]; then
app="radarr"
# shellcheck disable=SC2154 # These are set by Starr on call
clientID="$radarr_download_client"
# shellcheck disable=SC2154 # These are set by Starr on call
downloadID="$radarr_download_id"
# shellcheck disable=SC2154 # These are set by Starr on call
filePath="$radarr_moviefile_path"
# shellcheck disable=SC2154 # These are set by Starr on call
eventType="$radarr_eventtype"
elif [ -n "$sonarr_eventtype" ]; then
app="sonarr"
# shellcheck disable=SC2154 # These are set by Starr on call
sonarrReleaseType="$sonarr_release_releasetype"
# shellcheck disable=SC2154 # These are set by Starr on call
clientID="$sonarr_download_client"
# shellcheck disable=SC2154 # These are set by Starr on call
downloadID="$sonarr_download_id"
if [ -n "$sonarrReleaseType" ] && [ "$sonarrReleaseType" == "SeasonPack" ]; then
# shellcheck disable=SC2154 # These are set by Starr on call
folderPath="$sonarr_destinationpath"
else
if [ -z "$sonarr_release_releasetype" ]; then
# shellcheck disable=SC2154 # These are set by Starr on call
folderPath="$sonarr_episodefile_sourcefolder"
# shellcheck disable=SC2154 # These are set by Starr on call
filePath="$sonarr_episodefile_path"
else
# shellcheck disable=SC2154 # These are set by Starr on call
filePath="$sonarr_episodefile_paths"
fi
fi
# shellcheck disable=SC2154 # These are set by Starr on call
eventType="$sonarr_eventtype"
fi
if [ "$app" == "unknown" ]; then
log_message "ERROR" "Unknown application type detected. Exiting."
exit 2
fi
log_message "INFO" "Detected application: $app"
}
# Validate the process
validate_process() {
[ ! -f "$LOG_FILE" ] && touch "$LOG_FILE"
[ ! -f "$LOGID_FILE" ] && touch "$LOGID_FILE"
unique_id="${downloadID}-${clientID}"
if [ -z "$unique_id" ]; then
log_message "ERROR" "Unique ID is missing. Exiting."
exit 1
fi
if grep -qF "$unique_id" "$LOGID_FILE"; then
log_message "INFO" "Download ID [$unique_id] already processed and logged in [$LOGID_FILE]. Exiting."
exit 0
fi
if [ -z "$eventType" ]; then
log_message "ERROR" "No event type specified. Exiting."
exit 1
fi
if [ "$eventType" == "Test" ]; then
log_message "INFO" "Test event detected. Exiting."
exit 0
fi
if [ -z "$filePath" ] && [ -z "$folderPath" ] && [ -z "$downloadID" ]; then
log_message "ERROR" "Essential parameters missing. Exiting."
exit 2
fi
if [ -z "$downloadID" ]; then
log_message "ERROR" "Download ID is missing. Checking if file path works for data/path based cross-seeding."
if [[ -z "$filePath" && -z "$folderPath" ]]; then
log_message "ERROR" "File and Folder paths are missing. Exiting."
exit 2
fi
fi
}
# Function to send data for search
send_data_search() {
if [ -n "$sonarrReleaseType" ] && [ "$sonarrReleaseType" == "SeasonPack" ]; then
xseed_resp=$(cross_seed_request "webhook" "path=$folderPath")
else
xseed_resp=$(cross_seed_request "webhook" "path=$filePath")
fi
}
# Main logic for handling operations
handle_operations() {
detect_application
validate_process
# Check if client is a torrent client
if is_valid_client "$clientID" "torrent"; then
log_message "INFO" "Processing torrent client operations for $clientID..."
if [ -n "$downloadID" ]; then
xseed_resp=$(cross_seed_request "webhook" "infoHash=$downloadID")
fi
if [ "$xseed_resp" != "204" ]; then
sleep 15
send_data_search
fi
# Check if client is a usenet client
elif is_valid_client "$clientID" "usenet"; then
if [ -z "$sonarrReleaseType" ] && [[ "$folderPath" =~ S[0-9]{1,2}(?!\.E[0-9]{1,2}) ]]; then
log_message "WARN" "Depreciated Action. Skipping season pack search. Please switch to On Import Complete for Usenet Season Pack Support!"
exit 0
fi
log_message "INFO" "Processing Usenet client operations for $clientID..."
send_data_search
else
log_message "ERROR" "Unrecognized client $clientID. Exiting."
exit 2
fi
log_message "INFO" "Cross-seed API response: $xseed_resp"
if [ "$xseed_resp" == "204" ]; then
echo "$unique_id" >>"$LOGID_FILE"
log_message "INFO" "Process completed successfully."
else
if [ "$xseed_resp" == "000" ]; then
log_message "ERROR" "Process Timed Out. Exiting."
fi
log_message "ERROR" "Process failed with API response: $xseed_resp"
exit 1
fi
}
handle_operations