-
Notifications
You must be signed in to change notification settings - Fork 2
/
omdb.go
56 lines (40 loc) · 1.07 KB
/
omdb.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
func SearchContent(title string) map[string]string {
var movielist map[string]*json.RawMessage
var searches = make([]*json.RawMessage, 2)
var target map[string]string
req, err := http.NewRequest("GET", "http://omdbapi.com", nil)
if err != nil {
panic(err)
}
//API for searching movie
q := req.URL.Query()
q.Add("s", title)
q.Add("apikey", "98bba0d3")
req.URL.RawQuery = q.Encode()
resp, _ := http.Get(req.URL.String())
l_body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(l_body, &movielist)
json.Unmarshal(*movielist["Search"], &searches)
//Top search
json.Unmarshal(*searches[0], &target)
imdbID := target["imdbID"] //IMDb id for metadata
req, err = http.NewRequest("GET", "http://omdbapi.com", nil)
if err != nil {
panic(err)
}
//API for getting metadata
q = req.URL.Query()
q.Add("i", imdbID)
q.Add("apikey", "98bba0d3")
req.URL.RawQuery = q.Encode()
resp, _ = http.Get(req.URL.String())
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &target)
return target
}