diff --git a/.gitignore b/.gitignore index 38d2912..b5b4739 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .idea *.iml *.db3 +*.db3-* *.bak* /downloads/ /temp/ diff --git a/main.go b/main.go index 8f27242..0eb1310 100644 --- a/main.go +++ b/main.go @@ -1,25 +1,31 @@ package main +import ( + "flag" + "github.com/playwright-community/playwright-go" + "log" +) + func main() { - //var err error - //var onlyInstall = flag.Bool("install", false, "install the required browser and do nothing else") - //flag.Parse() - // - //options := &playwright.RunOptions{ - // Browsers: []string{"firefox"}, - //} - //err = playwright.Install(options) - //if err != nil { - // log.Panicf("error installing playwright: %v", err) - //} - // - //if *onlyInstall { - // return - //} - // - //db := openDatabase() - //defer db.Close() - // + var err error + var onlyInstall = flag.Bool("install", false, "install the required browser and do nothing else") + flag.Parse() + + options := &playwright.RunOptions{ + Browsers: []string{"firefox"}, + } + err = playwright.Install(options) + if err != nil { + log.Panicf("error installing playwright: %v", err) + } + + if *onlyInstall { + return + } + + db := openDatabase() + defer db.MustClose() + //sleepTimeStr := os.Getenv("VIVAPLUS_SLEEPTIME") //sleepTime := 15 //if sleepTimeStr != "" { @@ -65,5 +71,5 @@ func main() { // log.Printf("Sleeping %d minutes until next run", sleepTime) // time.Sleep(time.Duration(sleepTime) * time.Minute) //} - serveWebview() + serveWebview(db) } diff --git a/migrations/5_add_watch_state.sql b/migrations/5_add_watch_state.sql new file mode 100644 index 0000000..cb1c592 --- /dev/null +++ b/migrations/5_add_watch_state.sql @@ -0,0 +1 @@ +ALTER TABLE videos ADD COLUMN watch_state TEXT; \ No newline at end of file diff --git a/res/index.html b/res/index.html new file mode 100644 index 0000000..e69de29 diff --git a/viewmodels.go b/viewmodels.go index 9c6fd2a..f7685d1 100644 --- a/viewmodels.go +++ b/viewmodels.go @@ -3,4 +3,5 @@ package main type VideoInfoVM struct { Title string Description string + Thumbnail string } diff --git a/web/index.gohtml b/web/index.gohtml index 45d2b06..0c2945a 100644 --- a/web/index.gohtml +++ b/web/index.gohtml @@ -11,6 +11,7 @@

{{.Title}}

{{.Description}}
+ Thumbnail
diff --git a/webview.go b/webview.go index 444ccd3..4c12a9f 100644 --- a/webview.go +++ b/webview.go @@ -1,28 +1,47 @@ package main import ( + "gitea.seeseepuff.be/seeseemelk/mysqlite" "log" "net/http" ) -func serveWebview() { +func serveWebview(db *mysqlite.Db) { addr := "localhost:8081" log.Printf("Listening on %s", addr) http.Handle("/static/", http.StripPrefix("/static/", staticFileServer())) - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - vm := VideoInfoVM{ - Title: "Foo", - Description: "Lorem Ipsum sed dolor...", - } - err := renderTemplate(w, "index", vm) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } - }) + http.HandleFunc("/", serveIndex(db)) + http.HandleFunc("/video/", serveVideo(db)) err := http.ListenAndServe(addr, nil) if err != nil { panic(err) } } + +func serveIndex(db *mysqlite.Db) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + // Query the database for the oldest unwatched episode + var title, description, thumbnail string + err := db.Query(`SELECT title, description, thumbnail FROM videos WHERE (watch_state IS NULL OR watch_state != 'watched') ORDER BY upload_date, episode LIMIT 1`).ScanSingle(&title, &description, &thumbnail) + if err != nil { + http.Error(w, "No unwatched episodes found", http.StatusNotFound) + return + } + + vm := VideoInfoVM{ + Title: title, + Description: description, + Thumbnail: thumbnail, + } + err = renderTemplate(w, "index", vm) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + } +} + +func serveVideo(db *mysqlite.Db) func(w http.ResponseWriter, r *http.Request) { + +}