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

Add movie editor endpoints #69

Merged
merged 6 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ linters:
- dupl
- nlreturn

#linters-settings:
# govet:
# enable:
# - fieldalignment

run:
timeout: 35m
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module golift.io/starr

go 1.17

require golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // publicsuffix, cookiejar.
require golang.org/x/net v0.0.0-20221004154528-8021a29435af // publicsuffix, cookiejar.

// All of this is for the tests.
require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221004154528-8021a29435af h1:wv66FM3rLZGPdxpYL+ApnDe2HzHcTFta3z5nsc13wI4=
golang.org/x/net v0.0.0-20221004154528-8021a29435af/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
23 changes: 23 additions & 0 deletions pointers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package starr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These may be useful down the road too.


// String returns a pointer to a string.
func String(s string) *string {
return &s
}

// True returns a pointer to a true boolean.
func True() *bool {
s := true
return &s
}

// False returns a pointer to a false boolean.
func False() *bool {
s := false
return &s
}

// Int64 returns a pointer to the provided integer.
func Int64(s int64) *int64 {
return &s
}
34 changes: 17 additions & 17 deletions radarr/importlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ const bpImportList = APIver + "/importlist"

// ImportList represents the api/v3/importlist endpoint.
type ImportList struct {
ID int64 `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
EnableAuto bool `json:"enableAuto"`
ShouldMonitor bool `json:"shouldMonitor"`
SearchOnAdd bool `json:"searchOnAdd"`
RootFolderPath string `json:"rootFolderPath"`
QualityProfileID int64 `json:"qualityProfileId"`
MinimumAvailability string `json:"minimumAvailability"`
ListType string `json:"listType"`
ListOrder int64 `json:"listOrder"`
Fields []*Field `json:"fields"`
ImplementationName string `json:"implementationName"`
Implementation string `json:"implementation"`
ConfigContract string `json:"configContract"`
InfoLink string `json:"infoLink"`
Tags []int `json:"tags"`
ID int64 `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
EnableAuto bool `json:"enableAuto"`
ShouldMonitor bool `json:"shouldMonitor"`
SearchOnAdd bool `json:"searchOnAdd"`
RootFolderPath string `json:"rootFolderPath"`
QualityProfileID int64 `json:"qualityProfileId"`
MinimumAvailability Availability `json:"minimumAvailability"`
ListType string `json:"listType"`
ListOrder int64 `json:"listOrder"`
Fields []*Field `json:"fields"`
ImplementationName string `json:"implementationName"`
Implementation string `json:"implementation"`
ConfigContract string `json:"configContract"`
InfoLink string `json:"infoLink"`
Tags []int `json:"tags"`
}

