-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (131 loc) · 3.66 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
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/xinsnake/youtube2podcast/config"
"google.golang.org/api/googleapi/transport"
youtube "google.golang.org/api/youtube/v3"
)
const envConfigPath = "Y2P_CONFIG_PATH"
var cfg config.Config
var yService *youtube.Service
func main() {
readConfigFiles()
testDirectoryPermission()
testDependencyApplications()
prepareYouTubeService()
go serveStaticContent()
go fireOffFetchLoop()
for {
time.Sleep(3600 * time.Second)
}
}
func readConfigFiles() {
log.Printf("Reading config files")
var err error
configPath := os.Getenv(envConfigPath)
if configPath == "" {
log.Fatalf("Config file path (%s) cannot be empty", envConfigPath)
}
cfg, err = config.Parse(configPath)
if err != nil {
log.Fatalf("Unable to parse config file: %v", err)
}
}
func testDirectoryPermission() {
log.Printf("Test directory permission")
testFile := filepath.Join(cfg.DataDir, "test")
f, err := os.OpenFile(testFile, os.O_CREATE|os.O_RDWR, 0644)
defer f.Close()
if err != nil {
log.Fatalf("Folder permission test failed (create): %s: %v", testFile, err)
}
err = os.Remove(testFile)
if err != nil {
log.Fatalf("Folder permission test failed (delete): %s: %v", testFile, err)
}
}
func testDependencyApplications() {
log.Printf("Test dependency applications")
ffmpegCmd := exec.Command(cfg.Exec.Ffmpeg, "-version")
output, err := ffmpegCmd.CombinedOutput()
if err != nil {
log.Fatalf("Unable to find ffmpeg: %s: %v", output, err)
}
youtubeDlCmd := exec.Command(cfg.Exec.Youtubedl, "--version")
output, err = youtubeDlCmd.CombinedOutput()
if err != nil {
log.Fatalf("Unable to find youtube-dl: %s: %v", output, err)
}
}
func serveStaticContent() {
log.Printf("Start serving static assets")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" || r.URL.Path == "index.html" {
indexFile := filepath.Join(cfg.StaticDir, "index.html")
tmpl, err := template.ParseFiles(indexFile)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Unable to parse template: %v", err)))
return
}
feeds, err := getCurrentFeeds()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Unable to parse feeds: %v", err)))
return
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, struct {
Config config.Config
Feeds []rssRoot
}{
cfg,
feeds,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Unable to execute template: %v", err)))
return
}
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
return
}
if r.URL.Path == "/favicon.ico" || r.URL.Path == "/logo.png" {
staticPath := filepath.Join(cfg.StaticDir, r.URL.Path)
http.ServeFile(w, r, staticPath)
return
}
path := filepath.Join(cfg.DataDir, r.URL.Path)
if f, err := os.Stat(path); err == nil && !f.IsDir() {
http.ServeFile(w, r, path)
return
}
http.NotFound(w, r)
})
err := http.ListenAndServe(fmt.Sprintf(":%s", cfg.Port), nil)
if err != nil {
log.Fatalf("Unable to listen to port %s: %v", cfg.Port, err)
}
}
func prepareYouTubeService() {
log.Printf("Preparing YouTube service")
var err error
yClient := &http.Client{Transport: &transport.APIKey{Key: cfg.GoogleAPIKey}}
yService, err = youtube.New(yClient)
if err != nil {
log.Fatalf("Error creating YouTube service: %v", err)
}
if _, err = yService.Activities.List("id").
ChannelId(cfg.Channels[0].ChannelID).MaxResults(1).Do(); err != nil {
log.Fatalf("Error getting a working YouTube service: %v", err)
}
}