-
Notifications
You must be signed in to change notification settings - Fork 2
/
tmdb_movie_search.go
58 lines (50 loc) · 1.46 KB
/
tmdb_movie_search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"errors"
"net/url"
"strconv"
"strings"
)
type TmdbMovieSearchResults struct {
Results []TmdbMovieSearchResult
}
type TmdbMovieSearchResult struct {
Id int
Title string
Overview string
BackdropPath string `json:"backdrop_path"`
PosterPath string `json:"poster_path"`
ReleaseDate string `json:"release_date"` // yyyy-mm-dd
Homepage string
VoteAverage float32 `json:"vote_average"`
}
// Finds the movie id.
// If year is given, it includes that in the criteria.
func requestTmdbMovieSearch(title string, year *int) (TmdbMovieSearchResult, error) {
if strings.HasPrefix(title, "!") { // Don't use TMDB.
newTitleA := strings.TrimSpace(title[1:])
newTitle := strings.Split(newTitleA, ".")[0] // Eg if it is '!Foo.deinterlace.audiostream2' this only returns the 'Foo'.
actualYear := 2000
if year != nil {
actualYear = *year
}
yearStr := strconv.Itoa(actualYear)
return TmdbMovieSearchResult{
Title: newTitle,
ReleaseDate: yearStr + "-01-01",
}, nil
}
url := tmdbApiRoot + "search/movie?api_key=" + tmdbApiKey + "&query=" + url.QueryEscape(title)
if year != nil {
url += "&year=" + strconv.Itoa(*year)
}
var results TmdbMovieSearchResults
if _, err := tmdbDownloadParse(url, &results); err != nil {
return TmdbMovieSearchResult{}, err
}
// Return the first result.
for _, result := range results.Results {
return result, nil
}
return TmdbMovieSearchResult{}, errors.New("No results")
}