media: add support for legacy tvdb api (#16)
This commit is contained in:
58
media/tvdb/media.go
Normal file
58
media/tvdb/media.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package tvdb
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/l3uddz/nabarr/util"
|
||||
"github.com/lucperkins/rek"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrItemNotFound = errors.New("not found")
|
||||
)
|
||||
|
||||
func (c *Client) GetItem(tvdbId string) (*Item, error) {
|
||||
// empty item when appropriate
|
||||
if c.apiKey == "" || tvdbId == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// prepare request
|
||||
reqUrl := util.JoinURL(c.apiURL, "series", tvdbId)
|
||||
c.log.Trace().
|
||||
Str("url", reqUrl).
|
||||
Msg("Searching tvdb")
|
||||
|
||||
// send request
|
||||
c.rl.Take()
|
||||
resp, err := rek.Get(reqUrl, rek.Headers(c.apiHeaders), rek.Timeout(c.apiTimeout))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request lookup: %w", err)
|
||||
}
|
||||
defer resp.Body().Close()
|
||||
|
||||
// validate response
|
||||
if resp.StatusCode() != 200 {
|
||||
return nil, fmt.Errorf("validate lookup response: %s", resp.Status())
|
||||
}
|
||||
|
||||
// decode response
|
||||
b := new(lookupResponse)
|
||||
if err := json.NewDecoder(resp.Body()).Decode(b); err != nil {
|
||||
return nil, fmt.Errorf("decode lookup response: %w", err)
|
||||
}
|
||||
|
||||
if b.Data.SeriesName == "" {
|
||||
return nil, fmt.Errorf("item with imdbId: %v: %w", tvdbId, ErrItemNotFound)
|
||||
}
|
||||
|
||||
return &Item{
|
||||
Runtime: util.Atoi(b.Data.Runtime, 0),
|
||||
Language: b.Data.Language,
|
||||
Genre: b.Data.Genre,
|
||||
AirsDayOfWeek: b.Data.AirsDayOfWeek,
|
||||
SiteRating: b.Data.SiteRating,
|
||||
SiteRatingCount: b.Data.SiteRatingCount,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user