Compare commits

..

No commits in common. "master" and "matt/#1" have entirely different histories.

7 changed files with 33 additions and 83 deletions

View File

@ -3,9 +3,6 @@ package main
import (
"flag"
"log"
"sort"
"strconv"
"strings"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/communicator"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/config"
@ -16,16 +13,14 @@ import (
func main() {
var c string
var days int
var sectionID string
var sectionID int
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.StringVar(&sectionID, "sectionid", "", "Plex Section ID. Multiples can be specified (separated by a comma). Ex: 1,2")
flag.IntVar(&sectionID, "sectionid", 0, "Plex Section ID")
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()
@ -33,7 +28,7 @@ func main() {
if c == "" {
log.Fatal("You need to specify a configuration file.")
}
if sectionID == "" {
if sectionID == 0 {
log.Fatal("You need to specify a section ID for Plex.")
}
@ -42,47 +37,32 @@ func main() {
log.Fatal(err)
}
sectionIds := strings.Split(sectionID, ",")
titlesFullList := make([]string, 0)
libraryType := locator.GetLibraryType(cfg, sectionID)
for _, section := range sectionIds {
sectionIDConv, _ := strconv.Atoi(section)
libraryType := locator.GetLibraryType(cfg, sectionIDConv)
ids, titles := locator.GetTitles(cfg, sectionID, days)
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 check {
err = communicator.TelegramPost(cfg, titles)
if err != nil {
log.Fatal(err)
}
}
if check {
sort.Strings(titlesFullList)
if text {
communicator.StdoutPost(titlesFullList)
} else {
err = communicator.TelegramPost(cfg, titlesFullList)
if delete {
if libraryType == "movie" {
files := eraser.LookupMovieFileLocation(cfg, ids)
err = eraser.DeleteFiles(delete, files)
if err != nil {
log.Fatal(err)
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)
// }
}
}
}

View File

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

3
go.mod
View File

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

View File

@ -46,14 +46,3 @@ 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,7 +9,8 @@ import (
// Config - This struct will hold configuration components.
type Config struct {
PlexPyURL string `json:"plexPyURL"`
BaseURL string `json:"baseURL"`
PlexPyContext string `json:"plexPyContext"`
PlexPyAPIKey string `json:"plexPyAPIKey"`
PlexToken string `json:"plexToken"`
PlexHost string `json:"plexHost"`
@ -17,9 +18,8 @@ type Config struct {
TelegramToken string `json:"telegramToken"`
TelegramChatID string `json:"telegramChatID"`
ServerName string `json:"serverName"`
SonarrURL string `json:"sonarrURL"`
SonarrContext string `json:"sonarrContext"`
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%d%s%s", config.SonarrURL, "/api/series/", i, "/?deleteFiles=true&apikey=", config.SonarrAPIKey)
sonarrURL := fmt.Sprintf("%s%s%s%d%s%s", config.BaseURL, config.SonarrContext, "/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.Println(err)
log.Fatal(err)
}
}
}

View File

@ -9,7 +9,6 @@ import (
"net/http"
"sort"
"strconv"
"strings"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/config"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/model"
@ -56,7 +55,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%d", config.PlexPyURL, "/api/v2?apikey=", config.PlexPyAPIKey, "&cmd=get_library&section_id=", sectionID)
countURL := fmt.Sprintf("%s%s%s%s%s%d", config.BaseURL, config.PlexPyContext, "/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)
@ -90,7 +89,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%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)
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)
req, err := http.NewRequest(http.MethodGet, titlesURL, nil)
if err != nil {
@ -122,20 +121,7 @@ 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)
@ -162,7 +148,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", config.SonarrURL, "/api/series?apikey=", config.SonarrAPIKey)
sonarrURL := fmt.Sprintf("%s%s%s%s", config.BaseURL, config.SonarrContext, "/api/series?apikey=", config.SonarrAPIKey)
req, err := http.NewRequest(http.MethodGet, sonarrURL, nil)
if err != nil {
log.Fatal(err)