initial commit
This commit is contained in:
104
imdb.go
Normal file
104
imdb.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package imdb
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
)
|
||||
|
||||
const (
|
||||
searchURL = "https://v3.sg.media-imdb.com/suggestion/x/%s.json"
|
||||
)
|
||||
|
||||
type Answer struct {
|
||||
Data []Data `json:"d"`
|
||||
Query string `json:"q"`
|
||||
V int `json:"v"`
|
||||
}
|
||||
|
||||
// type Image struct {
|
||||
// Height int `json:"height"`
|
||||
// ImageURL string `json:"imageUrl"`
|
||||
// Width int `json:"width"`
|
||||
// }
|
||||
|
||||
type (
|
||||
QID string
|
||||
Q string
|
||||
)
|
||||
|
||||
const (
|
||||
QID_Movie QID = "movie"
|
||||
QID_TVMovie QID = "tvMovie"
|
||||
QID_MusicVideo QID = "musicVideo"
|
||||
QID_TVSeries QID = "tvSeries"
|
||||
QID_TVMiniSeries QID = "tvMiniSeries"
|
||||
)
|
||||
|
||||
const (
|
||||
Q_Feature Q = "feature"
|
||||
Q_TVMovie Q = "TV movie"
|
||||
Q_MusicVideo Q = "musicVideo"
|
||||
Q_TVSeries Q = "TV series"
|
||||
Q_TVMiniSeries Q = "TV mini-series"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
// Image Image `json:"i,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"l"`
|
||||
Stars string `json:"s"`
|
||||
Q Q `json:"q,omitempty"`
|
||||
QID QID `json:"qid,omitempty"`
|
||||
Rank int `json:"rank,omitempty"`
|
||||
Year int `json:"y,omitempty"`
|
||||
}
|
||||
|
||||
func SearchMovie(title string) ([]Data, error) {
|
||||
data, err := Search(title)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data = slices.DeleteFunc(data, func(d Data) bool {
|
||||
return d.QID != QID_Movie
|
||||
})
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func SearchTVSeries(title string) ([]Data, error) {
|
||||
data, err := Search(title)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data = slices.DeleteFunc(data, func(d Data) bool {
|
||||
return d.Q != Q_TVSeries
|
||||
})
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func Search(title string) ([]Data, error) {
|
||||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(searchURL, title), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
var ans Answer
|
||||
|
||||
if err := json.NewDecoder(res.Body).Decode(&ans); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ans.Data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user