Merge pull request #10 from agnivade/master

Code cleanup and new features
This commit is contained in:
Christopher Herrera 2016-06-20 11:47:25 -04:00 committed by GitHub
commit 3fce0cbc12
4 changed files with 231 additions and 86 deletions

0
LICENSE Executable file → Normal file
View file

37
README.md Executable file → Normal file
View file

@ -17,17 +17,42 @@ I *strongly* encourage you to check it out and contribute to keep it growing.
*** ***
Project Usage Project Usage
------------- -------------
The API usage is very simple. Just import the go-imdb package The API usage is very simple.
```go
package main
import ( import (
imdb "github.com/eefret/go-imdb" "fmt"
"github.com/eefret/gomdb"
) )
And use any of the methods func main() {
query := &gomdb.QueryData{Title: "Macbeth", SearchType: gomdb.MovieSearch}
res, err := gomdb.Search(query)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res.Search)
query = &gomdb.QueryData{Title: "Macbeth", Year: "2015"}
res2, err := gomdb.MovieByTitle(query)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res2)
res3, err := gomdb.MovieByImdbID("tt2884018")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res3)
}
```
res, err := imdb.Search("The fifth element", "")
res2, err := imdb.MovieByTitle("True Grit", "1969")
res3, err := imdb.MovieByImdbID("tt2015381")
See the project documentation to see the Response Objects and stuff See the project documentation to see the Response Objects and stuff

106
gomdb.go Executable file → Normal file
View file

