Compare commits

..

No commits in common. "5f94d7703235f122318a39a7f2972560a25344e1" and "1cebe777a7e66f7eb1ce0334efafc89e61a65982" have entirely different histories.

3 changed files with 33 additions and 114 deletions

View file

@ -1,8 +1,5 @@
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)
@ -31,10 +28,8 @@ import (
)
func main() {
api := gomdb.Init(YOUR_API_KEY)
query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch}
res, err := api.Search(query)
res, err := gomdb.Search(query)
if err != nil {
fmt.Println(err)
return
@ -42,38 +37,26 @@ func main() {
fmt.Println(res.Search)
query = &gomdb.QueryData{Title: "Macbeth", Year: "2015"}
res2, err := api.MovieByTitle(query)
res2, err := gomdb.MovieByTitle(query)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res2)
query = &gomdb.QueryData{Title: "Rick and Morty", Season: "1", Episode: "8", SearchType: gomdb.EpisodeSearch}
res3, err := api.MovieByTitle(query)
res3, err := gomdb.MovieByImdbID("tt2884018")
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)

View file

@ -10,7 +10,7 @@ import (
)
const (
baseURL = "http://www.omdbapi.com"
baseURL = "http://www.omdbapi.com/?"
plot = "full"
tomatoes = "true"
@ -19,22 +19,12 @@ 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
@ -94,8 +84,8 @@ type MovieResult struct {
}
//Search for movies given a Title and year, Year is optional you can pass nil
func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
resp, err := api.requestAPI("search", query.Title, query.Year, query.SearchType)
func Search(query *QueryData) (*SearchResponse, error) {
resp, err := requestAPI("search", query.Title, query.Year, query.SearchType)
if err != nil {
return nil, err
}
@ -115,9 +105,8 @@ func (api *OmdbApi) Search(query *QueryData) (*SearchResponse, error) {
}
//MovieByTitle returns a MovieResult given Title
func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season,
query.Episode)
func MovieByTitle(query *QueryData) (*MovieResult, error) {
resp, err := requestAPI("title", query.Title, query.Year, query.SearchType)
if err != nil {
return nil, err
}
@ -136,9 +125,8 @@ func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
}
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
func (api *OmdbApi) MovieByImdbID(query *QueryData) (*MovieResult, error) {
resp, err := api.requestAPI("id", query.ImdbId, query.Year, query.SearchType, query.Season,
query.Episode)
func MovieByImdbID(id string) (*MovieResult, error) {
resp, err := requestAPI("id", id)
if err != nil {
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"
// 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 (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
URL, err = url.Parse(baseURL)
if err != nil {
@ -177,8 +165,6 @@ func (api *OmdbApi) requestAPI(apiCategory string, params ...string) (resp *http
}
URL.Path += "/"
parameters := url.Values{}
parameters.Add("apikey", api.apiKey)
switch apiCategory {
case "search":
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("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)
}

View file

@ -1,26 +1,6 @@
package gomdb
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)
}
}
}
import "testing"
func TestSearch(t *testing.T) {
tests := []struct {
@ -42,10 +22,8 @@ func TestSearch(t *testing.T) {
},
}
api := Init(apiKey)
for i, item := range tests {
resp, err := api.Search(item.query)
resp, err := Search(item.query)
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
@ -69,17 +47,15 @@ func TestFailSearch(t *testing.T) {
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
}
api := Init(apiKey)
for i, item := range tests {
_, err := api.Search(item.query)
_, err := 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", i, err)
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err)
continue
}
}
@ -93,23 +69,21 @@ func TestInvalidCategory(t *testing.T) {
{&QueryData{Title: "Dexter", SearchType: "bad"}},
}
api := Init(apiKey)
for i, item := range tests {
_, err := api.Search(item.query)
_, err := 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", i, err)
t.Errorf("Test[%d]: Unexpected value- %s, Got- %s", i, err)
continue
}
}
}
func TestMediaByTitle(t *testing.T) {
func TestMovieByTitle(t *testing.T) {
tests := []struct {
query *QueryData
title string
@ -127,22 +101,10 @@ func TestMediaByTitle(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 := api.MovieByTitle(item.query)
resp, err := MovieByTitle(item.query)
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
@ -158,38 +120,31 @@ func TestMediaByTitle(t *testing.T) {
}
}
func TestMediaByImdbID(t *testing.T) {
func TestMovieByImdbID(t *testing.T) {
tests := []struct {
query *QueryData
id string
title string
year string
}{
{&QueryData{ImdbId: "tt0137523", SearchType: MovieSearch},
{
"tt0137523",
"Fight Club",
"1999",
},
{&QueryData{ImdbId: "tt1798709", SearchType: MovieSearch},
{
"tt1798709",
"Her",
"2013",
},
{&QueryData{ImdbId: "tt2884018", SearchType: MovieSearch},
{
"tt2884018",
"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 := api.MovieByImdbID(item.query)
resp, err := MovieByImdbID(item.id)
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue