-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (86 loc) · 2.04 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
package main
import (
"bufio"
"html/template"
"log"
"net/http"
"os"
"strconv"
"github.com/ryanbradynd05/go-tmdb"
)
var tpl *template.Template
//Shows Struct for tv Shows
type Shows struct {
Name string
ImageLinks string
}
type allShows []Shows
type myShows []Shows
//Data Struct that holds data to be rendered into template
type Data struct {
Allshows allShows
Myshows myShows
}
func init() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}
func main() {
http.HandleFunc("/", indexHandler)
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
log.Fatal(http.ListenAndServe(":8081", nil))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
tmdb1 := tmdb.Init("5beb2bf1821813990f328d5c98b5fdc5")
var tvInfo *tmdb.TvPagedResults
var err error
var tvResults []tmdb.TvShort
option := make(map[string]string)
totPages := 1
for i := 1; i <= totPages; i++ {
option["page"] = strconv.Itoa(i)
tvInfo, err = tmdb1.GetTvAiringToday(option)
if err != nil {
log.Fatal(err)
}
totPages = tvInfo.TotalPages
tvResults = append(tvResults, tvInfo.Results...)
}
lines, err := readLines("showlist.txt")
if err != nil {
log.Fatalf("readLines: %s", err)
}
var allshows allShows
var myshows myShows
for _, result := range tvResults {
for _, line := range lines {
if line == result.Name {
myshows = append(myshows, Shows{
line,
"https://image.tmdb.org/t/p/original" + result.PosterPath,
})
}
}
allshows = append(allshows, Shows{
result.Name,
"https://image.tmdb.org/t/p/original" + result.PosterPath,
})
}
data := &Data{allshows, myshows}
tplerr := tpl.ExecuteTemplate(w, "index.gohtml", data)
if tplerr != nil {
log.Fatalln(tplerr)
}
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}