@ -1,19 +1,4 @@
/* // Package gomdb is a golang implementation of the OMDB API.
Copyright 2014 Kaissersoft Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gomdb package gomdb
import ( import (
@ -24,21 +9,23 @@ import (
"net/url" "net/url"
) )
//======================================================================= const (
// Const baseURL = "http://www.omdbapi.com/?"
//======================================================================= plot = "full"
tomatoes = "true"
const baseURL string = "http://www.omdbapi.com/?" MovieSearch = "movie"
const plot string = "full" SeriesSearch = "series"
const tomatoes string = "true" EpisodeSearch = "episode"
)
//======================================================================= // QueryData is the type to create the search query
// Global vars type QueryData struct {
//======================================================================= Title string
Year string
//======================================================================= ImdbId string
// Structs SearchType string
//======================================================================= }
//SearchResult is the type for the search results //SearchResult is the type for the search results
type SearchResult struct { type SearchResult struct {
@ -53,6 +40,7 @@ type SearchResponse struct {
Search []SearchResult Search []SearchResult
Response string Response string
Error string Error string
totalResults int
} }
//MovieResult is the result struct of an specific movie search //MovieResult is the result struct of an specific movie search
@ -95,13 +83,9 @@ type MovieResult struct {
Error string Error string
} }
//======================================================================= //Search for movies given a Title and year, Year is optional you can pass nil
// Funcs func Search(query *QueryData) (*SearchResponse, error) {
//======================================================================= resp, err := requestAPI("search", query.Title, query.Year, query.SearchType)
//Search search for movies given a Title and year, Year is optional you can pass nil
func Search(title string, year string) (*SearchResponse, error) {
resp, err := requestAPI(title, "", "", year)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -121,8 +105,8 @@ func Search(title string, year string) (*SearchResponse, error) {
} }
//MovieByTitle returns a MovieResult given Title //MovieByTitle returns a MovieResult given Title
func MovieByTitle(title string, year string) (*MovieResult, error) { func MovieByTitle(query *QueryData) (*MovieResult, error) {
resp, err := requestAPI("", "", title, year) resp, err := requestAPI("title", query.Title, query.Year, query.SearchType)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -142,7 +126,7 @@ func MovieByTitle(title string, year string) (*MovieResult, error) {
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381" //MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
func MovieByImdbID(id string) (*MovieResult, error) { func MovieByImdbID(id string) (*MovieResult, error) {
resp, err := requestAPI("", id, "", "") resp, err := requestAPI("id", id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -160,36 +144,44 @@ func MovieByImdbID(id string) (*MovieResult, error) {
return r, nil return r, nil
} }
func requestAPI(s string, i string, t string, y string) (resp *http.Response, err error) { // helper function to call the API
//s = Search Parameter, if this is != nil then its a searchMovies // param: apiCategory refers to which API we are calling. Can be "search", "title" or "id"
//i = Id Parameter, if this is != nil then its a getMovieByImdbID // Depending on that value, we will search by "t" or "s" or "i"
//t = Title Parameter, if this is != nil then its a getMovieByTitle // param: params are the variadic list of params passed for that category
//y = Year Parameter, Optional data for s and t search func requestAPI(apiCategory string, params ...string) (resp *http.Response, err error) {
//var res http.Response
var URL *url.URL var URL *url.URL
URL, err = url.Parse(baseURL) URL, err = url.Parse(baseURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Checking for invalid category
if len(params) > 1 && params[2] != "" {
if params[2] != MovieSearch &&
params[2] != SeriesSearch &&
params[2] != EpisodeSearch {
return nil, errors.New("Invalid search category- " + params[2])
}
}
URL.Path += "/" URL.Path += "/"
parameters := url.Values{} parameters := url.Values{}
if len(s) > 0 { switch apiCategory {
parameters.Add("s", s) case "search":
parameters.Add("y", y) parameters.Add("s", params[0])
} else if len(i) > 0 { parameters.Add("y", params[1])
parameters.Add("i", i) parameters.Add("type", params[2])
case "title":
parameters.Add("t", params[0])
parameters.Add("y", params[1])
parameters.Add("type", params[2])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
} else if len(t) > 0 { case "id":
parameters.Add("t", t) parameters.Add("i", params[0])
parameters.Add("plot", plot) parameters.Add("plot", plot)
parameters.Add("tomatoes", tomatoes) parameters.Add("tomatoes", tomatoes)
parameters.Add("y", y)
} else {
return nil, errors.New("Invalid Request")
} }
URL.RawQuery = parameters.Encode() URL.RawQuery = parameters.Encode()
res, err := http.Get(URL.String()) res, err := http.Get(URL.String())
err = checkErr(res.StatusCode) err = checkErr(res.StatusCode)

162
gomdb_test.go Executable file → Normal file
View file

@ -2,32 +2,160 @@ package gomdb
import "testing" import "testing"
func TestImdbSearchMovies(t *testing.T) { func TestSearch(t *testing.T) {
resp, err := Search("Fight Club", "1999") tests := []struct {
if err != nil { query *QueryData
t.Error("Failed Search Movies") title string
year string
}{
{&QueryData{Title: "Fight Club", Year: "1999", SearchType: MovieSearch},
"Fight Club",
"1999",
},
{&QueryData{Title: "Her"},
"Her",
"2013",
},
{&QueryData{Title: "Macbeth", Year: "2015"},
"Macbeth",
"2015",
},
}
for i, item := range tests {
resp, err := Search(item.query)
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
}
if resp.Search[0].Title != item.title {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.title, resp.Search[0].Title)
continue
}
if resp.Search[0].Year != item.year {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.year, resp.Search[0].Year)
continue
} }
if resp.Search[0].Title != "Fight Club" {
t.Error("Wrong Movie")
} }
} }
func TestImdbGetMovieByTitle(t *testing.T) { func TestFailSearch(t *testing.T) {
resp, err := MovieByTitle("Fight Club", "1999") tests := []struct {
if err != nil { query *QueryData
t.Error("Failed GetMovieByTitle") }{
{&QueryData{Title: "Game of Thrones", Year: "2001"}},
{&QueryData{Title: "Dexter", SearchType: EpisodeSearch}},
}
for i, item := range tests {
_, 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, Got- %s", i, err)
continue
} }
if resp.Title != "Fight Club" {
t.Error("Wrong Movie")
} }
} }
func TestImdbGetMovieByImdbID(t *testing.T) { func TestInvalidCategory(t *testing.T) {
resp, err := MovieByImdbID("tt0137523") tests := []struct {
query *QueryData
}{
{&QueryData{Title: "Game of Thrones", Year: "2001", SearchType: "bad"}},
{&QueryData{Title: "Dexter", SearchType: "bad"}},
}
for i, item := range tests {
_, 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, Got- %s", i, err)
continue
}
}
}
func TestMovieByTitle(t *testing.T) {
tests := []struct {
query *QueryData
title string
year string
}{
{&QueryData{Title: "Fight Club", Year: "1999", SearchType: MovieSearch},
"Fight Club",
"1999",
},
{&QueryData{Title: "Her"},
"Her",
"2013",
},
{&QueryData{Title: "Macbeth", Year: "2015"},
"Macbeth",
"2015",
},
}
for i, item := range tests {
resp, err := MovieByTitle(item.query)
if err != nil { if err != nil {
t.Error("Failed GetMovieByImdbID") t.Errorf("Test[%d]: %s", i, err)
continue
}
if resp.Title != item.title {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.title, resp.Title)
continue
}
if resp.Year != item.year {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.year, resp.Year)
continue
}
}
}
func TestMovieByImdbID(t *testing.T) {
tests := []struct {
id string
title string
year string
}{
{
"tt0137523",
"Fight Club",
"1999",
},
{
"tt1798709",
"Her",
"2013",
},
{
"tt2884018",
"Macbeth",
"2015",
},
}
for i, item := range tests {
resp, err := MovieByImdbID(item.id)
if err != nil {
t.Errorf("Test[%d]: %s", i, err)
continue
}
if resp.Title != item.title {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.title, resp.Title)
continue
}
if resp.Year != item.year {
t.Errorf("Test[%d]: Expected- %s, Got- %s", i, item.year, resp.Year)
continue
} }
if resp.Title != "Fight Club" {
t.Error("Wrong Movie")
} }
} }