-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
212 lines (174 loc) · 5.82 KB
/
main.go
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
// usr/bin/go run $0 $@ ; exit
package main
import (
"debridGo/config"
"debridGo/conversion"
"debridGo/mediaServer"
"debridGo/servarr"
"debridGo/types"
"encoding/json"
"flag"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func main() {
// dir := flag.String("dir", "", "")
// rootDir := flag.String("rootDir", "", "")
// torrent := flag.String("torrent", "", "")
saveDir := flag.String("saveDir", "", "")
rdtcHash := flag.String("hash", "", "")
// count := flag.Int64("count", 0, "")
flag.Parse()
ex, err := config.ExecutableDir()
if err != nil {
log.Fatalln(err)
}
f, err := os.OpenFile(ex+"/log-debridgo.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
// Get values from configDebridGo.toml
conf, err := config.Values()
if err != nil {
log.Fatalln("Could not get value from toml file: ", err)
}
// Get environment variables from sonarr or radarr using the custom script option and the "On Grab" trigger.
// Save these variables in a file so they can be used when "debridGo" is executed by "rdtClient".
var rclonePath string
// RADARR env variables
radarrInternalMovieID := os.Getenv("radarr_movie_id")
movieTitle := os.Getenv("radarr_movie_title")
movieYear := os.Getenv("radarr_movie_year")
torrentHash := os.Getenv("radarr_download_id") // Torrent hash that comes from radarr/sonarr. Useful to match it with the torrent hash from rdtclient
rclonePath = conf.Rclone.RemoteName + ":" + conf.Rclone.MoviesDir + "/" + movieTitle + " (" + movieYear + ")" + "/"
// SONARR env variables
sonarrInternalSeriesID := os.Getenv("sonarr_series_id") // Internal ID of the series
seriesTitle := os.Getenv("sonarr_series_title") // Title of the series
seriesSeasonNumber := os.Getenv("sonarr_release_seasonnumber") // Season number from release
if torrentHash == "" {
torrentHash = os.Getenv("sonarr_download_id") // Torrent hash that comes from radarr/sonarr. Useful to match it with the torrent hash from rdtclient
rclonePath = conf.Rclone.RemoteName + ":" + conf.Rclone.SeriesDir + "/" + seriesTitle + "/" + "Season " + seriesSeasonNumber + "/"
}
// if this script was triggered from sonarr/radarr save necessary data to a JSON file.
if torrentHash != "" {
// convert id from string to int and set correct category.
var id int
var category string
if radarrInternalMovieID != "" {
id, _ = strconv.Atoi(radarrInternalMovieID)
category = "radarr"
} else {
id, _ = strconv.Atoi(sonarrInternalSeriesID)
category = "tv-sonarr"
}
// Instance of Data struct.
data := types.DataJSON{
TorrentHash: torrentHash,
ID: id,
Category: category,
RclonePath: rclonePath,
}
// Marshal the struct to JSON.
jsonData, err := json.Marshal(data)
if err != nil {
log.Fatalln("Error marshalling JSON: ", err)
return
}
err = os.WriteFile(conf.DebridGo.DownloadDir+"/"+strings.ToLower(torrentHash)+".json", jsonData, 0777)
if err != nil {
log.Fatalln("Error writing to file: ", err)
}
log.Printf("Saving data to %v.json", strings.ToLower(torrentHash))
}
// This section gets triggered by rdtclient when a download finishes.
if *saveDir != "" {
// Get the full path of video files in saveDir.
files, err := getVideoFiles(*saveDir)
if err != nil {
log.Fatalln(err)
}
// Convert all video files in the saveDir one at a time. This will create new .mp4 video files and new .vtt subtitle files.
for _, file := range files {
err = conversion.Video(file)
if err != nil {
log.Fatalln(err)
}
}
// Get values from rdtcHash.json located in rdtclient root download directory.
jsonFile, err := os.ReadFile(conf.DebridGo.DownloadDir + "/" + *rdtcHash + ".json")
if err != nil {
log.Fatalln(err)
}
var data types.DataJSON
err = json.Unmarshal(jsonFile, &data)
if err != nil {
log.Fatalln(err)
}
// Copy to destination using rclone.
err = servarr.CopyToDst(*saveDir, data.RclonePath)
if err != nil {
log.Fatalln(err)
}
// Check sonarr/radarr for new added files.
if data.Category == "tv-sonarr" {
err = servarr.RescanSonarr(data.ID, conf.Sonarr.ApiURL, conf.Sonarr.ApiKey)
if err != nil {
log.Println(err)
} else {
log.Println("Series rescanned successfully.")
}
}
if data.Category == "radarr" {
err = servarr.RescanRadarr(data.ID, conf.Radarr.ApiURL, conf.Radarr.ApiKey, conf.Bazarr.ApiURL, conf.Bazarr.ApiKey)
if err != nil {
log.Fatalln(err)
}
log.Println("Movie rescanned successfully.")
}
// Once everything is ready and where it belongs, send a request to emby/jellyfin to scan the library. And then to Jellyseerr
err = mediaServer.ScanEmby(conf.Emby.ApiURL, conf.Emby.ApiKey)
if err != nil {
log.Fatalln(err)
}
// Sync Jellyseerr.
err = mediaServer.SyncJellyseerr(conf.Jellyseerr.ApiURL, conf.Jellyseerr.ApiKey)
if err != nil {
log.Fatalln(err)
}
// Remove saveDir and json file previously created.
err = os.RemoveAll(*saveDir)
if err != nil {
log.Fatalln(err)
}
log.Println("Removed directory: ", *saveDir)
err = os.Remove(conf.DebridGo.DownloadDir + "/" + *rdtcHash + ".json")
if err != nil {
log.Fatalln(err)
}
log.Printf("Removed file: %v/%v.json", conf.DebridGo.DownloadDir, *rdtcHash)
log.Println("Done. Goodbye :)")
}
}
func getVideoFiles(saveDir string) ([]string, error) {
var files []string
filepath.WalkDir(saveDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && d.Name() != filepath.Base(saveDir) {
return filepath.SkipDir
}
// Append files for video conversion. Only mp4 and mkv are wanted.
if strings.HasSuffix(d.Name(), "mp4") || strings.HasSuffix(d.Name(), "mkv") {
files = append(files, saveDir+"/"+d.Name())
}
return nil
})
return files, nil
}