initial commit

This commit is contained in:
Eefret 2014-09-21 13:48:10 -04:00
commit c510a8d14d
2 changed files with 205 additions and 0 deletions

173
imdb.go Normal file
View file

@ -0,0 +1,173 @@
package imdb
import (
"encoding/json"
"errors"
"fmt"
//"log"
"net/http"
"strings"
)
//=======================================================================
// Const
//=======================================================================
const baseUrl string = "http://www.omdbapi.com/?"
const plot string = "full"
const tomatoes string = "true"
//=======================================================================
// Global vars
//=======================================================================
//=======================================================================
// Structs
//=======================================================================
//Type for the Search Response
type SearchResult struct {
Title string
Year string
ImdbId string
Type string
}
//Type that respond Search
type SearchResponse struct {
Search []SearchResult
Response string
Error string
}
//Type for searching a specific movie
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
imdbRating string
imdbVotes string
imdbID string
Type string
tomatoMeter string
tomatoImage string
tomatoRating string
tomatoReviews string
tomatoFresh string
tomatoRotten string
tomatoConsensus string
tomatoUserMeter string
tomatoUserRating string
tomatoUserReviews string
DVD string
BoxOffice string
Production string
Website string
Response string
Error string
}
//=======================================================================
// Funcs
//=======================================================================
//Search for movies given a Title and year, Year is optional you can pass nil
func SearchMovies(title string, year string) (*SearchResponse, error) {
resp, err := omdbApiRequest(title, "", "", year)
if err != nil {
return nil, err
}
r := new(SearchResponse)
err = json.NewDecoder(resp.Body).Decode(r)
if err != nil {
return nil, err
}
if r.Response == "False" {
return r, errors.New(r.Error)
}
return r, nil
}
//returns a MovieResult given Title
func GetMovieByTitle(title string, year string) (*MovieResult, error) {
resp, err := omdbApiRequest("", "", title, year)
if err != nil {
return nil, err
}
r := new(MovieResult)
err = json.NewDecoder(resp.Body).Decode(r)
if err != nil {
return nil, err
}
if r.Response == "False" {
return r, errors.New(r.Error)
}
return r, nil
}
// returns a MovieResult given a ImdbId ex:"tt2015381"
func GetMovieByImdbId(id string) (*MovieResult, error) {
resp, err := omdbApiRequest("", id, "", "")
if err != nil {
return nil, err
}
r := new(MovieResult)
err = json.NewDecoder(resp.Body).Decode(r)
if err != nil {
return nil, err
}
if r.Response == "False" {
return r, errors.New(r.Error)
}
return r, nil
}
func omdbApiRequest(s string, i string, t string, y string) (resp *http.Response, err error) {
//s = Search Parameter, if this is != nil then its a searchMovies
//i = Id Parameter, if this is != nil then its a getMovieByImdbId
//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
var url string
if s != "" {
s = strings.Replace(s, " ", "%20", -1)
url = fmt.Sprintf(baseUrl+"s=%s&y=%s", s, y)
} else if i != "" {
url = fmt.Sprintf(baseUrl+"i=%s&plot=%s&tomatoes=%s", i, plot, tomatoes)
} else if t != "" {
t = strings.Replace(t, " ", "%20", -1)
url = fmt.Sprintf(baseUrl+"t=%s&plot=%s&tomatoes=%s&y=%s", t, plot, tomatoes, y)
} else {
return nil, errors.New("Invalid Request")
}
//log.Print(url) //DEBUG
res, err := http.Get(url)
err = checkErrorStatus(res.StatusCode)
if err != nil {
return nil, err
}
return res, nil
}
func checkErrorStatus(status int) error {
if status != 200 {
return errors.New(fmt.Sprintf("Status Code %d received from IMDB", status))
} else {
return nil
}
}

32
test-imdb/main.go Normal file
View file

@ -0,0 +1,32 @@
package main
import (
"fmt"
imdb "github.com/eefret/go-imdb"
"log"
)
func main() {
//Testing SearchMovies
res, err := imdb.SearchMovies("The fifth element", "")
if err != nil {
log.Fatal(err)
}
//Testing GetMovieByTitle
res2, err := imdb.GetMovieByTitle("True Grit", "1969")
if err != nil {
log.Fatal(err)
}
//Testing GetMovieByImdbId
res3, err := imdb.GetMovieByImdbId("tt2015381")
if err != nil {
log.Fatal(err)
}
fmt.Println(res.Search[0].Title)
fmt.Println(res2.Title)
fmt.Println(res3.Title)
}