DeleteArr/DeleteArr.go

170 lines
4.1 KiB
Go
Raw Normal View History

2023-12-17 15:05:00 +07:00
package main
import (
2023-12-17 20:31:40 +07:00
"bytes"
"encoding/json"
2023-12-17 15:05:00 +07:00
"log"
2023-12-17 20:31:40 +07:00
"net/http"
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-17 20:31:40 +07:00
"time"
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-17 20:31:40 +07:00
func (m *MediaFiles) SendGotify(message string, arr string) {
url := "https://gotify.rouggy.com/message?token=AKMj5SsZblmpAJ_"
gm := GotifyMessage{
Title: "Deleting Media from " + arr,
Priority: "10",
Message: message,
}
marshalled, err := json.Marshal(gm)
if err != nil {
log.Fatalf("impossible to marshall Gotify Message: %s", err)
}
req, err := http.NewRequest("POST", url, bytes.NewReader(marshalled))
if err != nil {
log.Fatalf("impossible to build request: %s", err)
}
req.Header.Set("Content-Type", "application/json")
// create http client
// do not forget to set timeout; otherwise, no timeout!
client := http.Client{Timeout: 10 * time.Second}
// send the request
res, err := client.Do(req)
if err != nil {
log.Fatalf("impossible to send request: %s", err)
}
log.Printf("status Code: %d", res.StatusCode)
}
2023-12-17 17:37:39 +07:00
func (m *MediaFiles) IsInFolder() {
2023-12-17 18:27:22 +07:00
folderList := []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-17 18:28:50 +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 17:12:46 +07:00
2023-12-17 15:19:23 +07:00
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
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")
2023-12-17 17:59:14 +07:00
log.Printf("The 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")
2023-12-17 17:59:14 +07:00
log.Printf("The 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 = "Sonarr"
2023-12-17 17:37:39 +07:00
}
m.IsInFolder()
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())
if filepath.Ext(v.Name()) == ".mkv" {
mkvCount += 1
2023-12-17 19:40:19 +07:00
} else {
os.Remove(m.SourceFolder + "/" + v.Name())
log.Printf("Deleting file 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 {
log.Printf("Found only one MKV files in the folder, deleting thoe folder %v", m.SourceFolder)
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-17 20:31:40 +07:00
m.SendGotify("Deleting Source Path"+m.SourcePath, m.Arr)
2023-12-17 15:05:00 +07:00
}