/
movie-service.go
75 lines (60 loc) · 1.79 KB
/
movie-service.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
69
70
71
72
73
74
75
package services
import (
"os"
"fmt"
"net/http"
"io/ioutil"
"encoding/json"
"github.com/kindlewit/go-festility/models"
"github.com/kindlewit/go-festility/constants"
)
const BASE_URL = "https://api.themoviedb.org/3"
// Fetch movie details from TMDB API.
func GetMovie(movieID string) (data models.TMDBmovie, err error) {
var API_KEY = os.Getenv("API_KEY");
// Read https://developers.themoviedb.org/3/movies/get-movie-details
resp, err := http.Get(fmt.Sprintf("%s/movie/%s?api_key=%s", BASE_URL, movieID, API_KEY));
if err != nil {
return data, constants.ErrApiFetch;
}
defer resp.Body.Close();
// TODO: status check
body, err := ioutil.ReadAll(resp.Body);
if err != nil {
fmt.Println(err.Error());
return data, constants.ErrApiParse;
}
err = json.Unmarshal(body, &data);
if err != nil {
fmt.Println(err.Error());
return data, constants.ErrApiParse;
}
return data, nil;
}
// Get director details of a movie id from TMDB API.
func GetDirector(movieID string) (data []string, err error) {
var API_KEY = os.Getenv("API_KEY");
var castsList struct {
Crew []models.Crew `json:"crew"`
};
// Read https://developers.themoviedb.org/3/movies/get-movie-credits
resp, err := http.Get(fmt.Sprintf("%s/movie/%s/credits?api_key=%s", BASE_URL, movieID, API_KEY));
if err != nil {
fmt.Println(err.Error());
return data, constants.ErrApiFetch;
}
defer resp.Body.Close();
// TODO: status check
body, err := ioutil.ReadAll(resp.Body);
if err != nil {
fmt.Println(err.Error());
return data, constants.ErrApiParse;
}
err = json.Unmarshal(body, &castsList);
for i := 0; i < len(castsList.Crew); i++ {
if castsList.Crew[i].Job == "Director" {
data = append(data, castsList.Crew[i].Name);
}
}
return data, nil;
}