golift.io/starr@v1.0.0/sonarr/calendar.go (about) 1 package sonarr 2 3 import ( 4 "context" 5 "fmt" 6 "net/url" 7 "path" 8 "time" 9 10 "golift.io/starr" 11 ) 12 13 // Define Base Path for Calendar queries. 14 const bpCalendar = APIver + "/calendar" 15 16 // Calendar defines the filters for fetching calendar items. 17 type Calendar struct { 18 Start time.Time 19 End time.Time 20 Unmonitored bool 21 IncludeSeries bool 22 IncludeEpisodeFile bool 23 IncludeEpisodeImages bool 24 } 25 26 // GetCalendar returns calendars based on filters. 27 func (s *Sonarr) GetCalendar(filter Calendar) ([]*Episode, error) { 28 return s.GetCalendarContext(context.Background(), filter) 29 } 30 31 // GetCalendarContext returns calendars based on filters. 32 func (s *Sonarr) GetCalendarContext(ctx context.Context, filter Calendar) ([]*Episode, error) { 33 var output []*Episode 34 35 req := starr.Request{URI: bpCalendar, Query: make(url.Values)} 36 req.Query.Add("unmonitored", fmt.Sprint(filter.Unmonitored)) 37 req.Query.Add("includeSeries", fmt.Sprint(filter.IncludeSeries)) 38 req.Query.Add("includeEpisodeFile", fmt.Sprint(filter.IncludeEpisodeFile)) 39 req.Query.Add("includeEpisodeImages", fmt.Sprint(filter.IncludeEpisodeImages)) 40 41 if !filter.Start.IsZero() { 42 req.Query.Add("start", filter.Start.UTC().Format(starr.CalendarTimeFilterFormat)) 43 } 44 45 if !filter.End.IsZero() { 46 req.Query.Add("end", filter.End.UTC().Format(starr.CalendarTimeFilterFormat)) 47 } 48 49 if err := s.GetInto(ctx, req, &output); err != nil { 50 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 51 } 52 53 return output, nil 54 } 55 56 // GetCalendarID returns a single calendar by ID. 57 func (s *Sonarr) GetCalendarID(calendarID int64) (*Episode, error) { 58 return s.GetCalendarIDContext(context.Background(), calendarID) 59 } 60 61 // GetCalendarIDContext returns a single calendar by ID. 62 func (s *Sonarr) GetCalendarIDContext(ctx context.Context, calendarID int64) (*Episode, error) { 63 var output *Episode 64 65 req := starr.Request{URI: path.Join(bpCalendar, fmt.Sprint(calendarID))} 66 if err := s.GetInto(ctx, req, &output); err != nil { 67 return nil, fmt.Errorf("api.Get(%s): %w", &req, err) 68 } 69 70 return output, nil 71 }