DeleteArr/DeleteArr.go

197 lines
4.8 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:05:00 +07:00
package main
import (
"log"
2023-12-17 20:31:40 +07:00
"net/http"
2023-12-17 20:57:51 +07:00
"net/url"
2023-12-17 15:05:00 +07:00
"os"
2023-12-17 15:19:23 +07:00
"path/filepath"
2023-12-17 16:59:54 +07:00
"slices"
2023-12-17 18:27:22 +07:00
"strings"
2023-12-18 08:35:21 +07:00
"gopkg.in/yaml.v2"
2023-12-17 15:05:00 +07:00
)
2023-12-17 20:31:40 +07:00
type GotifyMessage struct {
Title string `json:"title"`
Priority string `json:"priority"`
Message string `json:"message"`
}
2023-12-17 17:37:39 +07:00
type MediaFiles struct {
2023-12-17 16:45:10 +07:00
EventType string
SourcePath string
SourceFolder string
2023-12-17 18:32:29 +07:00
FileName string
2023-12-17 16:45:10 +07:00
InFolder bool
2023-12-17 17:37:39 +07:00
Arr string
2023-12-17 16:45:10 +07:00
}
2023-12-18 08:35:21 +07:00
type Config struct {
Gotify struct {
Enabled bool `yaml:"enabled"`
ServerURL string `yaml:"server_url"`
Token string `yaml:"token"`
} `yaml:"gotify"`
General struct {
RootFolders []string `yaml:"root_folders"`
} `yaml:"general"`
}
func NewConfig(configPath string) (*Config, error) {
// Create config structure
config := &Config{}
// Open config file
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
// Init new YAML decode
d := yaml.NewDecoder(file)
// Start YAML decoding from file
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
func (m *MediaFiles) SendGotify(message string, arr string, cfg *Config) {
2023-12-18 08:39:57 +07:00
if cfg.Gotify.Enabled {
http.PostForm(cfg.Gotify.ServerURL+"/message?token="+cfg.Gotify.Token,
url.Values{"message": {message}, "title": {"Deleting Media from " + arr}, "priority": {"10"}})
2024-09-24 15:31:11 +07:00
log.Printf("Message sent to Gotify: %s", message)
2023-12-18 08:39:57 +07:00
}
2023-12-17 20:31:40 +07:00
}
2023-12-18 08:35:21 +07:00
func (m *MediaFiles) IsInFolder(cfg *Config) {
folderList := cfg.General.RootFolders
// []string{"Movies", "4K-Movies", "Series", "4K-Series", "Kids", "Animes"}
2023-12-17 17:04:48 +07:00
2023-12-17 18:32:29 +07:00
SplitFolder := strings.Split(m.SourcePath, "/")
LastFolder := SplitFolder[len(SplitFolder)-2]
m.FileName = SplitFolder[len(SplitFolder)-1]
2023-12-17 18:27:22 +07:00
log.Printf("Found last folder to be: %v", LastFolder)
ContainsFolder := slices.Contains(folderList, LastFolder)
2023-12-17 16:59:54 +07:00
2023-12-17 17:12:46 +07:00
if !ContainsFolder {
2023-12-17 17:37:39 +07:00
m.InFolder = true
2023-12-17 18:32:29 +07:00
log.Printf("Movie %v is in a Folder\n", m.FileName)
2023-12-17 16:59:54 +07:00
} else {
2023-12-17 17:37:39 +07:00
m.InFolder = false
2023-12-17 18:32:29 +07:00
log.Printf("Movie %v is not in a Folder\n", m.FileName)
2023-12-17 16:45:10 +07:00
}
}
2023-12-17 15:05:00 +07:00
func main() {
2023-12-18 08:53:22 +07:00
// os.Setenv("radarr_moviefile_sourcepath", "/mnt/Multimedia/Download/PostProcess/Movies/Butchers.Crossing.2023.MULTi.1080p.WEB.x264-FW.mkv")
// os.Setenv("radarr_moviefile_sourcefolder", "/mnt/Multimedia/Download/PostProcess/Movies")
// os.Setenv("radarr_eventtype", "Download")
2023-12-17 21:05:17 +07:00
// os.Setenv("EventType", "Test")
2023-12-17 17:12:46 +07:00
2023-12-17 21:12:19 +07:00
if os.Getenv("radarr_eventtype") == "Test" || os.Getenv("sonarr_eventtype") == "Test" {
2023-12-17 21:08:55 +07:00
log.Println("Radarr/Sonarr is testing the script and it works")
os.Exit(0)
}
2023-12-17 15:19:23 +07:00
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
2023-12-18 08:35:21 +07:00
configPath := filepath.Join(exPath, "config.yml")
cfg, err := NewConfig(configPath)
if err != nil {
log.Fatal(err)
}
2023-12-17 15:19:23 +07:00
f, err := os.OpenFile(exPath+"/log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
2023-12-17 15:05:00 +07:00
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
2023-12-17 16:45:10 +07:00
RadarrEventType := os.Getenv("radarr_eventtype")
SonarrEventType := os.Getenv("sonarr_eventtype")
2023-12-17 17:37:39 +07:00
m := &MediaFiles{}
2023-12-17 16:45:10 +07:00
if RadarrEventType != "" {
2023-12-17 18:27:22 +07:00
m.EventType = os.Getenv("radarr_eventtype")
2023-12-17 17:37:39 +07:00
m.SourcePath = os.Getenv("radarr_moviefile_sourcepath")
2024-01-12 10:23:57 +07:00
log.Printf("---------------------------------------------------------\nThe Source Path is: %v", m.SourcePath)
2023-12-17 17:37:39 +07:00
m.SourceFolder = os.Getenv("radarr_moviefile_sourcefolder")
2023-12-17 17:59:14 +07:00
log.Printf("The Source Folder is: %v", m.SourceFolder)
2023-12-17 18:27:22 +07:00
m.Arr = "Radarr"
2023-12-17 17:37:39 +07:00
2023-12-17 16:45:10 +07:00
}
if SonarrEventType != "" {
2023-12-17 17:37:39 +07:00
m.EventType = os.Getenv("sonarr_eventtype")
m.SourcePath = os.Getenv("sonarr_episodefile_sourcepath")
2024-01-12 10:23:57 +07:00
log.Printf("---------------------------------------------------------\nThe Source Path is: %v", m.SourcePath)
2023-12-18 15:38:57 +07:00
m.SourceFolder = os.Getenv("sonarr_episodefile_sourcefolder")
2023-12-17 17:59:14 +07:00
log.Printf("The Source Folder is: %v", m.SourceFolder)
2023-12-17 18:27:22 +07:00
m.Arr = "Sonarr"
2023-12-17 17:37:39 +07:00
}
2023-12-18 08:35:21 +07:00
m.IsInFolder(cfg)
2023-12-17 17:37:39 +07:00
if m.InFolder {
f, err := os.Open(m.SourceFolder)
if err != nil {
log.Println(err)
return
}
files, err := f.Readdir(0)
if err != nil {
log.Println(err)
return
}
2023-12-17 17:47:40 +07:00
mkvCount := 0
2023-12-17 17:37:39 +07:00
for _, v := range files {
2023-12-17 17:47:40 +07:00
log.Println("Found file: " + v.Name())
2024-01-12 10:09:22 +07:00
if filepath.Base(v.Name()) == "sample.mkv" {
os.Remove(m.SourceFolder + "/" + v.Name())
}
2023-12-17 17:47:40 +07:00
if filepath.Ext(v.Name()) == ".mkv" {
mkvCount += 1
2023-12-17 19:40:19 +07:00
} else {
os.Remove(m.SourceFolder + "/" + v.Name())
2023-12-18 08:53:22 +07:00
log.Printf("Deleting non MKV file: %v", m.SourceFolder+"/"+v.Name())
2023-12-17 17:47:40 +07:00
}
2023-12-17 17:37:39 +07:00
}
2023-12-17 19:40:19 +07:00
if mkvCount > 1 {
log.Printf("Found %v MKV files in the folder, deleting only %v", mkvCount, m.SourcePath)
os.Remove(m.SourcePath)
} else {
2023-12-18 08:53:22 +07:00
log.Printf("Found only one MKV files in the folder, deleting the folder %v", m.SourceFolder)
2023-12-17 19:40:19 +07:00
os.RemoveAll(m.SourceFolder)
}
2023-12-17 17:59:14 +07:00
// if not in folder just delete the file
} else {
os.RemoveAll(m.SourcePath)
log.Printf("Deleting the file %v", m.SourcePath)
2023-12-17 16:45:10 +07:00
}
2023-12-18 08:35:21 +07:00
m.SendGotify("Deleting Source Path"+m.SourcePath, m.Arr, cfg)
2023-12-17 20:31:40 +07:00
2023-12-17 15:05:00 +07:00
}