/
service.go
219 lines (188 loc) · 5.75 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/tsotosa/atmm/config"
"github.com/tsotosa/atmm/gconst"
"github.com/tsotosa/atmm/model"
"go.uber.org/zap"
"io"
"io/ioutil"
"log"
"net/http"
"os"
)
func searchTvShow(query string) (response model.TheMovieDbSearchTvResponse, err error) {
//fmt.Printf("\nsearching for tv show with query:%#v\n", query)
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, config.Conf.TheMovieDbBaseApiUrlV3+"search/tv", nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("api_key", config.Conf.ApiKey)
q.Add("query", query)
q.Add("language", "en-US")
q.Add("page", "1")
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
fmt.Println("Errored when sending request to the server")
return
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var result model.TheMovieDbSearchTvResponse
if err := json.Unmarshal(responseBody, &result); err != nil {
fmt.Println("Failed to unmarshall JSON")
fmt.Println(err)
}
return result, nil
}
func getTvShowEpisodeDetails(tvId, seasonNumber, episodeNumber string) (response model.TheMovieDbTvShowEpisodeDetails, err error) {
//fmt.Printf("\nsearching for tv show episode with query:%#v %#v %#v\n", tvId, seasonNumber, episodeNumber)
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet,
fmt.Sprintf("%s/tv/%s/season/%s/episode/%s",
config.Conf.TheMovieDbBaseApiUrlV3, tvId, seasonNumber, episodeNumber), nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("api_key", config.Conf.ApiKey)
//q.Add("query", "query")
q.Add("language", "en-US")
//q.Add("page", "1")
// assign encoded query string to http request
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
fmt.Println("Errored when sending request to the server")
return
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode == http.StatusNotFound {
return model.TheMovieDbTvShowEpisodeDetails{}, errors.New(string(rune(resp.StatusCode)))
}
var result model.TheMovieDbTvShowEpisodeDetails
if err := json.Unmarshal(responseBody, &result); err != nil {
fmt.Println("Failed to unmarshall JSON")
fmt.Println(err)
}
return result, nil
}
func GetGithubReleases(page int) ([]model.GithubRelease, error) {
var res []model.GithubRelease
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet,
fmt.Sprintf("%srepos/%s/%s/releases", gconst.GithubApiBaseUrl, "TsotosA", "atmm"), nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Accept", gconst.GithubJsonAcceptHeader)
if config.Conf.GithubUsername != "" && config.Conf.GithubPersonalToken != "" {
req.SetBasicAuth(config.Conf.GithubUsername, config.Conf.GithubPersonalToken)
}
resp, err := client.Do(req)
if err != nil {
zap.S().Warnf("could not complete api call with error: %v", err)
return res, err
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
zap.S().Warnf("could not read response body with error: %v", err)
return res, err
}
if resp.StatusCode == http.StatusNotFound {
zap.S().Warnf("github relied with: %v", resp.StatusCode)
return res, errors.New(fmt.Sprintf("github replied with %v", resp.StatusCode))
}
if err := json.Unmarshal(responseBody, &res); err != nil {
zap.S().Warnf("could not unmarshall response body with error: %v", err)
return res, err
}
return res, nil
}
func DownloadUrlToLocation(filename, url, location string) error {
zap.S().Debugf("downloading update binary from url:%s", url)
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
zap.S().Warnf("could not download update binary with err: %v", err)
return err
}
req.Header.Set("Accept", gconst.HttpContentTypeOctetStream)
if config.Conf.GithubUsername != "" && config.Conf.GithubPersonalToken != "" {
req.SetBasicAuth(config.Conf.GithubUsername, config.Conf.GithubPersonalToken)
}
res, err := client.Do(req)
if err != nil {
zap.S().Warnf("could not complete api call with error: %v", err)
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
zap.S().Warnf("received %v intead of the expected 200", res.StatusCode)
return errors.New("received non 200 response code")
}
err = os.MkdirAll(location, 0777)
if err != nil {
zap.S().Warnf("could not create the required directories with error: %v", err)
return err
}
file, err := os.Create(fmt.Sprintf("%s%s", location, filename))
if err != nil {
zap.S().Warnf("could not create empty file with error: %v", err)
return err
}
defer file.Close()
_, err = io.Copy(file, res.Body)
if err != nil {
zap.S().Warnf("could not copy with error: %v", err)
return err
}
err = file.Chmod(0777)
if err != nil {
zap.S().Warnf("could not change permissions to new binary: %v", err)
return err
}
return nil
}
func searchMovie(query string) (response model.TheMovieDbSearchMovieResponse, err error) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, config.Conf.TheMovieDbBaseApiUrlV3+"search/movie", nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("api_key", config.Conf.ApiKey)
q.Add("query", query)
q.Add("language", "en-US")
q.Add("page", "1")
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
fmt.Println("Errored when sending request to the server")
return
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var result model.TheMovieDbSearchMovieResponse
if err := json.Unmarshal(responseBody, &result); err != nil {
fmt.Println("Failed to unmarshall JSON")
fmt.Println(err)
}
return result, nil
}