Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multiple pvrs #6

Merged
merged 4 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .github/workflows/lint.yml

This file was deleted.

21 changes: 8 additions & 13 deletions cmd/plexarr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
globals

// flags
PVR string `required:"1" type:"string" help:"PVR to match from"`
PVR []string `required:"1" type:"string" help:"PVR to match from"`
Library []string `required:"1" type:"string" help:"Plex Library to match against"`

Config string `type:"path" default:"${config_file}" env:"PLEXARR_CONFIG" help:"Config file path"`
Expand Down Expand Up @@ -151,7 +151,7 @@ func main() {

// get library items
l := log.With().
Str("pvr", cli.PVR).
Strs("pvrs", cli.PVR).
Bool("dry_run", cli.DryRun).
Logger()

Expand Down Expand Up @@ -196,14 +196,7 @@ func main() {
}

// retrieve items from pvr
pvr, err := getPvr(cli.PVR, cfg, plexItems)
if err != nil {
log.Fatal().
Err(err).
Msg("Failed initialising pvr")
}

pvrItems, err := pvr.GetLibraryItems()
pvrItems, err := getPvrItems(cli.PVR, cfg, plexItems)
if err != nil {
log.Fatal().
Err(err).
Expand All @@ -214,9 +207,11 @@ func main() {
l.Fatal().Msg("No pvr library items retrieved?")
}

l.Info().
Int("count", len(pvrItems)).
Msg("Retrieved pvr library items")
if len(cli.PVR) > 1 {
l.Info().
Int("count", len(pvrItems)).
Msg("Retrieved all pvr library items")
}

// track items not matched (display debug log)
plexItemsNotFound := make([]plex.MediaItem, 0)
Expand Down
4 changes: 2 additions & 2 deletions cmd/plexarr/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func getPlexLibraryItems(p *plex.Client, libraries []string) ([]plexLibraryItem,
// get library items
l := log.With().
Str("library", library).
Str("pvr", cli.PVR).
Strs("pvrs", cli.PVR).
Bool("dry_run", cli.DryRun).
Logger()

Expand Down Expand Up @@ -84,7 +84,7 @@ func findDuplicateItems(items []plex.MediaItem) ([]plex.MediaItem, error) {
func splitDuplicates(p *plex.Client, library plexLibraryItem) (int, error) {
l := log.With().
Str("library", library.Name).
Str("pvr", cli.PVR).
Strs("pvrs", cli.PVR).
Bool("dry_run", cli.DryRun).
Logger()

Expand Down
54 changes: 54 additions & 0 deletions cmd/plexarr/pvr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/l3uddz/plexarr"
"github.com/l3uddz/plexarr/pvrs/radarr"
"github.com/l3uddz/plexarr/pvrs/sonarr"
"github.com/rs/zerolog/log"
"strings"
)

Expand Down Expand Up @@ -56,3 +57,56 @@ func getPvr(name string, cfg config, libraries []plexLibraryItem) (plexarr.Pvr,

return nil, errors.New("pvr not found")
}

func getPvrItems(names []string, cfg config, plexItems []plexLibraryItem) (map[string]plexarr.PvrItem, error) {
pvrItems := make(map[string]plexarr.PvrItem)

// iterate pvr names
for _, pvrName := range names {
// get pvr object
pvr, err := getPvr(pvrName, cfg, plexItems)
if err != nil {
return nil, fmt.Errorf("initialise pvr: %v: %w", pvrName, err)
}

// retrieve pvr items
items, err := pvr.GetLibraryItems()
if err != nil {
return nil, fmt.Errorf("retrieve pvr library items: %v: %w", pvrName, err)
} else if len(items) == 0 {
return nil, fmt.Errorf("retrieve pvr library items: %v: no items found", pvrName)
}

itemsSkipped := 0
itemsAdded := 0

pl := log.With().
Str("pvr", pvrName).
Logger()

// process pvr items
for key, item := range items {
// does key already exist in pvrItems (have we seen this path before?)
if _, exists := pvrItems[key]; exists {
// this key (path) already exists, ignore it
pl.Warn().
Interface("item", item).
Msg("Path is not unique to this pvr, skipping item(s)")

itemsSkipped++
delete(pvrItems, key)
continue
}

itemsAdded++
pvrItems[key] = item
}

pl.Info().
Int("skipped", itemsSkipped).
Int("added", itemsAdded).
Msg("Retrieved pvr library items")
}

return pvrItems, nil
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.14

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/alecthomas/kong v0.2.11
github.com/alecthomas/kong v0.2.12
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/mattn/go-sqlite3 v1.14.3
github.com/mattn/go-sqlite3 v1.14.5
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.20.0
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v2 v2.4.0
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/kong v0.2.11 h1:RKeJXXWfg9N47RYfMm0+igkxBCTF4bzbneAxaqid0c4=
github.com/alecthomas/kong v0.2.11/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/alecthomas/kong v0.2.12 h1:X3kkCOXGUNzLmiu+nQtoxWqj4U2a39MpSJR3QdQXOwI=
github.com/alecthomas/kong v0.2.12/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/mattn/go-sqlite3 v1.14.3 h1:j7a/xn1U6TKA/PHHxqZuzh64CdtRc7rU9M+AvkOl5bA=
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand All @@ -26,14 +26,14 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c h1:38q6VNPWR010vN82/SB121GujZNIfAUb4YttE2rhGuc=
golang.org/x/sys v0.0.0-20200926100807-9d91bd62050c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 h1:+CBz4km/0KPU3RGTwARGh/noP3bEwtHcq+0YcBQM2JQ=
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=