Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
)

func main() {
api := gomdb.Init(YOUR_API_KEY)
api := gomdb.Init(YOUR_API_KEY)

query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch}
res, err := api.Search(query)
if err != nil {
Expand All @@ -48,13 +49,30 @@ func main() {
}
fmt.Println(res2)

res3, err := api.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)
```


Expand Down
17 changes: 13 additions & 4 deletions gomdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
baseURL = "http://www.omdbapi.com/?"
baseURL = "http://www.omdbapi.com"
plot = "full"
tomatoes = "true"

Expand All @@ -33,6 +33,8 @@ type QueryData struct {
Year string
ImdbId string
SearchType string
Season string
Episode string
}

//SearchResult is the type for the search results
Expand Down Expand Up @@ -114,7 +116,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)
resp, err := api.requestAPI("title", query.Title, query.Year, query.SearchType, query.Season,
query.Episode)
if err != nil {
return nil, err
}
Expand All @@ -133,8 +136,9 @@ func (api *OmdbApi) MovieByTitle(query *QueryData) (*MovieResult, error) {
}

//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
func (api *OmdbApi) MovieByImdbID(id string) (*MovieResult, error) {
resp, err := api.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
}
Expand Down Expand Up @@ -184,10 +188,15 @@ 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)
}
Expand Down
44 changes: 32 additions & 12 deletions gomdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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")
Expand Down Expand Up @@ -40,7 +41,9 @@ func TestSearch(t *testing.T) {
"2015",
},
}

api := Init(apiKey)

for i, item := range tests {
resp, err := api.Search(item.query)
if err != nil {
Expand All @@ -67,6 +70,7 @@ func TestFailSearch(t *testing.T) {
}

api := Init(apiKey)

for i, item := range tests {
_, err := api.Search(item.query)
if err == nil {
Expand All @@ -75,7 +79,7 @@ func TestFailSearch(t *testing.T) {
}
// 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
}
}
Expand All @@ -90,6 +94,7 @@ func TestInvalidCategory(t *testing.T) {
}

api := Init(apiKey)

for i, item := range tests {
_, err := api.Search(item.query)
if err == nil {
Expand All @@ -98,13 +103,13 @@ func TestInvalidCategory(t *testing.T) {
}
// 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
Expand All @@ -122,6 +127,16 @@ 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)
Expand All @@ -143,33 +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 := api.MovieByImdbID(item.id)
resp, err := api.MovieByImdbID(item.query)
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
Expand Down