2018-10-16 17:16:58 +00:00
|
|
|
package eraser
|
2018-11-15 05:34:24 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-21 00:43:18 +00:00
|
|
|
"encoding/json"
|
2018-11-15 05:34:24 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-21 00:43:18 +00:00
|
|
|
"strings"
|
2018-11-15 05:34:24 +00:00
|
|
|
|
|
|
|
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/config"
|
|
|
|
"git.linuxrocker.com/mattburchett/Housekeeper/pkg/model"
|
|
|
|
)
|
|
|
|
|
2018-11-20 19:50:47 +00:00
|
|
|
// LookupMovieFileLocation will gather a list of Information based on IDs returned by locator.GetTitles
|
|
|
|
func LookupMovieFileLocation(config config.Config, ids []int) []string {
|
2018-11-15 05:34:24 +00:00
|
|
|
fileList := make([]string, 0)
|
|
|
|
|
|
|
|
for _, i := range ids {
|
|
|
|
plexURL := fmt.Sprintf("%s:%d%s%d%s%s", config.PlexHost, config.PlexPort, "/library/metadata/", i, "/?X-Plex-Token=", config.PlexToken)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, plexURL, 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)
|
|
|
|
}
|
2018-11-20 19:50:47 +00:00
|
|
|
plexModel := model.XMLPlexMovieAPI{}
|
2018-11-15 05:34:24 +00:00
|
|
|
xml.Unmarshal(body, &plexModel)
|
|
|
|
fileList = append(fileList, filepath.Dir(plexModel.Video.Media.Part.File))
|
|
|
|
}
|
|
|
|
return fileList
|
|
|
|
}
|
|
|
|
|
2018-11-20 19:50:47 +00:00
|
|
|
// LookupTVFileLocation will gather a list of Information based on IDs returned by locator.GetTitles
|
|
|
|
func LookupTVFileLocation(config config.Config, ids []int) []string {
|
|
|
|
fileList := make([]string, 0)
|
2018-11-20 20:26:47 +00:00
|
|
|
m := make(map[string]bool)
|
2018-11-20 20:29:14 +00:00
|
|
|
results := make([]string, 0)
|
2018-11-20 19:50:47 +00:00
|
|
|
|
|
|
|
for _, i := range ids {
|
2018-11-20 19:51:47 +00:00
|
|
|
plexURL := fmt.Sprintf("%s:%d%s%d%s%s", config.PlexHost, config.PlexPort, "/library/metadata/", i, "/allLeaves/?X-Plex-Token=", config.PlexToken)
|
2018-11-20 19:50:47 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, plexURL, 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)
|
|
|
|
}
|
|
|
|
plexModel := model.XMLPlexTVAPI{}
|
|
|
|
xml.Unmarshal(body, &plexModel)
|
|
|
|
|
|
|
|
plexTV := plexModel.Video
|
|
|
|
|
|
|
|
for _, i := range plexTV {
|
2018-11-20 20:28:37 +00:00
|
|
|
fileList = append(fileList, filepath.Dir(filepath.Dir(i.Media.Part.File)))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range fileList {
|
|
|
|
if _, ok := m[r]; !ok {
|
|
|
|
m[r] = true
|
|
|
|
results = append(results, r)
|
2018-11-20 20:05:51 +00:00
|
|
|
}
|
2018-11-20 19:50:47 +00:00
|
|
|
}
|
2018-11-20 20:20:34 +00:00
|
|
|
|
2018-11-20 19:50:47 +00:00
|
|
|
}
|
2018-11-20 20:28:51 +00:00
|
|
|
return results
|
2018-11-20 20:14:40 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 00:43:18 +00:00
|
|
|
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)
|
|
|
|
req, err := http.NewRequest(http.MethodDelete, 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteModel := model.SonarrResponse{}
|
|
|
|
jsonErr := json.Unmarshal(body, &deleteModel)
|
|
|
|
if jsonErr != nil {
|
|
|
|
log.Fatal(jsonErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains("does not exist", deleteModel.Message) {
|
|
|
|
log.Printf("The following ID does not exist: %v", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 23:07:22 +00:00
|
|
|
// DeleteFiles will actually perform the deletion.
|
|
|
|
func DeleteFiles(delete bool, files []string) error {
|
2018-11-15 05:34:24 +00:00
|
|
|
var err error
|
|
|
|
if delete {
|
|
|
|
for _, i := range files {
|
|
|
|
fmt.Printf("Removing %v\n", i)
|
|
|
|
err = os.RemoveAll(i)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|