fixing formatting and linting package
This commit is contained in:
commit
4509bed0db
1 changed files with 31 additions and 12 deletions
43
imdb.go
43
imdb.go
|
@ -21,12 +21,13 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
//=======================================================================
|
//=======================================================================
|
||||||
// Const
|
// Const
|
||||||
//=======================================================================
|
//=======================================================================
|
||||||
|
|
||||||
const baseURL string = "http://www.omdbapi.com/?"
|
const baseURL string = "http://www.omdbapi.com/?"
|
||||||
const plot string = "full"
|
const plot string = "full"
|
||||||
const tomatoes string = "true"
|
const tomatoes string = "true"
|
||||||
|
@ -103,9 +104,11 @@ func SearchMovies(title string, year string) (*SearchResponse, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
r := new(SearchResponse)
|
r := new(SearchResponse)
|
||||||
err = json.NewDecoder(resp.Body).Decode(r)
|
err = json.NewDecoder(resp.Body).Decode(r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -122,9 +125,11 @@ func GetMovieByTitle(title string, year string) (*MovieResult, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
r := new(MovieResult)
|
r := new(MovieResult)
|
||||||
err = json.NewDecoder(resp.Body).Decode(r)
|
err = json.NewDecoder(resp.Body).Decode(r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -140,9 +145,11 @@ func GetMovieByImdbID(id string) (*MovieResult, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
r := new(MovieResult)
|
r := new(MovieResult)
|
||||||
err = json.NewDecoder(resp.Body).Decode(r)
|
err = json.NewDecoder(resp.Body).Decode(r)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -158,20 +165,32 @@ func omdbAPIRequest(s string, i string, t string, y string) (resp *http.Response
|
||||||
//t = Title Parameter, if this is != nil then its a getMovieByTitle
|
//t = Title Parameter, if this is != nil then its a getMovieByTitle
|
||||||
//y = Year Parameter, Optional data for s and t search
|
//y = Year Parameter, Optional data for s and t search
|
||||||
//var res http.Response
|
//var res http.Response
|
||||||
var url string
|
|
||||||
if s != "" {
|
var URL *url.URL
|
||||||
s = strings.Replace(s, " ", "%20", -1)
|
URL, err = url.Parse(baseURL)
|
||||||
url = fmt.Sprintf(baseURL+"s=%s&y=%s", s, y)
|
|
||||||
} else if i != "" {
|
if err != nil {
|
||||||
url = fmt.Sprintf(baseURL+"i=%s&plot=%s&tomatoes=%s", i, plot, tomatoes)
|
return nil, err
|
||||||
} else if t != "" {
|
}
|
||||||
t = strings.Replace(t, " ", "%20", -1)
|
URL.Path += "/"
|
||||||
url = fmt.Sprintf(baseURL+"t=%s&plot=%s&tomatoes=%s&y=%s", t, plot, tomatoes, y)
|
parameters := url.Values{}
|
||||||
|
if len(s) > 0 {
|
||||||
|
parameters.Add("s", s)
|
||||||
|
parameters.Add("y", y)
|
||||||
|
} else if len(i) > 0 {
|
||||||
|
parameters.Add("i", i)
|
||||||
|
parameters.Add("plot", plot)
|
||||||
|
parameters.Add("tomatoes", tomatoes)
|
||||||
|
} else if len(t) > 0 {
|
||||||
|
parameters.Add("t", t)
|
||||||
|
parameters.Add("plot", plot)
|
||||||
|
parameters.Add("tomatoes", tomatoes)
|
||||||
|
parameters.Add("y", y)
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New("Invalid Request")
|
return nil, errors.New("Invalid Request")
|
||||||
}
|
}
|
||||||
//log.Print(url) //DEBUG
|
URL.RawQuery = parameters.Encode()
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(URL.String())
|
||||||
err = checkErrorStatus(res.StatusCode)
|
err = checkErrorStatus(res.StatusCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in a new issue