-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
284 lines (244 loc) · 8.01 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"bytes"
"context"
"encoding/csv"
"flag"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/doingodswork/stremio-top-movies/pkg/stremio"
)
const (
version = "0.1.0"
)
var (
bindAddr = flag.String("bindAddr", "localhost", `Local interface address to bind to. "localhost" only allows access from the local host. "0.0.0.0" binds to all network interfaces.`)
port = flag.Int("port", 8080, "Port to listen on")
dataDir = flag.String("dataDir", ".", "Location of the data directory. It contains CSV files with IMDb IDs and a \"metas\" subdirectory with meta JSON files")
logLevel = flag.String("logLevel", "info", `Log level to show only logs with the given and more severe levels. Can be "trace", "debug", "info", "warn", "error", "fatal", "panic"`)
)
var (
manifest = stremio.Manifest{
ID: "tv.deflix.stremio-top-movies",
Name: "Top movies",
Description: "Multiple catalogs of top movie lists: IMDb Top 250, IMDb Most Popular, Top Box Office (US), Rotten Tomatoes Certified Fresh Movies, Academy Award for Best Picture, Cannes Film Festival Palme d'Or winners",
Version: version,
ResourceItems: resources,
Types: []string{"movie"},
Catalogs: catalogs,
IDprefixes: []string{"tt"},
// Must use www.deflix.tv instead of just deflix.tv because GitHub takes care of redirecting non-www to www and this leads to HTTPS certificate issues.
Background: "https://www.deflix.tv/images/Logo-1024px.png",
Logo: "https://www.deflix.tv/images/Logo-250px.png",
}
resources = []stremio.ResourceItem{
stremio.ResourceItem{
Name: "catalog",
},
}
catalogs = []stremio.CatalogItem{
stremio.CatalogItem{
Type: "movie",
ID: "imdb-top-250",
Name: "IMDb Top Rated (a.k.a. \"IMDb Top 250\")"},
stremio.CatalogItem{
Type: "movie",
ID: "imdb-most-popular",
Name: "IMDb Most Popular"},
stremio.CatalogItem{
Type: "movie",
ID: "top-box-office-us",
Name: "Top Box Office (US) (last weekend)"},
stremio.CatalogItem{
Type: "movie",
ID: "rt-certified-fresh",
Name: "Rotten Tomatoes Certified Fresh (DVD + Streaming)"},
stremio.CatalogItem{
Type: "movie",
ID: "academy-awards-winners",
Name: "Academy Award for Best Picture"},
stremio.CatalogItem{
Type: "movie",
ID: "palme-dor-winners",
Name: "Cannes Film Festival Palme d'Or winners"},
}
)
const (
redirectURL = "https://www.deflix.tv"
)
var (
imdbTop250CatalogResponse []byte
imdbMostPopularCatalogResponse []byte
boxOfficeUScatalogResponse []byte
rtCertifiedFreshCatalogResponse []byte
academyAwardsCatalogResponse []byte
palmeDorCatalogResponse []byte
)
var (
// Time used in response header so the client caches responses this long
resCacheSeconds = strconv.Itoa(1 * 24 * 60 * 60) // 1d
)
func init() {
// Timeout for global default HTTP client (for when using `http.Get()`)
http.DefaultClient.Timeout = 5 * time.Second
// Configure logging (except for level, which we only know from the config which is obtained later).
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
}
func main() {
flag.Parse()
setLogLevel(*logLevel)
// Clean input
if strings.HasSuffix(*dataDir, "/") {
*dataDir = strings.TrimRight(*dataDir, "/")
}
log.Println("Initializing catalogs...")
imdbTop250CatalogResponse = createCatalogResponse("imdb-top-250")
imdbMostPopularCatalogResponse = createCatalogResponse("imdb-most-popular")
boxOfficeUScatalogResponse = createCatalogResponse("box-office-weekend-us")
rtCertifiedFreshCatalogResponse = createCatalogResponse("rt-certified-fresh-dvd-streaming")
academyAwardsCatalogResponse = createCatalogResponse("academy-awards-winners")
palmeDorCatalogResponse = createCatalogResponse("palme-dor-winners")
log.Println("Initialized catalogs")
log.Println("Setting up server...")
r := mux.NewRouter()
s := r.Methods("GET").Subrouter()
s.Use(timerMiddleware,
corsMiddleware, // Stremio doesn't show stream responses when no CORS middleware is used!
handlers.ProxyHeaders,
recoveryMiddleware,
loggingMiddleware)
s.HandleFunc("/health", healthHandler)
// Stremio endpoints
s.HandleFunc("/manifest.json", manifestHandler)
s.HandleFunc("/catalog/{type}/{id}.json", catalogHandler)
// Additional endpoints
// Root redirects to website
s.HandleFunc("/", rootHandler)
addr := *bindAddr + ":" + strconv.Itoa(*port)
srv := &http.Server{
Addr: addr,
Handler: s,
// Timeouts to avoid Slowloris attacks
ReadTimeout: time.Second * 5,
WriteTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
MaxHeaderBytes: 1 * 1000, // 1 KB
}
log.Println("Set up server")
stopping := false
stoppingPtr := &stopping
log.Printf("Starting server on %v", addr)
go func() {
if err := srv.ListenAndServe(); err != nil {
if !*stoppingPtr {
log.Fatalf("Couldn't start server: %v", err)
} else {
log.Fatalf("Error in srv.ListenAndServe() during server shutdown (probably context deadline expired before the server could shutdown cleanly): %v", err)
}
}
}()
// Timed logger for easier debugging with logs
go func() {
for {
log.Trace("...")
time.Sleep(time.Second)
}
}()
// Graceful shutdown
c := make(chan os.Signal, 1)
// Accept SIGINT (Ctrl+C) and SIGTERM (`docker stop`)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
sig := <-c
log.Printf("Received signal %v, shutting down...\n", sig)
*stoppingPtr = true
// Create a deadline to wait for. `docker stop` gives us 10 seconds.
// No need to get the cancel func and defer calling it, because srv.Shutdown() will consider the timeout from the context.
ctx, _ := context.WithTimeout(context.Background(), 9*time.Second)
// Doesn't block if no connections, but will otherwise wait until the timeout deadline
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Error shutting down server: %v", err)
}
log.Println("Server shut down")
}
func createCatalogResponse(catalog string) []byte {
buf := bytes.NewBufferString(`{"metas":[`)
records := read(*dataDir + "/" + catalog + ".csv")
metas := readMetas(records, *dataDir+"/metas")
for i, meta := range metas {
buf.WriteString(meta)
if i < len(metas)-1 {
buf.WriteString(",")
}
}
buf.WriteString("]}")
return buf.Bytes()
}
func read(filePath string) [][]string {
fileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
log.Fatalf("Couldn't read file: %v", err)
}
csvReader := csv.NewReader(bytes.NewReader(fileBytes))
records, err := csvReader.ReadAll()
if err != nil {
log.Fatalf("Couldn't read CSV: %v", err)
}
return records
}
func readMetas(records [][]string, metasDir string) []string {
headRecord := records[0]
imdbIndex := 0
found := false
for ; imdbIndex < len(headRecord); imdbIndex++ {
if headRecord[imdbIndex] == "IMDb ID" {
found = true
break
}
}
if !found {
log.Fatalf("Couldn't find \"IMDb ID\" in CSV header: %v", headRecord)
}
var result []string
for _, record := range records[1:] {
imdbID := record[imdbIndex]
// We assume that the metafetcher has been used to already write all meta JSON files for all required IMDb IDs to the directory, so we can directly read the files here via the IMDb ID + ".json", instead of going through the actual files and only read it when it matches one of our IMDb IDs.
fileContent, err := ioutil.ReadFile(metasDir + "/" + imdbID + ".json")
if err != nil {
log.Errorf("Couldn't read meta file for IMDb ID %v: %v", imdbID, err)
continue
}
result = append(result, string(fileContent))
}
return result
}
func setLogLevel(logLevel string) {
switch logLevel {
case "trace":
log.SetLevel(log.TraceLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "panic":
log.SetLevel(log.PanicLevel)
default:
log.WithField("logLevel", logLevel).Fatal("Unknown logLevel")
}
}