- Fix spotify image import on scrobble for new artists/albums
- Create image resizer
This commit is contained in:
Daniel Mason 2022-01-08 20:46:13 +13:00
parent cca9974728
commit 723077ce2d
8 changed files with 101 additions and 19 deletions

View file

@ -62,8 +62,14 @@ func insertAlbum(name string, mbid string, spotifyId string, artists []string, i
if album.Img == "" {
if img != "" {
album.Img = img
album.updateAlbum("img", img, tx)
err := importImage(album.UUID, img)
if err != nil {
log.Printf("Failed to import image: %+v. For Album: %s", err, album.Name)
return album, nil
}
album.Img = "pending"
_ = album.updateAlbum("img", "pending", tx)
}
}

View file

@ -72,8 +72,14 @@ func insertArtist(name string, mbid string, spotifyId string, img string, tx *sq
if artist.Img == "" {
if img != "" {
artist.Img = img
artist.updateArtist("img", img, tx)
err := importImage(artist.UUID, img)
if err != nil {
log.Printf("Failed to import image: %+v. For Album: %s", err, artist.Name)
return artist, nil
}
artist.Img = "pending"
_ = artist.updateArtist("img", "pending", tx)
}
}

View file

@ -3,8 +3,11 @@ package goscrobble
import (
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/disintegration/imaging"
)
func importImage(uuid string, url string) error {
@ -33,13 +36,67 @@ func importImage(uuid string, url string) error {
return err
}
// Goroutine the resize to keep it _faaaast_
go resizeImage(uuid)
return nil
}
func resizeImage(uuid string) {
// resize to 300x300 and maybe smaller?
return
func resizeImages() {
resizeBulk("artists")
resizeBulk("albums")
}
func resizeBulk(recordType string) {
// Fetch pending 500 at a time cause we do it every minute anyway
rows, err := db.Query("SELECT BIN_TO_UUID(`uuid`, true) FROM `" + recordType + "` WHERE `img` = 'pending' LIMIT 500")
if err != nil {
log.Printf("Failed to get pending images: %+v", err)
return
}
// Fetch pending 100 at a time
for rows.Next() {
var uuid string
err := rows.Scan(&uuid)
if err != nil {
log.Printf("Failed to fetch record image resize: %+v", err)
rows.Close()
return
}
// Run the resize to 300px
success := resizeImage(uuid, 300)
if !success {
// If we get an error.. lets just remove the image link for now so it can reimport
_, err = db.Exec("UPDATE `"+recordType+"` SET `img` = NULL WHERE `uuid` = UUID_TO_BIN(?,true)", uuid)
} else {
// Update DB to reflect complete
_, err = db.Exec("UPDATE `"+recordType+"` SET `img` = 'complete' WHERE `uuid` = UUID_TO_BIN(?,true)", uuid)
}
}
rows.Close()
}
func resizeAlbums() {
}
func resizeImage(uuid string, size int) bool {
// Open source image
src, err := imaging.Open(DataDirectory + string(os.PathSeparator) + "img" + string(os.PathSeparator) + uuid + "_full.jpg")
if err != nil {
log.Printf("Failed to open image: %+v", err)
return false
}
// Resize image to specified size
resizedImage := imaging.Resize(src, size, size, imaging.Lanczos)
// Save resized image
err = imaging.Save(resizedImage, DataDirectory+string(os.PathSeparator)+"img"+string(os.PathSeparator)+uuid+"_300px.jpg")
if err != nil {
log.Printf("failed to save image: %v", err)
return false
}
return true
}

View file

@ -324,12 +324,14 @@ func (user *User) updateImageDataFromSpotify() {
for uuid, img := range toUpdate {
album, err = getAlbumByUUID(uuid)
err = importImage(uuid, img)
if err != nil {
continue
}
// Update
_ = album.updateAlbum("img", "pending", tx)
}
tx.Commit()
return

View file

@ -10,8 +10,8 @@ var endTicker chan bool
func StartBackgroundWorkers() {
endTicker := make(chan bool)
hourTicker := time.NewTicker(time.Duration(1) * time.Hour)
minuteTicker := time.NewTicker(time.Duration(60) * time.Second)
hourTicker := time.NewTicker(time.Duration(1) * time.Hour)
go func() {
for {
@ -19,6 +19,15 @@ func StartBackgroundWorkers() {
case <-endTicker:
fmt.Println("Stopping background workers")
return
case <-minuteTicker.C:
// Update playdata from Spotify
go updateSpotifyData()
// Update playdate from Navidrome
go updateNavidromeData()
// There should only be whatever new images are in pending
go resizeImages()
case <-hourTicker.C:
// Clear old password reset tokens
go clearOldResetTokens()
@ -26,12 +35,6 @@ func StartBackgroundWorkers() {
// Attempt to pull missing images from spotify - hackerino version!
user, _ := getUserByUsername("idanoo")
go user.updateImageDataFromSpotify()
case <-minuteTicker.C:
// Update playdata from Spotify
go updateSpotifyData()
// Update playdate from Navidrome
go updateNavidromeData()
}
}
}()