Housekeeper/pkg/locator/locator.go

200 lines
4.7 KiB
Go
Raw Normal View History

2018-10-16 17:16:58 +00:00
package locator
2018-11-14 03:51:03 +00:00
import (
"encoding/json"
"encoding/xml"
2018-11-14 03:51:03 +00:00
"fmt"
"io/ioutil"
"log"
"net/http"
2018-11-14 23:37:57 +00:00
"sort"
2018-11-14 21:43:38 +00:00
"strconv"
2020-12-11 21:39:17 +00:00
"strings"
2018-11-14 03:51:03 +00:00
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/config"
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/model"
2018-11-14 21:43:38 +00:00
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/util"
2018-11-14 03:51:03 +00:00
)
// GetLibraryType checks to see what type the library is.
func GetLibraryType(config config.Config, sectionID int) string {
typeURL := fmt.Sprintf("%s:%d%s%d%s%s", config.PlexHost, config.PlexPort, "/library/sections/", sectionID, "/?X-Plex-Token=", config.PlexToken)
req, err := http.NewRequest(http.MethodGet, typeURL, nil)
httpClient := http.Client{}
req.Header.Set("User-Agent", "Housekeeper")
res, getErr := httpClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
if err != nil {
log.Fatal(err)
}
typeModel := model.XMLPlexLibraryType{}
xml.Unmarshal(body, &typeModel)
var libraryType string
if typeModel.Thumb == "/:/resources/movie.png" {
libraryType = "movie"
} else if typeModel.Thumb == "/:/resources/show.png" {
libraryType = "show"
} else {
log.Fatal("Unsupported library type found. This app only supports movies and shows.")
}
return libraryType
}
2018-11-15 05:34:24 +00:00
// GetCount will gather a count of media in a specific library, required for GetTitles.
2018-11-14 03:51:03 +00:00
func GetCount(config config.Config, sectionID int) int {
2021-05-25 11:37:59 +00:00
countURL := fmt.Sprintf("%s%s%s%s%d", config.PlexPyURL, "/api/v2?apikey=", config.PlexPyAPIKey, "&cmd=get_library&section_id=", sectionID)
2018-11-14 03:51:03 +00:00
req, err := http.NewRequest(http.MethodGet, countURL, nil)
if err != nil {
log.Fatal(err)
}
httpClient := http.Client{}
req.Header.Set("User-Agent", "Housekeeper")
res, getErr := httpClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
countModel := model.PlexPyLibraryInfo{}
jsonErr := json.Unmarshal(body, &countModel)
if jsonErr != nil {
log.Fatal(jsonErr)
}
count := countModel.Response.Data.Count
return count
}
2018-11-14 21:43:38 +00:00
// GetTitles will gather a list of information from the media in the library, based on the previous count.
2018-11-15 05:34:24 +00:00
func GetTitles(config config.Config, sectionID int, days int) ([]int, []string) {
2018-11-14 21:43:38 +00:00
count := GetCount(config, sectionID)
2021-05-25 11:37:59 +00:00
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)
2018-11-14 21:43:38 +00:00
req, err := http.NewRequest(http.MethodGet, titlesURL, nil)
if err != nil {
log.Fatal(err)
}
httpClient := http.Client{}
req.Header.Set("User-Agent", "Housekeeper")
res, getErr := httpClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
titleModel := model.PlexPyMediaInfo{}
jsonErr := json.Unmarshal(body, &titleModel)
if jsonErr != nil {
log.Fatal(jsonErr)
}
data := titleModel.Response.Data.Data
2018-11-14 23:37:57 +00:00
titles := make([]string, 0)
ids := make([]int, 0)
2018-11-14 21:43:38 +00:00
epoch := util.SubtractedEpoch(days)
2020-12-11 21:39:17 +00:00
exclude := strings.Split(config.ExcludeList, ",")
var breakOut bool
2018-11-14 21:43:38 +00:00
for _, i := range data {
2020-12-11 21:39:17 +00:00
for _, ex := range exclude {
if strings.Contains(i.Title, ex) {
breakOut = true
}
}
if breakOut {
breakOut = false
continue
}
2018-11-20 17:46:42 +00:00
if int64(i.LastPlayed) <= epoch && int64(i.LastPlayed) != 0 {
2018-11-14 23:37:57 +00:00
titles = append(titles, i.Title)
strirk, err := strconv.Atoi(i.RatingKey)
if err != nil {
log.Fatal(err)
}
ids = append(ids, strirk)
2018-11-14 21:43:38 +00:00
}
2018-11-20 17:46:42 +00:00
if i.LastPlayed <= 0 {
if i.AddedAt <= epoch {
2018-11-14 23:37:57 +00:00
titles = append(titles, i.Title)
strirk, err := strconv.Atoi(i.RatingKey)
if err != nil {
log.Fatal(err)
}
ids = append(ids, strirk)
2018-11-14 21:43:38 +00:00
}
}
}
2018-11-14 23:37:57 +00:00
sort.Strings(titles)
2018-11-15 05:34:24 +00:00
return ids, titles
2018-11-14 03:51:03 +00:00
}
2018-11-21 00:43:18 +00:00
// GetSonarrIDs gets the IDs to delete from the title list in PlexPy.
func GetSonarrIDs(config config.Config, titles []string) []int {
ids := make([]int, 0)
2021-05-25 11:37:59 +00:00
sonarrURL := fmt.Sprintf("%s%s%s", config.SonarrURL, "/api/series?apikey=", config.SonarrAPIKey)
2018-11-21 00:43:18 +00:00
req, err := http.NewRequest(http.MethodGet, sonarrURL, nil)
if err != nil {
log.Fatal(err)
}
httpClient := http.Client{}
req.Header.Set("User-Agent", "Housekeeper")
res, getErr := httpClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
sonarrModel := model.SonarrSeries{}
jsonErr := json.Unmarshal(body, &sonarrModel)
if jsonErr != nil {
log.Fatal(jsonErr)
}
for _, r := range sonarrModel {
for _, s := range titles {
if r.Title == s {
ids = append(ids, r.ID)
}
}
}
return ids
}