Finish migration to mysqlite
Some checks failed
Build / build (push) Failing after 31s

This commit is contained in:
Sebastiaan de Schaetzen 2025-03-10 06:45:58 +01:00
parent 4b42e4f998
commit ae765fdf64
2 changed files with 121 additions and 110 deletions

View File

@ -16,7 +16,7 @@ import (
"time" "time"
) )
func DownloadAllVideos(db *mysqlite.Db) error { func DownloadAllVideos(db *mysqlite.Db) (rerr error) {
// Ensure no partial videos exist // Ensure no partial videos exist
tempDir := os.Getenv("VIVAPLUS_DOWNLOAD") tempDir := os.Getenv("VIVAPLUS_DOWNLOAD")
if tempDir == "" { if tempDir == "" {
@ -43,10 +43,18 @@ func DownloadAllVideos(db *mysqlite.Db) error {
log.Printf("Starting downloads...") log.Printf("Starting downloads...")
for { for {
err = downloadVideo(db, downloadDir, tempDir)
if err != nil {
return err
}
}
}
func downloadVideo(db *mysqlite.Db, downloadDir string, tempDir string) (rerr error) {
// Fetch the next record from the database // Fetch the next record from the database
var id, episode int var id, episode int
var href, cast, title, description, uploadDateStr, thumbnailUrl string var href, cast, title, description, uploadDateStr, thumbnailUrl string
err = db.Query("select id, url, `cast`, title, description, upload_date, thumbnail, episode from videos where state = 'pending' and episode is not null order by year, episode limit 1"). err := db.Query("select id, url, `cast`, title, description, upload_date, thumbnail, episode from videos where state = 'pending' and episode is not null order by year, episode limit 1").
ScanSingle(&id, &href, &cast, &title, &description, &uploadDateStr, &thumbnailUrl, &episode) ScanSingle(&id, &href, &cast, &title, &description, &uploadDateStr, &thumbnailUrl, &episode)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
log.Printf("No videos found for downloading") log.Printf("No videos found for downloading")
@ -104,7 +112,10 @@ func DownloadAllVideos(db *mysqlite.Db) error {
} }
nfoString := xml.Header + string(nfoData) nfoString := xml.Header + string(nfoData)
nfoFile, err := os.Create(filepath.Join(destinationDir, baseFileName+".nfo")) nfoFile, err := os.Create(filepath.Join(destinationDir, baseFileName+".nfo"))
defer nfoFile.Close() if err != nil {
return fmt.Errorf("error creating NFO file: %w", err)
}
defer forwardError(nfoFile.Close(), &rerr)
_, err = nfoFile.WriteString(nfoString) _, err = nfoFile.WriteString(nfoString)
if err != nil { if err != nil {
return fmt.Errorf("error writing NFO file: %w", err) return fmt.Errorf("error writing NFO file: %w", err)
@ -120,12 +131,12 @@ func DownloadAllVideos(db *mysqlite.Db) error {
if err != nil { if err != nil {
return fmt.Errorf("error creating thumbnail: %w", err) return fmt.Errorf("error creating thumbnail: %w", err)
} }
defer thumbnailFile.Close() defer forwardError(thumbnailFile.Close(), &rerr)
thumbnailResp, err := http.Get(thumbnailUrl) thumbnailResp, err := http.Get(thumbnailUrl)
if err != nil { if err != nil {
return fmt.Errorf("error fetching thumbnail: %w", err) return fmt.Errorf("error fetching thumbnail: %w", err)
} }
defer thumbnailResp.Body.Close() defer forwardError(thumbnailResp.Body.Close(), &rerr)
_, err = io.Copy(thumbnailFile, thumbnailResp.Body) _, err = io.Copy(thumbnailFile, thumbnailResp.Body)
if err != nil { if err != nil {
return fmt.Errorf("error writing thumbnail: %w", err) return fmt.Errorf("error writing thumbnail: %w", err)
@ -136,23 +147,16 @@ func DownloadAllVideos(db *mysqlite.Db) error {
if err != nil { if err != nil {
return fmt.Errorf("error starting transaction: %w", err) return fmt.Errorf("error starting transaction: %w", err)
} }
defer tx.Rollback() defer forwardError(tx.Rollback(), &rerr)
result, err := tx.Exec("update videos set state = 'done' where id = ?", id) err = tx.Query("update videos set state = 'done' where id = ?").Bind(id).Exec()
if err != nil { if err != nil {
return fmt.Errorf("error updating database: %w", err) return fmt.Errorf("error updating database: %w", err)
} }
count, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("error getting number of rows affected: %d", err)
}
if count != 1 {
return fmt.Errorf("unexpected number of rows changed (expected exactly 1): %d", count)
}
err = tx.Commit() err = tx.Commit()
if err != nil { if err != nil {
return fmt.Errorf("error commiting transaction: %w", err) return fmt.Errorf("error commiting transaction: %w", err)
} }
log.Printf("Finished downloading file") log.Printf("Finished downloading file")
} return nil
} }

7
util.go Normal file
View File

@ -0,0 +1,7 @@
package main
func forwardError(err error, to *error) {
if err != nil {
*to = err
}
}