Pushing a bunch of hosuekeeper updates

This commit is contained in:
Matt Burchett
2018-11-14 03:51:03 +00:00
parent 254933c2f0
commit e9280412a7
7 changed files with 204 additions and 39 deletions

41
pkg/config/config.go Normal file
View File

@ -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
}

View File

@ -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&section_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 {
}

View File

@ -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"`
}

11
pkg/util/epoch.go Normal file
View File

@ -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
}