Skip to content

Commit

Permalink
feat(sonarr): additional cli flag and bug fix (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz authored Sep 4, 2021
1 parent 2ed35d3 commit 6a78c7f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cmd/missarr/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
type SonarrCmd struct {
Limit int `default:"10" help:"How many items to search for before stopping"`
LastSearched time.Duration `default:"672h" help:"How long before an item can be searched again"`
LastAirDate time.Duration `default:"72h" help:"How long before an item can be considered missing based on air date"`
AllowSpecial bool `default:"false" help:"Dont consider specials as missing"`
SkipRefresh bool `default:"false" help:"Retrieve current missing from sonarr"`
}

Expand Down Expand Up @@ -42,7 +44,7 @@ func (r *SonarrCmd) Run(c *config, db *sql.DB, mg *migrate.Migrator) error {
Msg("Retrieved missing episodes")

// refresh datastore
us, rs, fs, err = sc.RefreshStore(se)
us, rs, fs, err = sc.RefreshStore(se, r.AllowSpecial, time.Now().Add(-r.LastAirDate))
if err != nil {
return fmt.Errorf("missing to store: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion sonarr/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ON CONFLICT (series, season) DO UPDATE SET
air_date = excluded.air_date
, search_date = CASE
WHEN excluded.air_date > series.air_date THEN NULL
ELSE COALESCE(series.search_date, excluded.search_date)
ELSE COALESCE(excluded.search_date, series.search_date)
END
`

Expand Down
12 changes: 11 additions & 1 deletion sonarr/ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c *Client) GetAll() ([]Series, error) {
return c.store.GetAll()
}

func (c *Client) RefreshStore(episodes []Episode) (int, int, []Series, error) {
func (c *Client) RefreshStore(episodes []Episode, allowSpecials bool, maxAirDate time.Time) (int, int, []Series, error) {
// sort episodes into series
sm := make(map[string]time.Time)
seasons := make([]Series, 0)
Expand All @@ -24,6 +24,16 @@ func (c *Client) RefreshStore(episodes []Episode) (int, int, []Series, error) {
continue
}

// skip specials
if !allowSpecials && e.SeasonNumber == 0 {
continue
}

// skip episode if the air date is not before the max air date
if e.AirDateUtc.After(maxAirDate) {
continue
}

// series season before?
k := fmt.Sprintf("%v_%v", e.SeriesId, e.SeasonNumber)
if _, ok := sm[k]; ok {
Expand Down

0 comments on commit 6a78c7f

Please sign in to comment.