forked from bakerboy448/StarrScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xseed.sh
executable file
·216 lines (195 loc) · 7.47 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
#!/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='3.0.0'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_PATH="$SCRIPT_DIR/.env"
# 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
# Use environment variables with descriptive default values
TORRENT_CLIENT_NAME=${TORRENT_CLIENT_NAME:-Qbit}
USENET_CLIENT_NAME=${USENET_CLIENT_NAME:-SABnzbd}
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:-}
log_message "DEBUG" "Using '.env' file for config?: $EVAR"
log_message "INFO" "Using Configuration:"
log_message "INFO" "TORRENT_CLIENT_NAME=$TORRENT_CLIENT_NAME"
log_message "INFO" "USENET_CLIENT_NAME=$USENET_CLIENT_NAME"
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 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
case "$clientID" in
"$TORRENT_CLIENT_NAME")
log_message "INFO" "Processing torrent client operations..."
if [ -n "$downloadID" ]; then
xseed_resp=$(cross_seed_request "webhook" "infoHash=$downloadID")
fi
if [ "$xseed_resp" != "204" ]; then
sleep 15
send_data_search
fi
;;
"$USENET_CLIENT_NAME")
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..."
send_data_search
;;
*)
log_message "ERROR" "Unrecognized client $clientID. Exiting."
exit 2
;;
esac
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