2014-09-21 17:57:53 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-06-26 20:07:46 +00:00
|
|
|
package gomdb
|
2014-09-21 17:48:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-09-26 21:34:33 +00:00
|
|
|
"net/url"
|
2014-09-21 17:48:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
// Const
|
|
|
|
//=======================================================================
|
2014-09-28 18:10:53 +00:00
|
|
|
|
2014-09-28 18:07:05 +00:00
|
|
|
const baseURL string = "http://www.omdbapi.com/?"
|
2014-09-21 17:48:10 +00:00
|
|
|
const plot string = "full"
|
|
|
|
const tomatoes string = "true"
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
// Global vars
|
|
|
|
//=======================================================================
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
// Structs
|
|
|
|
//=======================================================================
|
|
|
|
|
2014-09-28 18:07:05 +00:00
|
|
|
//SearchResult is the type for the search results
|
2014-09-21 17:48:10 +00:00
|
|
|
type SearchResult struct {
|
|
|
|
Title string
|
|
|
|
Year string
|
2014-09-28 18:07:05 +00:00
|
|
|
ImdbID string
|
2014-09-21 17:48:10 +00:00
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
2014-09-28 18:07:05 +00:00
|
|
|
//SearchResponse is the struct of the response in a search
|
2014-09-21 17:48:10 +00:00
|
|
|
type SearchResponse struct {
|
|
|
|
Search []SearchResult
|
|
|
|
Response string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
2014-09-28 18:07:05 +00:00
|
|
|
//MovieResult is the result struct of an specific movie search
|
2014-09-21 17:48:10 +00:00
|
|
|
type MovieResult struct {
|
|
|
|
Title string
|
|
|
|
Year string
|
|
|
|
Rated string
|
|
|
|
Released string
|
|
|
|
Runtime string
|
|
|
|
Genre string
|
|
|
|
Director string
|
|
|
|
Writer string
|
|
|
|
Actors string
|
|
|
|
Plot string
|
|
|
|
Language string
|
|
|
|
Country string
|
|
|
|
Awards string
|
|
|
|
Poster string
|
|
|
|
Metascore string
|
2014-09-21 20:24:51 +00:00
|
|
|
ImdbRating string
|
|
|
|
ImdbVotes string
|
|
|
|
ImdbID string
|
2014-09-21 17:48:10 +00:00
|
|
|
Type string
|
2014-09-21 20:24:51 +00:00
|
|
|
TomatoMeter string
|
|
|
|
TomatoImage string
|
|
|
|
TomatoRating string
|
|
|
|
TomatoReviews string
|
|
|
|
TomatoFresh string
|
|
|
|
TomatoRotten string
|
|
|
|
TomatoConsensus string
|
|
|
|
TomatoUserMeter string
|
|
|
|
TomatoUserRating string
|
|
|
|
TomatoUserReviews string
|
2014-09-21 17:48:10 +00:00
|
|
|
DVD string
|
|
|
|
BoxOffice string
|
|
|
|
Production string
|
|
|
|
Website string
|
|
|
|
Response string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
// Funcs
|
|
|
|
//=======================================================================
|
|
|
|
|
2015-07-17 20:16:57 +00:00
|
|
|
//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)
|
2014-09-21 17:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-25 17:31:39 +00:00
|
|
|
defer resp.Body.Close()
|
2014-09-21 17:48:10 +00:00
|
|
|
|
|
|
|
r := new(SearchResponse)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(r)
|
2014-09-26 21:34:33 +00:00
|
|
|
|
2014-09-21 17:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Response == "False" {
|
|
|
|
return r, errors.New(r.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:16:57 +00:00
|
|
|
//MovieByTitle returns a MovieResult given Title
|
|
|
|
func MovieByTitle(title string, year string) (*MovieResult, error) {
|
|
|
|
resp, err := requestAPI("", "", title, year)
|
2014-09-21 17:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-25 17:31:39 +00:00
|
|
|
defer resp.Body.Close()
|
2014-09-21 17:48:10 +00:00
|
|
|
|
|
|
|
r := new(MovieResult)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(r)
|
2014-09-26 21:34:33 +00:00
|
|
|
|
2014-09-21 17:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Response == "False" {
|
|
|
|
return r, errors.New(r.Error)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:16:57 +00:00
|
|
|
//MovieByImdbID returns a MovieResult given a ImdbID ex:"tt2015381"
|
|
|
|
func MovieByImdbID(id string) (*MovieResult, error) {
|
|
|
|
resp, err := requestAPI("", id, "", "")
|
2014-09-21 17:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-25 17:31:39 +00:00
|
|
|
defer resp.Body.Close()
|
2014-09-21 17:48:10 +00:00
|
|
|
|
|
|
|
r := new(MovieResult)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(r)
|
2014-09-26 21:34:33 +00:00
|
|
|
|
2014-09-21 17:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if r.Response == "False" {
|
|
|
|
return r, errors.New(r.Error)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:16:57 +00:00
|
|
|
func requestAPI(s string, i string, t string, y string) (resp *http.Response, err error) {
|
2014-09-21 17:48:10 +00:00
|
|
|
//s = Search Parameter, if this is != nil then its a searchMovies
|
2014-09-28 18:07:05 +00:00
|
|
|
//i = Id Parameter, if this is != nil then its a getMovieByImdbID
|
2014-09-21 17:48:10 +00:00
|
|
|
//t = Title Parameter, if this is != nil then its a getMovieByTitle
|
|
|
|
//y = Year Parameter, Optional data for s and t search
|
|
|
|
//var res http.Response
|
2014-09-28 18:10:53 +00:00
|
|
|
|
|
|
|
var URL *url.URL
|
|
|
|
URL, err = url.Parse(baseURL)
|
2014-09-26 21:34:33 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-28 18:10:53 +00:00
|
|
|
URL.Path += "/"
|
2014-09-26 21:34:33 +00:00
|
|
|
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)
|
2014-09-21 17:48:10 +00:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("Invalid Request")
|
|
|
|
}
|
2014-09-28 18:10:53 +00:00
|
|
|
URL.RawQuery = parameters.Encode()
|
|
|
|
res, err := http.Get(URL.String())
|
2015-07-17 20:16:57 +00:00
|
|
|
err = checkErr(res.StatusCode)
|
2014-09-21 17:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:16:57 +00:00
|
|
|
func checkErr(status int) error {
|
2014-09-21 17:48:10 +00:00
|
|
|
if status != 200 {
|
2014-09-28 18:07:05 +00:00
|
|
|
return fmt.Errorf("Status Code %d received from IMDB", status)
|
2014-09-21 17:48:10 +00:00
|
|
|
}
|
2014-09-28 18:07:05 +00:00
|
|
|
return nil
|
2014-09-21 17:48:10 +00:00
|
|
|
}
|
2014-09-21 20:50:45 +00:00
|
|
|
|
|
|
|
//Stringer Interface for MovieResult
|
|
|
|
func (mr MovieResult) String() string {
|
|
|
|
return fmt.Sprintf("#%s: %s (%s)", mr.ImdbID, mr.Title, mr.Year)
|
|
|
|
}
|
|
|
|
|
2014-09-21 20:55:44 +00:00
|
|
|
//Stringer Interface for SearchResult
|
2014-09-21 20:50:45 +00:00
|
|
|
func (sr SearchResult) String() string {
|
2014-09-28 18:07:05 +00:00
|
|
|
return fmt.Sprintf("#%s: %s (%s) Type: %s", sr.ImdbID, sr.Title, sr.Year, sr.Type)
|
2014-09-21 20:50:45 +00:00
|
|
|
}
|