Compare commits
5 commits
1cebe777a7
...
5f94d77032
Author | SHA1 | Date | |
---|---|---|---|
|
5f94d77032 | ||
|
4319c85503 | ||
|
8a1e0abd44 | ||
|
fffb9ce2cf | ||
|
561e54fa87 |
3 changed files with 114 additions and 33 deletions
33
README.md
33
README.md
|
@ -1,5 +1,8 @@
|
|||
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)
|
||||
|
||||
|
@ -28,8 +31,10 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
api := gomdb.Init(YOUR_API_KEY)
|
||||
|
||||
query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch}
|
||||
res, err := gomdb.Search(query)
|
||||
res, err := api.Search(query)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
@ -37,26 +42,38 @@ func main() {
|
|||
fmt.Println(res.Search)
|
||||
|
||||
query = &gomdb.QueryData{Title: "Macbeth", Year: "2015"}
|
||||
res2, err := gomdb.MovieByTitle(query)
|
||||
res2, err := api.MovieByTitle(query)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(res2)
|
||||
|
||||
res3, err := gomdb.MovieByImdbID("tt2884018")
|
||||
query = &gomdb.QueryData{Title: "Rick and Morty", Season: "1", Episode: "8", SearchType: gomdb.EpisodeSearch}
|
||||
res3, err := api.MovieByTitle(query)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
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
|
||||
|
||||
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 (
|
||||
baseURL = "http://www.omdbapi.com/?"
|
||||
baseURL = "http://www.omdbapi.com"
|
||||
plot = "full"
|
||||
tomatoes = "true"
|
||||
|
||||
|
@ -19,12 +19,22 @@ const (
|
|||
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
|
||||
type QueryData struct {
|
||||
Title string
|
||||
Year string
|
||||
ImdbId string
|
||||
SearchType string
|
||||
Season string
|
||||
Episode string
|
||||
}
|
||||
|
||||
//SearchResult is the type for the search results
|
||||
|
@ -84,8 +94,8 @@ type MovieResult struct {
|
|||
}
|
||||
|
||||
//Search for movies given a Title and year, Year is optional you can pass nil
|
||||
func Search(query *QueryData) (*SearchResponse, error) {
|
||||
resp, err := requestAPI("search", query.Title, query.Year, query.SearchType)
|
||||
func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
|
||||
resp, err := api.requestAPI("search", query.Title, query.Year, query.SearchType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -105,8 +115,9 @@ func Search(query *QueryData) (*SearchResponse, error) {
|
|||
}
|
||||
|
||||
//MovieByTitle returns a MovieResult given Title
|
||||
func MovieByTitle(query *QueryData) (*MovieResult, error) {
|
||||
resp, err := requestAPI("title", query.Title, query.Year, query.SearchType)
|
||||
func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
|
||||
resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season,
|
||||
query.Episode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -125,8 +136,9 @@ func MovieByTitle(query *QueryData) (*MovieResult, error) {
|
|||
}
|
||||
|
||||
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
|
||||
func MovieByImdbID(id string) (*MovieResult, error) {
|
||||
resp, err := requestAPI("id", id)
|
||||
func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
|
||||
resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season,
|
||||
query.Episode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -148,7 +160,7 @@ func MovieByImdbID(id string) (*MovieResult, error) {
|
|||
// 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"
|
||||
// param: params are the variadic list of params passed for that category
|
||||
func requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
|
||||
func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
|
||||
var URL *url.URL
|
||||
URL, err = url.Parse(baseURL)
|
||||
if err != nil {
|
||||
|
@ -165,6 +177,8 @@ func requestAPI(apiCategory string, params ...string) (resp *http.Response, err
|
|||
}
|
||||
URL.Path += "/"
|
||||
parameters := url.Values{}
|
||||
parameters.Add("apikey", api.apiKey)
|
||||
|
||||
switch apiCategory {
|
||||
case "search":
|
||||
parameters.Add("s", params[0])
|
||||
|
@ -174,10 +188,15 @@ func requestAPI(apiCategory string, params ...string) (resp *http.Response, err
|
|||
parameters.Add("t", params[0])
|
||||
parameters.Add("y", params[1])
|
||||
parameters.Add("type", params[2])
|
||||
parameters.Add("Season", params[3])
|
||||
parameters.Add("Episode", params[4])
|
||||
parameters.Add("plot", plot)
|
||||
parameters.Add("tomatoes", tomatoes)
|
||||
case "id":
|
||||
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("tomatoes", tomatoes)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,26 @@
|
|||
package gomdb
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"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) {
|
||||
tests := []struct {
|
||||
|
@ -22,8 +42,10 @@ func TestSearch(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
api := Init(apiKey)
|
||||
|
||||
for i, item := range tests {
|
||||
resp, err := Search(item.query)
|
||||
resp, err := api.Search(item.query)
|
||||
if err != nil {
|
||||
t.Errorf("Test[%d]: %s", i, err)
|
||||
continue
|
||||
|
@ -47,15 +69,17 @@ func TestFailSearch(t *testing.T) {
|
|||
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
|
||||
}
|
||||
|
||||
api := Init(apiKey)
|
||||
|
||||
for i, item := range tests {
|
||||
_, err := Search(item.query)
|
||||
_, err := api.Search(item.query)
|
||||
if err == nil {
|
||||
t.Errorf("Test[%d]: Got nil error", i)
|
||||
continue
|
||||
}
|
||||
// Checking for strings is bad. But the API might change.
|
||||
if err.Error() != "Movie not found!" {
|
||||
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err)
|
||||
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
@ -69,21 +93,23 @@ func TestInvalidCategory(t *testing.T) {
|
|||
{&QueryData{Title: "Dexter", SearchType: "bad"}},
|
||||
}
|
||||
|
||||
api := Init(apiKey)
|
||||
|
||||
for i, item := range tests {
|
||||
_, err := Search(item.query)
|
||||
_, err := api.Search(item.query)
|
||||
if err == nil {
|
||||
t.Errorf("Test[%d]: Got nil error", i)
|
||||
continue
|
||||
}
|
||||
// Checking for strings is bad. But the error type is formatted
|
||||
if err.Error() != "Invalid search category- bad" {
|
||||
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err)
|
||||
t.Errorf("Test[%d]: Unexpected value- %s", i, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMovieByTitle(t *testing.T) {
|
||||
func TestMediaByTitle(t *testing.T) {
|
||||
tests := []struct {
|
||||
query *QueryData
|
||||
title string
|
||||
|
@ -101,10 +127,22 @@ func TestMovieByTitle(t *testing.T) {
|
|||
"Macbeth",
|
||||
"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 {
|
||||
resp, err := MovieByTitle(item.query)
|
||||
resp, err := api.MovieByTitle(item.query)
|
||||
if err != nil {
|
||||
t.Errorf("Test[%d]: %s", i, err)
|
||||
continue
|
||||
|
@ -120,31 +158,38 @@ func TestMovieByTitle(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMovieByImdbID(t *testing.T) {
|
||||
func TestMediaByImdbID(t *testing.T) {
|
||||
tests := []struct {
|
||||
id string
|
||||
query *QueryData
|
||||
title string
|
||||
year string
|
||||
}{
|
||||
{
|
||||
"tt0137523",
|
||||
{&QueryData{ImdbId: "tt0137523", SearchType: MovieSearch},
|
||||
"Fight Club",
|
||||
"1999",
|
||||
},
|
||||
{
|
||||
"tt1798709",
|
||||
{&QueryData{ImdbId: "tt1798709", SearchType: MovieSearch},
|
||||
"Her",
|
||||
"2013",
|
||||
},
|
||||
{
|
||||
"tt2884018",
|
||||
{&QueryData{ImdbId: "tt2884018", SearchType: MovieSearch},
|
||||
"Macbeth",
|
||||
"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 {
|
||||
resp, err := MovieByImdbID(item.id)
|
||||
resp, err := api.MovieByImdbID(item.query)
|
||||
if err != nil {
|
||||
t.Errorf("Test[%d]: %s", i, err)
|
||||
continue
|
||||
|
|
Loading…
Reference in a new issue