Compare commits
No commits in common. "5f94d7703235f122318a39a7f2972560a25344e1" and "1cebe777a7e66f7eb1ce0334efafc89e61a65982" have entirely different histories.
5f94d77032
...
1cebe777a7
3 changed files with 33 additions and 114 deletions
33
README.md
33
README.md
|
@ -1,8 +1,5 @@
|
||||||
The Golang Omdb API
|
The Golang Omdb API
|
||||||
=======
|
=======
|
||||||
[![Build Status](https://travis-ci.org/eefret/gomdb.svg?branch=master)](https://travis-ci.org/eefret/gomdb)
|
|
||||||
[![GoDoc](https://godoc.org/github.com/eefret/go-imdb?status.svg)](https://godoc.org/github.com/eefret/go-imdb)
|
|
||||||
|
|
||||||
|
|
||||||
Author: Christopher T. Herrera (eefretsoul AT gmail DOT com)
|
Author: Christopher T. Herrera (eefretsoul AT gmail DOT com)
|
||||||
|
|
||||||
|
@ -31,10 +28,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
api := gomdb.Init(YOUR_API_KEY)
|
|
||||||
|
|
||||||
query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch}
|
query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch}
|
||||||
res, err := api.Search(query)
|
res, err := gomdb.Search(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -42,38 +37,26 @@ func main() {
|
||||||
fmt.Println(res.Search)
|
fmt.Println(res.Search)
|
||||||
|
|
||||||
query = &gomdb.QueryData{Title: "Macbeth", Year: "2015"}
|
query = &gomdb.QueryData{Title: "Macbeth", Year: "2015"}
|
||||||
res2, err := api.MovieByTitle(query)
|
res2, err := gomdb.MovieByTitle(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(res2)
|
fmt.Println(res2)
|
||||||
|
|
||||||
query = &gomdb.QueryData{Title: "Rick and Morty", Season: "1", Episode: "8", SearchType: gomdb.EpisodeSearch}
|
res3, err := gomdb.MovieByImdbID("tt2884018")
|
||||||
res3, err := api.MovieByTitle(query)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(res3)
|
fmt.Println(res3)
|
||||||
}
|
}
|
||||||
|
|
||||||
query = &gomdb.QueryData{ImdbId: "tt2884018"}
|
|
||||||
res4, err := api.MovieByImdbID(query)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(res4)
|
|
||||||
|
|
||||||
query = &gomdb.QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: gomdb.EpisodeSearch}
|
|
||||||
res5, err := api.MovieByImdbID(query)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(res5)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
See the project documentation to see the Response Objects and stuff
|
See the project documentation to see the Response Objects and stuff
|
||||||
|
|
||||||
|
Project Documentation
|
||||||
|
---------------------
|
||||||
|
The automatically generated documentation can be found in godocs.
|
||||||
|
[![GoDoc](https://godoc.org/github.com/eefret/go-imdb?status.svg)](https://godoc.org/github.com/eefret/go-imdb)
|
||||||
|
|
35
gomdb.go
35
gomdb.go
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
baseURL = "http://www.omdbapi.com"
|
baseURL = "http://www.omdbapi.com/?"
|
||||||
plot = "full"
|
plot = "full"
|
||||||
tomatoes = "true"
|
tomatoes = "true"
|
||||||
|
|
||||||
|
@ -19,22 +19,12 @@ const (
|
||||||
EpisodeSearch = "episode"
|
EpisodeSearch = "episode"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OmdbApi struct {
|
|
||||||
apiKey string
|
|
||||||
}
|
|
||||||
|
|
||||||
func Init(apiKey string) *OmdbApi {
|
|
||||||
return &OmdbApi{apiKey: apiKey}
|
|
||||||
}
|
|
||||||
|
|
||||||
// QueryData is the type to create the search query
|
// QueryData is the type to create the search query
|
||||||
type QueryData struct {
|
type QueryData struct {
|
||||||
Title string
|
Title string
|
||||||
Year string
|
Year string
|
||||||
ImdbId string
|
ImdbId string
|
||||||
SearchType string
|
SearchType string
|
||||||
Season string
|
|
||||||
Episode string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//SearchResult is the type for the search results
|
//SearchResult is the type for the search results
|
||||||
|
@ -94,8 +84,8 @@ type MovieResult struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Search for movies given a Title and year, Year is optional you can pass nil
|
//Search for movies given a Title and year, Year is optional you can pass nil
|
||||||
func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
|
func Search(query *QueryData) (*SearchResponse, error) {
|
||||||
resp, err := api.requestAPI("search", query.Title, query.Year, query.SearchType)
|
resp, err := requestAPI("search", query.Title, query.Year, query.SearchType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -115,9 +105,8 @@ func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//MovieByTitle returns a MovieResult given Title
|
//MovieByTitle returns a MovieResult given Title
|
||||||
func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
|
func MovieByTitle(query *QueryData) (*MovieResult, error) {
|
||||||
resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season,
|
resp, err := requestAPI("title", query.Title, query.Year, query.SearchType)
|
||||||
query.Episode)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -136,9 +125,8 @@ func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
|
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
|
||||||
func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
|
func MovieByImdbID(id string) (*MovieResult, error) {
|
||||||
resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season,
|
resp, err := requestAPI("id", id)
|
||||||
query.Episode)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -160,7 +148,7 @@ func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
|
||||||
// param: apiCategory refers to which API we are calling. Can be "search", "title" or "id"
|
// param: apiCategory refers to which API we are calling. Can be "search", "title" or "id"
|
||||||
// Depending on that value, we will search by "t" or "s" or "i"
|
// Depending on that value, we will search by "t" or "s" or "i"
|
||||||
// param: params are the variadic list of params passed for that category
|
// param: params are the variadic list of params passed for that category
|
||||||
func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
|
func requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
|
||||||
var URL *url.URL
|
var URL *url.URL
|
||||||
URL, err = url.Parse(baseURL)
|
URL, err = url.Parse(baseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -177,8 +165,6 @@ func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http
|
||||||
}
|
}
|
||||||
URL.Path += "/"
|
URL.Path += "/"
|
||||||
parameters := url.Values{}
|
parameters := url.Values{}
|
||||||
parameters.Add("apikey", api.apiKey)
|
|
||||||
|
|
||||||
switch apiCategory {
|
switch apiCategory {
|
||||||
case "search":
|
case "search":
|
||||||
parameters.Add("s", params[0])
|
parameters.Add("s", params[0])
|
||||||
|
@ -188,15 +174,10 @@ func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http
|
||||||
parameters.Add("t", params[0])
|
parameters.Add("t", params[0])
|
||||||
parameters.Add("y", params[1])
|
parameters.Add("y", params[1])
|
||||||
parameters.Add("type", params[2])
|
parameters.Add("type", params[2])
|
||||||
parameters.Add("Season", params[3])
|
|
||||||
parameters.Add("Episode", params[4])
|
|
||||||
parameters.Add("plot", plot)
|
parameters.Add("plot", plot)
|
||||||
parameters.Add("tomatoes", tomatoes)
|
parameters.Add("tomatoes", tomatoes)
|
||||||
case "id":
|
case "id":
|
||||||
parameters.Add("i", params[0])
|
parameters.Add("i", params[0])
|
||||||
parameters.Add("type", params[2])
|
|
||||||
parameters.Add("Season", params[3])
|
|
||||||
parameters.Add("Episode", params[4])
|
|
||||||
parameters.Add("plot", plot)
|
parameters.Add("plot", plot)
|
||||||
parameters.Add("tomatoes", tomatoes)
|
parameters.Add("tomatoes", tomatoes)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,6 @@
|
||||||
package gomdb
|
package gomdb
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
var apiKey = os.Getenv("OMDB_API_KEY")
|
|
||||||
|
|
||||||
func TestNoKey(t *testing.T) {
|
|
||||||
api := Init("")
|
|
||||||
|
|
||||||
_, err := api.Search(&QueryData{Title: "Her"})
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("Expected to fail")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
expectedErrorMsg := "Status Code 401 received from IMDB"
|
|
||||||
if err.Error() != expectedErrorMsg {
|
|
||||||
t.Errorf("Expected- %s, Got- %s", expectedErrorMsg, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearch(t *testing.T) {
|
func TestSearch(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
@ -42,10 +22,8 @@ func TestSearch(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
api := Init(apiKey)
|
|
||||||
|
|
||||||
for i, item := range tests {
|
for i, item := range tests {
|
||||||
resp, err := api.Search(item.query)
|
resp, err := Search(item.query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Test[%d]: %s", i, err)
|
t.Errorf("Test[%d]: %s", i, err)
|
||||||
continue
|
continue
|
||||||
|
@ -69,17 +47,15 @@ func TestFailSearch(t *testing.T) {
|
||||||
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
|
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
|
||||||
}
|
}
|
||||||
|
|
||||||
api := Init(apiKey)
|
|
||||||
|
|
||||||
for i, item := range tests {
|
for i, item := range tests {
|
||||||
_, err := api.Search(item.query)
|
_, err := Search(item.query)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Test[%d]: Got nil error", i)
|
t.Errorf("Test[%d]: Got nil error", i)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Checking for strings is bad. But the API might change.
|
// Checking for strings is bad. But the API might change.
|
||||||
if err.Error() != "Movie not found!" {
|
if err.Error() != "Movie not found!" {
|
||||||
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
|
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,23 +69,21 @@ func TestInvalidCategory(t *testing.T) {
|
||||||
{&QueryData{Title: "Dexter", SearchType: "bad"}},
|
{&QueryData{Title: "Dexter", SearchType: "bad"}},
|
||||||
}
|
}
|
||||||
|
|
||||||
api := Init(apiKey)
|
|
||||||
|
|
||||||
for i, item := range tests {
|
for i, item := range tests {
|
||||||
_, err := api.Search(item.query)
|
_, err := Search(item.query)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Test[%d]: Got nil error", i)
|
t.Errorf("Test[%d]: Got nil error", i)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Checking for strings is bad. But the error type is formatted
|
// Checking for strings is bad. But the error type is formatted
|
||||||
if err.Error() != "Invalid search category- bad" {
|
if err.Error() != "Invalid search category- bad" {
|
||||||
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
|
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMediaByTitle(t *testing.T) {
|
func TestMovieByTitle(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
query *QueryData
|
query *QueryData
|
||||||
title string
|
title string
|
||||||
|
@ -127,22 +101,10 @@ func TestMediaByTitle(t *testing.T) {
|
||||||
"Macbeth",
|
"Macbeth",
|
||||||
"2015",
|
"2015",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
&QueryData{Title: "Rick and Morty", Season: "1", SearchType: SeriesSearch},
|
|
||||||
"Rick and Morty",
|
|
||||||
"2013–",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
&QueryData{Title: "Rick and Morty", Season: "1", Episode: "8", SearchType: EpisodeSearch},
|
|
||||||
"Rixty Minutes",
|
|
||||||
"2014",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
api := Init(apiKey)
|
|
||||||
|
|
||||||
for i, item := range tests {
|
for i, item := range tests {
|
||||||
resp, err := api.MovieByTitle(item.query)
|
resp, err := MovieByTitle(item.query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Test[%d]: %s", i, err)
|
t.Errorf("Test[%d]: %s", i, err)
|
||||||
continue
|
continue
|
||||||
|
@ -158,38 +120,31 @@ func TestMediaByTitle(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMediaByImdbID(t *testing.T) {
|
func TestMovieByImdbID(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
query *QueryData
|
id string
|
||||||
title string
|
title string
|
||||||
year string
|
year string
|
||||||
}{
|
}{
|
||||||
{&QueryData{ImdbId: "tt0137523", SearchType: MovieSearch},
|
{
|
||||||
|
"tt0137523",
|
||||||
"Fight Club",
|
"Fight Club",
|
||||||
"1999",
|
"1999",
|
||||||
},
|
},
|
||||||
{&QueryData{ImdbId: "tt1798709", SearchType: MovieSearch},
|
{
|
||||||
|
"tt1798709",
|
||||||
"Her",
|
"Her",
|
||||||
"2013",
|
"2013",
|
||||||
},
|
},
|
||||||
{&QueryData{ImdbId: "tt2884018", SearchType: MovieSearch},
|
{
|
||||||
|
"tt2884018",
|
||||||
"Macbeth",
|
"Macbeth",
|
||||||
"2015",
|
"2015",
|
||||||
},
|
},
|
||||||
{&QueryData{ImdbId: "tt3952222", Season: "1", SearchType: SeriesSearch},
|
|
||||||
"Killjoys",
|
|
||||||
"2015–",
|
|
||||||
},
|
|
||||||
{&QueryData{ImdbId: "tt0944947", Season: "1", Episode: "1", SearchType: EpisodeSearch},
|
|
||||||
"Winter Is Coming",
|
|
||||||
"2011",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
api := Init(apiKey)
|
|
||||||
|
|
||||||
for i, item := range tests {
|
for i, item := range tests {
|
||||||
resp, err := api.MovieByImdbID(item.query)
|
resp, err := MovieByImdbID(item.id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Test[%d]: %s", i, err)
|
t.Errorf("Test[%d]: %s", i, err)
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in a new issue