TVmaze: drop trailing slash from base URL (was producing //shows/), send
season/number as plain ints (TVmaze rejects zero-padding), check HTTP
status before decoding so 404 bodies stop being decoded as episodes, and
fetch /shows/{id} separately for show name + IMDb mapping —
episodebynumber does not honor embed=show despite what the docs imply.
OMDb: check HTTP status, and include a body snippet when Response is
non-True with an empty Error field so rate-limit and HTML failure modes
are diagnosable.
Both: assert that the fields we actually depend on come back non-empty
after decode. Catches silent field rename/removal without making us
fragile to TVmaze adding new optional fields.
main.processFile: split the nometadata fallback by branch. Series uses
Collection (legitimate shows can have no IMDb mapping); movies keep the
IMDBID/Title check.
213 lines
5.1 KiB
Go
213 lines
5.1 KiB
Go
package metadata
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"videnc-vibe/pkg/types"
|
|
)
|
|
|
|
const (
|
|
omdbAPIURL = "https://www.omdbapi.com/"
|
|
tvmazeAPIURL = "https://api.tvmaze.com"
|
|
)
|
|
|
|
var (
|
|
imdbRegex = regexp.MustCompile(`tt(\d+)`)
|
|
tvmRegex = regexp.MustCompile(`(?i)tvm(\d+)`)
|
|
seRegex = regexp.MustCompile(`(?i)s(\d+)e(\d+)`)
|
|
)
|
|
|
|
type OMDbResponse struct {
|
|
Title string `json:"Title"`
|
|
Year string `json:"Released"`
|
|
IMDBID string `json:"imdbID"`
|
|
Response string `json:"Response"`
|
|
Error string `json:"Error"`
|
|
}
|
|
|
|
type TVMazeShowResponse struct {
|
|
Name string `json:"name"`
|
|
Extern struct {
|
|
IMDB string `json:"imdb"`
|
|
} `json:"externals"`
|
|
}
|
|
|
|
type TVMazeEpisode struct {
|
|
Name string `json:"name"`
|
|
Season int `json:"season"`
|
|
Number int `json:"number"`
|
|
Airdate string `json:"airdate"`
|
|
}
|
|
|
|
type Client struct {
|
|
omdbAPIKey string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewClient(apiKey string) *Client {
|
|
return &Client{
|
|
omdbAPIKey: apiKey,
|
|
httpClient: &http.Client{Timeout: 10 * time.Second},
|
|
}
|
|
}
|
|
|
|
func ParseFilename(filename string) (isSeries bool, imdbID, tvmazeID, season, episode string) {
|
|
filename = strings.TrimSuffix(filename, ".mkv")
|
|
|
|
seMatch := seRegex.FindStringSubmatch(filename)
|
|
tvmMatch := tvmRegex.FindStringSubmatch(filename)
|
|
imdbMatch := imdbRegex.FindStringSubmatch(filename)
|
|
|
|
if seMatch != nil && tvmMatch != nil {
|
|
isSeries = true
|
|
season = fmt.Sprintf("%02d", parseInt(seMatch[1]))
|
|
episode = fmt.Sprintf("%02d", parseInt(seMatch[2]))
|
|
tvmazeID = tvmMatch[1]
|
|
return
|
|
}
|
|
|
|
if imdbMatch != nil {
|
|
imdbID = imdbMatch[0]
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func parseInt(s string) int {
|
|
var n int
|
|
fmt.Sscanf(s, "%d", &n)
|
|
return n
|
|
}
|
|
|
|
func (c *Client) FetchMovieMetadata(imdbID string) (*types.Metadata, error) {
|
|
url := fmt.Sprintf("%s?i=%s&apikey=%s", omdbAPIURL, imdbID, c.omdbAPIKey)
|
|
|
|
resp, err := c.httpClient.Get(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("fetching OMDb: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("reading OMDb body: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("OMDb HTTP %d: %s", resp.StatusCode, snippet(body))
|
|
}
|
|
|
|
var result OMDbResponse
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
return nil, fmt.Errorf("decoding OMDb response: %w (body: %s)", err, snippet(body))
|
|
}
|
|
|
|
if result.Response != "True" {
|
|
if result.Error != "" {
|
|
return nil, fmt.Errorf("OMDb error: %s", result.Error)
|
|
}
|
|
return nil, fmt.Errorf("OMDb non-True response: %s", snippet(body))
|
|
}
|
|
|
|
if result.Title == "" || result.IMDBID == "" {
|
|
return nil, fmt.Errorf("OMDb response missing Title/imdbID (schema drift?): %s", snippet(body))
|
|
}
|
|
|
|
dateReleased := formatDate(result.Year)
|
|
|
|
return &types.Metadata{
|
|
Title: result.Title,
|
|
DateReleased: dateReleased,
|
|
IMDBID: result.IMDBID,
|
|
OriginalMedia: types.MediaTypeBluRay,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) FetchSeriesMetadata(tvmazeID, season, episode string) (*types.Metadata, error) {
|
|
seasonNum, err := strconv.Atoi(season)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parsing season %q: %w", season, err)
|
|
}
|
|
episodeNum, err := strconv.Atoi(episode)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parsing episode %q: %w", episode, err)
|
|
}
|
|
|
|
epURL := fmt.Sprintf("%s/shows/%s/episodebynumber?season=%d&number=%d", tvmazeAPIURL, tvmazeID, seasonNum, episodeNum)
|
|
var ep TVMazeEpisode
|
|
if err := c.fetchTVMazeJSON(epURL, &ep); err != nil {
|
|
return nil, fmt.Errorf("TVmaze episode %s S%dE%d: %w", tvmazeID, seasonNum, episodeNum, err)
|
|
}
|
|
|
|
showURL := fmt.Sprintf("%s/shows/%s", tvmazeAPIURL, tvmazeID)
|
|
var show TVMazeShowResponse
|
|
if err := c.fetchTVMazeJSON(showURL, &show); err != nil {
|
|
return nil, fmt.Errorf("TVmaze show %s: %w", tvmazeID, err)
|
|
}
|
|
|
|
if ep.Name == "" || show.Name == "" {
|
|
return nil, fmt.Errorf("TVmaze response missing episode/show name (schema drift?) for %s S%dE%d", tvmazeID, seasonNum, episodeNum)
|
|
}
|
|
|
|
return &types.Metadata{
|
|
Title: ep.Name,
|
|
Collection: show.Name,
|
|
Season: season,
|
|
Episode: episode,
|
|
DateReleased: formatDate(ep.Airdate),
|
|
IMDBID: show.Extern.IMDB,
|
|
TVMazeID: tvmazeID,
|
|
IsSeries: true,
|
|
OriginalMedia: types.MediaTypeBluRay,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) fetchTVMazeJSON(url string, v interface{}) error {
|
|
resp, err := c.httpClient.Get(url)
|
|
if err != nil {
|
|
return fmt.Errorf("GET: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 256*1024))
|
|
if err != nil {
|
|
return fmt.Errorf("read body: %w", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("HTTP %d: %s", resp.StatusCode, snippet(body))
|
|
}
|
|
|
|
if err := json.Unmarshal(body, v); err != nil {
|
|
return fmt.Errorf("decode: %w (body: %s)", err, snippet(body))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func snippet(body []byte) string {
|
|
s := strings.TrimSpace(string(body))
|
|
const max = 200
|
|
if len(s) > max {
|
|
s = s[:max] + "…"
|
|
}
|
|
return s
|
|
}
|
|
|
|
func formatDate(date string) string {
|
|
if date == "" || date == "N/A" {
|
|
return ""
|
|
}
|
|
t, err := time.Parse("02 Jan 2006", date)
|
|
if err != nil {
|
|
return date
|
|
}
|
|
return t.Format("2006-01-02")
|
|
}
|