// Field is currently only part of ImportList.
Expand Down
24 changes: 21 additions & 3 deletions radarr/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Movie struct {
ID int64 `json:"id"`
Title string `json:"title,omitempty"`
Path string `json:"path,omitempty"`
MinimumAvailability string `json:"minimumAvailability,omitempty"`
MinimumAvailability Availability `json:"minimumAvailability,omitempty"`
QualityProfileID int64 `json:"qualityProfileId,omitempty"`
TmdbID int64 `json:"tmdbId,omitempty"`
OriginalTitle string `json:"originalTitle,omitempty"`
Expand Down Expand Up @@ -101,7 +101,7 @@ type MediaInfo struct {
type AddMovieInput struct {
Title string `json:"title,omitempty"`
TitleSlug string `json:"titleSlug,omitempty"`
MinimumAvailability string `json:"minimumAvailability,omitempty"`
MinimumAvailability Availability `json:"minimumAvailability,omitempty"`
RootFolderPath string `json:"rootFolderPath"`
TmdbID int64 `json:"tmdbId"`
QualityProfileID int64 `json:"qualityProfileId"`
Expand Down Expand Up @@ -138,7 +138,7 @@ type AddMovieOutput struct {
Studio string `json:"studio"`
Path string `json:"path"`
QualityProfileID int64 `json:"qualityProfileId"`
MinimumAvailability string `json:"minimumAvailability"`
MinimumAvailability Availability `json:"minimumAvailability"`
FolderName string `json:"folderName"`
Runtime int `json:"runtime"`
CleanTitle string `json:"cleanTitle"`
Expand Down Expand Up @@ -280,3 +280,21 @@ func (r *Radarr) LookupContext(ctx context.Context, term string) ([]*Movie, erro

return output, nil
}

// DeleteMovie removes a movie from the database. Setting deleteFiles true will delete all content for the movie.
func (r *Radarr) DeleteMovie(movieID int64, deleteFiles, addImportExclusion bool) error {
return r.DeleteMovieContext(context.Background(), movieID, deleteFiles, addImportExclusion)
}

// DeleteMovieContext removes a movie from the database. Setting deleteFiles true will delete all content for the movie.
func (r *Radarr) DeleteMovieContext(ctx context.Context, movieID int64, deleteFiles, addImportExclusion bool) error {
req := starr.Request{URI: path.Join(bpMovie, fmt.Sprint(movieID)), Query: make(url.Values)}
req.Query.Set("deleteFiles", fmt.Sprint(deleteFiles))
req.Query.Set("addImportExclusion", fmt.Sprint(addImportExclusion))

if err := r.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}

return nil
}
88 changes: 88 additions & 0 deletions radarr/movieeditor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package radarr

import (
"bytes"
"context"
"encoding/json"
"fmt"

"golift.io/starr"
)

const bpMovieEditor = bpMovie + "/editor"

// BulkEdit is the input for the bulk movie editor endpoint.
// You may use starr.True(), starr.False(), starr.Int64(), and starr.String() to add data to the struct members.
// Use Availability.Ptr() to add a value to minimum availability, and starr.ApplyTags.Ptr() for apply tags.
type BulkEdit struct {
MovieIDs []int64 `json:"movieIds"`
Monitored *bool `json:"monitored,omitempty"`
QualityProfileID *int64 `json:"qualityProfileId,omitempty"`
MinimumAvailability *Availability `json:"minimumAvailability,omitempty"` // tba
RootFolderPath *string `json:"rootFolderPath,omitempty"` // path
Tags []int `json:"tags,omitempty"` // [0]
ApplyTags *starr.ApplyTags `json:"applyTags,omitempty"` // add
MoveFiles *bool `json:"moveFiles,omitempty"`
DeleteFiles *bool `json:"deleteFiles,omitempty"` // delete only
AddImportExclusion *bool `json:"addImportExclusion,omitempty"` // delete only
}

// Availability is an enum used as MinimumAvailability in a few places throughout Radarr.
type Availability string

// Availability / MinimumAvailability constants.
// https://radarr.video/docs/api/#/MovieEditor/put_api_v3_movie_editor
const (
AvailabilityToBeAnnounced Availability = "tba"
AvailabilityAnnounced Availability = "announced"
AvailabilityInCinemas Availability = "inCinemas"
AvailabilityReleased Availability = "released"
AvailabilityDeleted Availability = "deleted"
)
Comment on lines +30 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nice for a user perspective since it provides users with valid values. However, users can still use any string they want... so I'd say it doesn't make much difference for me, if you like this approach you can continue to use it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's all about users. This, if nothing else, gives them an easy way to see what values are appropriate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a difference in status and availability. tba and deleted will not work as a minimum availability in radarr. "announced", "released" & "inCinemas"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

Screen Shot 2022-10-07 at 11 18 15 AM

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Practical use would be the 3 nit mentioned

  • deleted is a status from tmdb
  • tba is legacy carryover

neither really has any practical use

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good. I just made them constants. Users don't have to use them. :)


// Ptr returns a pointer to a minimum availability. Useful for a BulkEdit struct.
func (a Availability) Ptr() *Availability {
return &a
}

// EditMovies allows bulk diting many movies at once.
func (r *Radarr) EditMovies(editMovies *BulkEdit) ([]*Movie, error) {
return r.EditMoviesContext(context.Background(), editMovies)
}

// EditMoviesContext allows bulk diting many movies at once.
func (r *Radarr) EditMoviesContext(ctx context.Context, editMovies *BulkEdit) ([]*Movie, error) {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(editMovies); err != nil {
return nil, fmt.Errorf("json.Marshal(%s): %w", bpMovieEditor, err)
}

var output []*Movie

req := starr.Request{URI: bpMovieEditor, Body: &body}
if err := r.PutInto(ctx, req, &output); err != nil {
return nil, fmt.Errorf("api.Put(%s): %w", &req, err)
}

return output, nil
}

// DeleteMovies bulk deletes movies. Can also mark them as excluded, and delete their files.
func (r *Radarr) DeleteMovies(deleteMovies *BulkEdit) error {
return r.DeleteMoviesContext(context.Background(), deleteMovies)
}

// DeleteDeleteMoviesContextMovies bulk deletes movies. Can also mark them as excluded, and delete their files.
func (r *Radarr) DeleteMoviesContext(ctx context.Context, deleteMovies *BulkEdit) error {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(deleteMovies); err != nil {
return fmt.Errorf("json.Marshal(%s): %w", bpMovieEditor, err)
}

req := starr.Request{URI: bpMovieEditor, Body: &body}
if err := r.DeleteAny(ctx, req); err != nil {
return fmt.Errorf("api.Delete(%s): %w", &req, err)
}

return nil
}
65 changes: 65 additions & 0 deletions radarr/movieeditor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package radarr_test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidnewhall this really rock! thanks 🚀


import (
"net/http"
"path"
"testing"

"github.com/stretchr/testify/assert"
"golift.io/starr"
"golift.io/starr/radarr"
)

func TestEditMovies(t *testing.T) {
t.Parallel()

tests := []*starr.TestMockData{
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "movie", "editor"),
ResponseStatus: http.StatusOK,
ResponseBody: `[{"id": 7, "monitored": true},{"id": 3, "monitored": true}]`,
WithError: nil,
WithRequest: &radarr.BulkEdit{
MovieIDs: []int64{7, 3},
Monitored: starr.True(),
DeleteFiles: starr.False(),
},
ExpectedRequest: `{"movieIds":[7,3],"monitored":true,"deleteFiles":false}` + "\n",
ExpectedMethod: http.MethodPut,
WithResponse: []*radarr.Movie{{ID: 7, Monitored: true}, {ID: 3, Monitored: true}},
},
{
Name: "200",
ExpectedPath: path.Join("/", starr.API, radarr.APIver, "movie", "editor"),
ResponseStatus: http.StatusOK,
ResponseBody: `[{"id":17,"minimumAvailability":"tba","tags":[44,55,66]},` +
`{"id":13,"minimumAvailability":"tba","tags":[44,55,66]}]`,
WithError: nil,
WithRequest: &radarr.BulkEdit{
MovieIDs: []int64{17, 13},
Tags: []int{44, 55, 66},
ApplyTags: starr.TagsAdd.Ptr(),
MinimumAvailability: radarr.AvailabilityToBeAnnounced.Ptr(),
},
ExpectedRequest: `{"movieIds":[17,13],"minimumAvailability":"tba","tags":[44,55,66],"applyTags":"add"}` + "\n",
ExpectedMethod: http.MethodPut,
WithResponse: []*radarr.Movie{
{ID: 17, MinimumAvailability: radarr.AvailabilityToBeAnnounced, Tags: []int{44, 55, 66}},
{ID: 13, MinimumAvailability: radarr.AvailabilityToBeAnnounced, Tags: []int{44, 55, 66}},
},
},
}

for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
t.Parallel()
mockServer := test.GetMockServer(t)
client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
output, err := client.EditMovies(test.WithRequest.(*radarr.BulkEdit))
assert.ErrorIs(t, err, test.WithError, "the wrong error was returned")
assert.EqualValues(t, test.WithResponse, output, "make sure ResponseBody and WithResponse are a match")
})
}
}
16 changes: 16 additions & 0 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,19 @@ func (d *PlayTime) MarshalJSON() ([]byte, error) {
}

var _ json.Unmarshaler = (*PlayTime)(nil)

// ApplyTags is an enum used as an input for Bulk editors, and perhaps other places.
type ApplyTags string

// ApplyTags enum constants. Use these as inputs for "ApplyTags" member values.
// Schema doc'd here: https://radarr.video/docs/api/#/MovieEditor/put_api_v3_movie_editor
const (
TagsAdd ApplyTags = "add"
TagsRemove ApplyTags = "remove"
TagsReplace ApplyTags = "replace"
)

// Ptr returns a pointer to an apply tags value. Useful for a BulkEdit struct.
func (a ApplyTags) Ptr() *ApplyTags {
return &a
}
2 changes: 1 addition & 1 deletion test_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (test *TestMockData) GetMockServer(t *testing.T) *httptest.Server {
assert.EqualValues(t, test.ExpectedPath, req.URL.String())
writer.WriteHeader(test.ResponseStatus)

assert.EqualValues(t, req.Method, test.ExpectedMethod)
assert.EqualValues(t, test.ExpectedMethod, req.Method)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was backward.


body, err := io.ReadAll(req.Body)
assert.NoError(t, err)
Expand Down