-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (123 loc) · 3.75 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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/boltdb/bolt"
)
const (
// DefaultDBPath is a default path to the database file
DefaultDBPath = "feed.db"
// Default podcast title
DefaultPodcastTitle = "YouCast"
)
var args struct {
Title string
ListenAddr string
DBPath string
StoragePath string
DevMode bool
}
func main() {
log.Println("YouCast version", Version)
flag.StringVar(&args.Title, "title", os.Getenv("PODCAST_TITLE"), "Podcast title")
flag.StringVar(&args.ListenAddr, "l", os.Getenv("LISTEN_ADDR"), "Listen address")
flag.StringVar(&args.DBPath, "db", os.Getenv("DB_PATH"), "Path to the database")
flag.StringVar(&args.StoragePath, "storage-dir", os.Getenv("STORAGE_PATH"), "Path to the directory where to store downloaded files")
flag.BoolVar(&args.DevMode, "dev", false, "Development mode (read assets from ./assets on each request)")
flag.Parse()
if args.Title == "" {
args.Title = DefaultPodcastTitle
}
if p, ok := os.LookupEnv("PORT"); ok {
args.ListenAddr = ":" + p
}
if args.ListenAddr == "" {
log.Fatalln("missing LISTEN_ADDR")
}
if args.DBPath == "" {
log.Println("missing DB_PATH, using", DefaultDBPath, "as a default")
args.DBPath = DefaultDBPath
}
if args.StoragePath == "" {
log.Fatalln("missing STORAGE_PATH")
}
db, err := bolt.Open(args.DBPath, 0600, nil)
if err != nil {
log.Fatalln("failed to open BoltDB file ", args.DBPath, " :", err)
}
storage := newBoltStorage("feed", db)
jobQueue := NewDownloadJobQueue(db)
go NewDownloadWorker(
jobQueue,
storage,
NewHTTPDownloader("", nil),
NewFFMpeg(),
).Run(context.Background(), 10*time.Second)
svc := NewFeedService(
storage,
args.StoragePath,
jobQueue,
NewHTTPDownloader("", nil),
NewFFMpeg(),
)
srv := NewFeedServer(PodcastMetadata{
Title: args.Title,
Description: "These videos could have been a podcast...",
}, svc)
srv.RegisterProvider("/yt", &YouTubeProvider{})
cachePath := path.Join(os.TempDir(), "youcast")
if err := os.MkdirAll(cachePath, os.ModePerm); err != nil && !os.IsExist(err) {
log.Fatalf("failed to create temporary directory %s: %s", cachePath, err)
}
srv.RegisterProvider("/my", NewUploadedMediaProvider(cachePath))
if token, ok := os.LookupEnv("TELEGRAM_API_TOKEN"); ok {
p, err := NewTelegramProvider(token, os.Getenv("TELEGRAM_API_ENDPOINT"), os.Getenv("TELEGRAM_FILE_SERVER"))
if err != nil {
log.Printf("failed to initialize telegram provider: %s", err)
} else {
srv.RegisterProvider("/tg", p)
for _, idStr := range strings.Split(os.Getenv("TELEGRAM_ALLOWED_USERS"), ",") {
id, err := strconv.Atoi(strings.TrimSpace(idStr))
if err != nil {
log.Printf("failed to whitelist user with id '%s': %s", idStr, err)
continue
}
p.WhitelistUser(id)
}
tgUpdates, err := p.Updates(context.Background())
if err != nil {
log.Printf("failed to start telegram updates consumption loop: %s", err)
} else {
go func() {
for audio := range tgUpdates {
meta, err := audio.Metadata(context.Background())
if err != nil {
log.Printf("failed to fetch %s data: %s", p.Name(), err)
continue
}
u, err := audio.DownloadURL(context.Background())
if err != nil {
log.Printf("failed to fetch download URL for %s: %s", p.Name(), err)
continue
}
if err := svc.AddItem(NewPodcastItem(meta, time.Now()), u); err != nil {
log.Printf("failed to add %s item to the feed: %s", p.Name(), err)
continue
}
}
}()
}
}
}
log.Println("starting server on", args.ListenAddr, "...")
if err := http.ListenAndServe(args.ListenAddr, CORSMiddleware(ProfileMiddleware(srv.ServeMux()))); err != nil {
log.Fatalln(err)
}
}