From e9280412a72a332bfd4ecd353e3c9a200c396958 Mon Sep 17 00:00:00 2001 From: Matt Burchett Date: Wed, 14 Nov 2018 03:51:03 +0000 Subject: [PATCH] Pushing a bunch of hosuekeeper updates --- .gitignore | 0 cmd/main.go | 78 +++++++++++++++++++------------------- config.json | 5 +++ pkg/config/config.go | 41 ++++++++++++++++++++ pkg/locator/locator.go | 46 ++++++++++++++++++++++ pkg/model/locator_model.go | 62 ++++++++++++++++++++++++++++++ pkg/util/epoch.go | 11 ++++++ 7 files changed, 204 insertions(+), 39 deletions(-) create mode 100644 .gitignore create mode 100644 config.json create mode 100644 pkg/config/config.go create mode 100644 pkg/model/locator_model.go create mode 100644 pkg/util/epoch.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/cmd/main.go b/cmd/main.go index effdf16..6b21f65 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,58 +2,58 @@ package main import ( "flag" - "fmt" "log" - "os" - "path/filepath" - "time" + + "git.linuxrocker.com/mattburchett/Housekeeper/pkg/config" + "git.linuxrocker.com/mattburchett/Housekeeper/pkg/locator" ) -func isOlder(t time.Time, days int) bool { - return time.Now().Sub(t) > 1*time.Hour -} +// func getFiles(location string, days int) ([]string, error) { +// var files []string +// err := filepath.Walk(location, func(path string, info os.FileInfo, err error) error { +// files = append(files, path) +// return nil +// }) +// if err != nil { +// log.Fatal(err) +// } -func getFiles(location string, days int) ([]string, error) { - var files []string - err := filepath.Walk(location, func(path string, info os.FileInfo, err error) error { - files = append(files, path) - return nil - }) - if err != nil { - log.Fatal(err) - } +// test := make([]string, 0) - test := make([]string, 0) +// for _, file := range files { +// at, err := os.Stat(file) +// if err != nil { +// log.Fatal(err) +// } +// if isOlder(at.ModTime(), days) { +// test = append(test, file) +// } - for _, file := range files { - at, err := os.Stat(file) - if err != nil { - log.Fatal(err) - } - if isOlder(at.ModTime(), days) { - test = append(test, file) - } +// } - } +// return test, err - return test, err - -} +// } func main() { - var location string + var c string var days int - flag.StringVar(&location, "location", "", "location to scan") - flag.IntVar(&days, "days", 0, "days to poll") - flag.Parse() + var sectionID int - files, err := getFiles(location, days) + flag.StringVar(&c, "c", "", "Configuration to load") + flag.IntVar(&days, "days", 0, "days to poll") + flag.IntVar(§ionID, "sectionid", 0, "pick a section ID") + flag.Parse() + if c == "" { + log.Fatal("You need to specify a configuration file.") + } + if sectionID == 0 { + log.Fatal("You need to specify a section ID for Plex.") + } + + cfg, err := config.GetConfig(c) if err != nil { log.Fatal(err) } - - for _, file := range files { - fmt.Println(file) - } - + locator.GetCount(cfg, sectionID) } diff --git a/config.json b/config.json new file mode 100644 index 0000000..2c77afc --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "baseURL": "http://dvr.linuxrocker.com", + "plexPyContext": "/plexpy", + "plexPyAPIKey": "6b707d0439a2449a9bcaed8c6f042de0" +} \ No newline at end of file diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..0d49006 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,41 @@ +package config + +import ( + "encoding/json" + "fmt" + "log" + "os" +) + +// Config - This struct will hold configuration components. +type Config struct { + BaseURL string `json:"baseURL"` + PlexPyContext string `json:"plexPyContext"` + PlexPyAPIKey string `json:"plexPyAPIKey"` +} + +//GetConfig gets the configuration values for the api using the file in the supplied configPath. +func GetConfig(configPath string) (Config, error) { + if _, err := os.Stat(configPath); os.IsNotExist(err) { + return Config{}, fmt.Errorf("could not find the config file at path %s", configPath) + } + log.Println("Loading Configuration File: " + configPath) + return loadConfigFromFile(configPath) +} + +//if the config loaded from the file errors, no defaults will be loaded and the app will exit. +func loadConfigFromFile(configPath string) (conf Config, err error) { + file, err := os.Open(configPath) + if err != nil { + log.Printf("Error opening config file: %v", err) + } else { + defer file.Close() + + err = json.NewDecoder(file).Decode(&conf) + if err != nil { + log.Printf("Error decoding config file: %v", err) + } + } + + return conf, err +} diff --git a/pkg/locator/locator.go b/pkg/locator/locator.go index 7133f72..db0063d 100644 --- a/pkg/locator/locator.go +++ b/pkg/locator/locator.go @@ -1 +1,47 @@ package locator + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + + "git.linuxrocker.com/mattburchett/Housekeeper/pkg/config" + "git.linuxrocker.com/mattburchett/Housekeeper/pkg/model" +) + +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§ion_id=", sectionID) + 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 +} + +func getTitles(config config.Config, sectionID int) string { + +} diff --git a/pkg/model/locator_model.go b/pkg/model/locator_model.go new file mode 100644 index 0000000..e0ce729 --- /dev/null +++ b/pkg/model/locator_model.go @@ -0,0 +1,62 @@ +package model + +// PlexPyLibraryInfo will gather all the library related info. We really just need the count from this... +type PlexPyLibraryInfo struct { + Response struct { + Message interface{} `json:"message"` + Data struct { + Count int `json:"count"` + SectionID int `json:"section_id"` + SectionName string `json:"section_name"` + LibraryArt string `json:"library_art"` + ParentCount interface{} `json:"parent_count"` + SectionType string `json:"section_type"` + DoNotifyCreated int `json:"do_notify_created"` + KeepHistory int `json:"keep_history"` + ChildCount interface{} `json:"child_count"` + LibraryThumb string `json:"library_thumb"` + DoNotify int `json:"do_notify"` + } `json:"data"` + Result string `json:"result"` + } `json:"response"` +} + +// PlexPyMediaInfo - This is the information for the Media Library and related media. +type PlexPyMediaInfo struct { + Response struct { + Message interface{} `json:"message"` + Data struct { + Draw int `json:"draw"` + RecordsTotal string `json:"recordsTotal"` + TotalFileSize int64 `json:"total_file_size"` + RecordsFiltered int `json:"recordsFiltered"` + FilteredFileSize int64 `json:"filtered_file_size"` + Data []struct { + Year string `json:"year"` + SortTitle string `json:"sort_title"` + ParentRatingKey string `json:"parent_rating_key"` + AudioCodec string `json:"audio_codec"` + FileSize string `json:"file_size"` + RatingKey string `json:"rating_key"` + Container string `json:"container"` + Thumb string `json:"thumb"` + Title string `json:"title"` + SectionType string `json:"section_type"` + MediaType string `json:"media_type"` + VideoResolution string `json:"video_resolution"` + GrandparentRatingKey string `json:"grandparent_rating_key"` + AudioChannels string `json:"audio_channels"` + LastPlayed int64 `json:"last_played"` + SectionID int `json:"section_id"` + PlayCount int `json:"play_count"` + Bitrate string `json:"bitrate"` + VideoFramerate string `json:"video_framerate"` + MediaIndex string `json:"media_index"` + AddedAt string `json:"added_at"` + VideoCodec string `json:"video_codec"` + ParentMediaIndex string `json:"parent_media_index"` + } `json:"data"` + } `json:"data"` + Result string `json:"result"` + } `json:"response"` +} diff --git a/pkg/util/epoch.go b/pkg/util/epoch.go new file mode 100644 index 0000000..db2fe98 --- /dev/null +++ b/pkg/util/epoch.go @@ -0,0 +1,11 @@ +package util + +import "time" + +func SubtractedEpoch(days int) int64 { + now := time.Now() + unix := now.Unix() + seconds := int64(days * 86400) + epoch := int64(unix - seconds) + return epoch +}