Compare commits

...

11 Commits

7 changed files with 83 additions and 33 deletions

View File

@ -3,6 +3,9 @@ package main
import (
"flag"
"log"
"sort"
"strconv"
"strings"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/communicator"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/config"
@ -13,14 +16,16 @@ import (
func main() {
var c string
var days int
var sectionID int
var sectionID string
var check bool
var text bool
var delete bool
flag.StringVar(&c, "config", "", "Configuration to load")
flag.IntVar(&days, "days", 0, "How many days of inactivity to look for on Plex.")
flag.IntVar(&sectionID, "sectionid", 0, "Plex Section ID")
flag.StringVar(&sectionID, "sectionid", "", "Plex Section ID. Multiples can be specified (separated by a comma). Ex: 1,2")
flag.BoolVar(&check, "check", true, "Perform only a check. This will send the message out to Telegram with what can be removed. Does not delete.")
flag.BoolVar(&text, "text", false, "This will override the communication to Telegram and print to stdout.")
flag.BoolVar(&delete, "delete", false, "Perform the delete task.")
flag.Parse()
@ -28,7 +33,7 @@ func main() {
if c == "" {
log.Fatal("You need to specify a configuration file.")
}
if sectionID == 0 {
if sectionID == "" {
log.Fatal("You need to specify a section ID for Plex.")
}
@ -37,32 +42,47 @@ func main() {
log.Fatal(err)
}
libraryType := locator.GetLibraryType(cfg, sectionID)
sectionIds := strings.Split(sectionID, ",")
titlesFullList := make([]string, 0)
ids, titles := locator.GetTitles(cfg, sectionID, days)
for _, section := range sectionIds {
sectionIDConv, _ := strconv.Atoi(section)
libraryType := locator.GetLibraryType(cfg, sectionIDConv)
if check {
err = communicator.TelegramPost(cfg, titles)
if err != nil {
log.Fatal(err)
ids, titles := locator.GetTitles(cfg, sectionIDConv, days)
for _, title := range titles {
titlesFullList = append(titlesFullList, title)
}
if delete {
if libraryType == "movie" {
files := eraser.LookupMovieFileLocation(cfg, ids)
err = eraser.DeleteFiles(delete, files)
if err != nil {
log.Println(err)
}
} else if libraryType == "show" {
// files := eraser.LookupTVFileLocation(cfg, ids)
sonarrIDs := locator.GetSonarrIDs(cfg, titles)
eraser.DeleteSeriesFromSonarr(cfg, sonarrIDs)
// err = eraser.DeleteFiles(delete, files)
// if err != nil {
// log.Println(err)
// }
}
}
}
if delete {
if libraryType == "movie" {
files := eraser.LookupMovieFileLocation(cfg, ids)
err = eraser.DeleteFiles(delete, files)
if check {
sort.Strings(titlesFullList)
if text {
communicator.StdoutPost(titlesFullList)
} else {
err = communicator.TelegramPost(cfg, titlesFullList)
if err != nil {
log.Println(err)
log.Fatal(err)
}
} else if libraryType == "show" {
// files := eraser.LookupTVFileLocation(cfg, ids)
sonarrIDs := locator.GetSonarrIDs(cfg, titles)
eraser.DeleteSeriesFromSonarr(cfg, sonarrIDs)
// err = eraser.DeleteFiles(delete, files)
// if err != nil {
// log.Println(err)
// }
}
}
}

View File

@ -1,11 +1,13 @@
{
"baseURL": "http://dvr.example.com",
"plexPyContext": "/plexpy",
"plexPyURL": "http://dvr.example.com/plexpy",
"plexPyAPIKey": "abc1234",
"plexToken": "ABC1234ABC1234",
"plexHost": "http://192.168.1.1",
"plexPort": 32400,
"telegramToken": "123456789:ABCDEFG",
"telegramChatID": "12345678",
"serverName": "Plex"
"serverName": "Plex",
"sonarrURL": "http://dvr.example.com/tv",
"sonarrAPIKey": "abc1234",
"excludeList": "A,bravo,char"
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.linuxrocker.com/mattburchett/Housekeeper
go 1.15

View File

@ -46,3 +46,14 @@ func TelegramPost(config config.Config, titles []string) error {
return err
}
// StdoutPost will relay the titles out to stdout.
func StdoutPost(titles []string) {
if len(titles) != 0 {
for _, title := range titles {
fmt.Println(title)
}
} else {
fmt.Println("There are no titles. Nothing to display.")
}
}

View File

@ -9,8 +9,7 @@ import (
// Config - This struct will hold configuration components.
type Config struct {
BaseURL string `json:"baseURL"`
PlexPyContext string `json:"plexPyContext"`
PlexPyURL string `json:"plexPyURL"`
PlexPyAPIKey string `json:"plexPyAPIKey"`
PlexToken string `json:"plexToken"`
PlexHost string `json:"plexHost"`
@ -18,8 +17,9 @@ type Config struct {
TelegramToken string `json:"telegramToken"`
TelegramChatID string `json:"telegramChatID"`
ServerName string `json:"serverName"`
SonarrContext string `json:"sonarrContext"`
SonarrURL string `json:"sonarrURL"`
SonarrAPIKey string `json:"sonarrAPIKey"`
ExcludeList string `json:"excludeList"` // ExcludeList will be checked against any section.
}
//GetConfig gets the configuration values for the api using the file in the supplied configPath.

View File

@ -95,7 +95,7 @@ func LookupTVFileLocation(config config.Config, ids []int) []string {
func DeleteSeriesFromSonarr(config config.Config, ids []int) {
for _, i := range ids {
sonarrURL := fmt.Sprintf("%s%s%s%d%s%s", config.BaseURL, config.SonarrContext, "/api/series/", i, "/?deleteFiles=true&apikey=", config.SonarrAPIKey)
sonarrURL := fmt.Sprintf("%s%s%d%s%s", config.SonarrURL, "/api/series/", i, "/?deleteFiles=true&apikey=", config.SonarrAPIKey)
req, err := http.NewRequest(http.MethodDelete, sonarrURL, nil)
if err != nil {
log.Fatal(err)
@ -134,7 +134,7 @@ func DeleteFiles(delete bool, files []string) error {
fmt.Printf("Removing %v\n", i)
err = os.RemoveAll(i)
if err != nil {
log.Fatal(err)
log.Println(err)
}
}
}

View File

@ -9,6 +9,7 @@ import (
"net/http"
"sort"
"strconv"
"strings"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/config"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/model"
@ -55,7 +56,7 @@ func GetLibraryType(config config.Config, sectionID int) string {
// GetCount will gather a count of media in a specific library, required for GetTitles.
func GetCount(config config.Config, sectionID int) int {
countURL := fmt.Sprintf("%s%s%s%s%s%d", config.BaseURL, config.PlexPyContext, "/api/v2?apikey=", config.PlexPyAPIKey, "&cmd=get_library&section_id=", sectionID)
countURL := fmt.Sprintf("%s%s%s%s%d", config.PlexPyURL, "/api/v2?apikey=", config.PlexPyAPIKey, "&cmd=get_library&section_id=", sectionID)
req, err := http.NewRequest(http.MethodGet, countURL, nil)
if err != nil {
log.Fatal(err)
@ -89,7 +90,7 @@ func GetCount(config config.Config, sectionID int) int {
func GetTitles(config config.Config, sectionID int, days int) ([]int, []string) {
count := GetCount(config, sectionID)
titlesURL := fmt.Sprintf("%s%s%s%s%s%d%s%d", config.BaseURL, config.PlexPyContext, "/api/v2?apikey=", config.PlexPyAPIKey, "&cmd=get_library_media_info&section_id=", sectionID, "&order_column=last_played&refresh=true&order_dir=asc&length=", count)
titlesURL := fmt.Sprintf("%s%s%s%s%d%s%d", config.PlexPyURL, "/api/v2?apikey=", config.PlexPyAPIKey, "&cmd=get_library_media_info&section_id=", sectionID, "&order_column=last_played&refresh=true&order_dir=asc&length=", count)
req, err := http.NewRequest(http.MethodGet, titlesURL, nil)
if err != nil {
@ -121,7 +122,20 @@ func GetTitles(config config.Config, sectionID int, days int) ([]int, []string)
epoch := util.SubtractedEpoch(days)
exclude := strings.Split(config.ExcludeList, ",")
var breakOut bool
for _, i := range data {
for _, ex := range exclude {
if strings.Contains(i.Title, ex) {
breakOut = true
}
}
if breakOut {
breakOut = false
continue
}
if int64(i.LastPlayed) <= epoch && int64(i.LastPlayed) != 0 {
titles = append(titles, i.Title)
strirk, err := strconv.Atoi(i.RatingKey)
@ -148,7 +162,7 @@ func GetTitles(config config.Config, sectionID int, days int) ([]int, []string)
// GetSonarrIDs gets the IDs to delete from the title list in PlexPy.
func GetSonarrIDs(config config.Config, titles []string) []int {
ids := make([]int, 0)
sonarrURL := fmt.Sprintf("%s%s%s%s", config.BaseURL, config.SonarrContext, "/api/series?apikey=", config.SonarrAPIKey)
sonarrURL := fmt.Sprintf("%s%s%s", config.SonarrURL, "/api/series?apikey=", config.SonarrAPIKey)
req, err := http.NewRequest(http.MethodGet, sonarrURL, nil)
if err != nil {
log.Fatal(err)