golift.io/starr@v1.0.0/sonarr/episode.go (about)

     1  package sonarr
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"fmt"
     8  	"net/url"
     9  	"path"
    10  	"time"
    11  
    12  	"golift.io/starr"
    13  )
    14  
    15  const bpEpisode = APIver + "/episode"
    16  
    17  // Episode is the /api/v3/episode endpoint.
    18  type Episode struct {
    19  	ID                       int64          `json:"id"`
    20  	SeriesID                 int64          `json:"seriesId"`
    21  	TvdbID                   int64          `json:"tvdbId"`
    22  	AbsoluteEpisodeNumber    int64          `json:"absoluteEpisodeNumber"`
    23  	EpisodeFileID            int64          `json:"episodeFileId"`
    24  	SeasonNumber             int64          `json:"seasonNumber"`
    25  	EpisodeNumber            int64          `json:"episodeNumber"`
    26  	AirDateUtc               time.Time      `json:"airDateUtc"`
    27  	AirDate                  string         `json:"airDate"`
    28  	Title                    string         `json:"title"`
    29  	Overview                 string         `json:"overview"`
    30  	UnverifiedSceneNumbering bool           `json:"unverifiedSceneNumbering"`
    31  	HasFile                  bool           `json:"hasFile"`
    32  	Monitored                bool           `json:"monitored"`
    33  	Images                   []*starr.Image `json:"images"`
    34  	Series                   *Series        `json:"series"`
    35  }
    36  
    37  // GetSeriesEpisodes returns all episodes for a series by series ID.
    38  // You can get series IDs from GetAllSeries() and GetSeries().
    39  func (s *Sonarr) GetSeriesEpisodes(seriesID int64) ([]*Episode, error) {
    40  	return s.GetSeriesEpisodesContext(context.Background(), seriesID)
    41  }
    42  
    43  // GetSeriesEpisodesContext returns all episodes for a series by series ID.
    44  // You can get series IDs from GetAllSeries() and GetSeries().
    45  func (s *Sonarr) GetSeriesEpisodesContext(ctx context.Context, seriesID int64) ([]*Episode, error) {
    46  	var output []*Episode
    47  
    48  	req := starr.Request{URI: bpEpisode, Query: make(url.Values)}
    49  	req.Query.Add("seriesId", fmt.Sprint(seriesID))
    50  
    51  	if err := s.GetInto(ctx, req, &output); err != nil {
    52  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    53  	}
    54  
    55  	return output, nil
    56  }
    57  
    58  // GetEpisodeByID locates and returns an episode by DB [episode] ID.
    59  func (s *Sonarr) GetEpisodeByID(episodeID int64) (*Episode, error) {
    60  	return s.GetEpisodeByIDContext(context.Background(), episodeID)
    61  }
    62  
    63  // GetEpisodeByIDContext locates and returns an episode by DB [episode] ID.
    64  func (s *Sonarr) GetEpisodeByIDContext(ctx context.Context, episodeID int64) (*Episode, error) {
    65  	var output Episode
    66  
    67  	req := starr.Request{URI: path.Join(bpEpisode, fmt.Sprint(episodeID))}
    68  	if err := s.GetInto(ctx, req, &output); err != nil {
    69  		return nil, fmt.Errorf("api.Get(%s): %w", &req, err)
    70  	}
    71  
    72  	return &output, nil
    73  }
    74  
    75  // MonitorEpisode sends a request to monitor (true) or unmonitor (false) a list of episodes by ID.
    76  // You can get episode IDs from GetSeriesEpisodes().
    77  func (s *Sonarr) MonitorEpisode(episodeIDs []int64, monitor bool) ([]*Episode, error) {
    78  	return s.MonitorEpisodeContext(context.Background(), episodeIDs, monitor)
    79  }
    80  
    81  // MonitorEpisodeContext sends a request to monitor (true) or unmonitor (false) a list of episodes by ID.
    82  // You can get episode IDs from GetSeriesEpisodes().
    83  func (s *Sonarr) MonitorEpisodeContext(ctx context.Context, episodeIDs []int64, monitor bool) ([]*Episode, error) {
    84  	var body bytes.Buffer
    85  	if err := json.NewEncoder(&body).Encode(&struct {
    86  		E []int64 `json:"episodeIds"`
    87  		M bool    `json:"monitored"`
    88  	}{E: episodeIDs, M: monitor}); err != nil {
    89  		return nil, fmt.Errorf("json.Marshal(%s): %w", bpEpisode, err)
    90  	}
    91  
    92  	var output []*Episode
    93  
    94  	req := starr.Request{URI: path.Join(bpEpisode, "monitor"), Body: &body}
    95  	if err := s.PutInto(ctx, req, &output); err != nil {
    96  		return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
    97  	}
    98  
    99  	return output, nil
   100  }