2023-01-13 19:23:36 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-01-19 12:05:46 +01:00
|
|
|
"context"
|
|
|
|
"os"
|
2023-01-13 20:04:57 +01:00
|
|
|
"os/exec"
|
2023-01-19 12:05:46 +01:00
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2023-01-13 20:04:57 +01:00
|
|
|
|
|
|
|
"git.milar.in/milarin/channel"
|
2023-01-13 19:23:36 +01:00
|
|
|
"git.milar.in/nyaanime/logic"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
2023-01-19 12:05:46 +01:00
|
|
|
var (
|
|
|
|
// AppCtx notifies all threads to finish their work and shutdown
|
|
|
|
AppCtx, cancelAppCtx = context.WithCancel(context.Background())
|
|
|
|
// AppExitWg is waiting for all threads until they are done
|
|
|
|
AppExitWg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
Runner = InitializeRunner()
|
|
|
|
)
|
2023-01-15 19:43:36 +01:00
|
|
|
|
2023-01-13 19:23:36 +01:00
|
|
|
func main() {
|
2023-01-19 12:05:46 +01:00
|
|
|
signalChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
go func() {
|
|
|
|
<-signalChan
|
|
|
|
exit()
|
|
|
|
}()
|
|
|
|
|
2023-01-13 20:04:57 +01:00
|
|
|
// check for ffprobe in PATH
|
|
|
|
if _, err := exec.LookPath("ffprobe"); err != nil {
|
|
|
|
panic(err) // TODO error handling
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:23:36 +01:00
|
|
|
// get access token once at startup to be sure that an access token is obtainable at all
|
|
|
|
if _, err := logic.GetAnilistAccessToken(); err != nil {
|
|
|
|
panic(err) // TODO error handling
|
|
|
|
}
|
|
|
|
|
2023-01-15 21:09:26 +01:00
|
|
|
if err := InitTelegramBot(); err != nil {
|
|
|
|
panic(err) // TODO error handling
|
|
|
|
}
|
|
|
|
|
2023-01-14 12:11:10 +01:00
|
|
|
logic.PrintPriorityTables()
|
|
|
|
|
2023-01-15 19:43:36 +01:00
|
|
|
fileChan, err := WatchDirectory(fsnotify.Create, DownloadPath)
|
2023-01-13 19:23:36 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err) // TODO error handling
|
|
|
|
}
|
|
|
|
|
2023-01-15 19:43:36 +01:00
|
|
|
fileHandleChan := channel.Map(fileChan, NewFileHandle)
|
|
|
|
workChan, logChan := channel.Tee(fileHandleChan)
|
|
|
|
|
2023-01-19 12:05:46 +01:00
|
|
|
AppExitWg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer AppExitWg.Done()
|
|
|
|
channel.Each(workChan, HandleFileInRunner)
|
|
|
|
}()
|
|
|
|
|
2023-01-15 19:43:36 +01:00
|
|
|
channel.Each(logChan, PrintFileHandle)
|
2023-01-13 19:23:36 +01:00
|
|
|
}
|
2023-01-14 12:11:10 +01:00
|
|
|
|
2023-01-15 19:43:36 +01:00
|
|
|
func HandleFileInRunner(fh *FileHandle) {
|
|
|
|
Runner.Run(func() { HandleFile(fh) })
|
2023-01-14 12:11:10 +01:00
|
|
|
}
|
2023-01-19 12:05:46 +01:00
|
|
|
|
|
|
|
func exit() {
|
|
|
|
cancelAppCtx() // notify all threads to shutdown
|
|
|
|
AppExitWg.Wait() // wait for threads until shutdown
|
|
|
|
}
|