-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmovies.go
68 lines (65 loc) · 1.84 KB
/
movies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package imdb
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
// Movie supports the lookup of IMDB movies by title.
func Movie(title string) {
url := "http://www.omdbapi.com?apikey=" + os.Getenv("OMDBKEY") + "&t="
term := strings.ReplaceAll(title, " ", "+")
resp, err := http.Get(url + term + "&plot=short")
if err != nil {
fmt.Printf("[!] Error in request: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("[!] Error in reading response body: %s", err)
}
cartographer(body)
}
// cartographer is used to handle conversions from byte string to marshaled json bytes.
func cartographer(body []byte) []byte {
type RatingsType struct {
Source string `json:"Source"`
Value string `json:"Value"`
}
type Movies struct {
Title string `json:"Title"`
Year string `json:"Year"`
Rated string `json:"Rated"`
Released string `json:"Released"`
Runtime string `json:"Runtime"`
Genre string `json:"Genre"`
Director string `json:"Director"`
Writer string `json:"Writer"`
Actors string `json:"Actors"`
Plot string `json:"Plot"`
Language string `json:"Language"`
Country string `json:"Country"`
Awards string `json:"Awards"`
Poster string `json:"Poster"`
Ratings []RatingsType
Metascore string `json:"Metascore"`
ImdbRating string `json:"imdbRating"`
ImdbVotes string `json:"imdbVotes"`
ImdbID string `json:"imdbID"`
Type string `json:"Type"`
DVD string `json:"DVD"`
BoxOffice string `json:"BoxOffice"`
Production string `json:"Production"`
Website string `json:"Website"`
Response string `json:"Response"`
}
data := &Movies{}
err := json.Unmarshal(body, data)
if err != nil {
fmt.Println("[!] Error unmarshaling: ", err)
}
b, _ := json.Marshal(data)
return b
}