mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-16 02:45:54 -07:00
chore: initial public commit
This commit is contained in:
commit
fc9054b78c
250 changed files with 32809 additions and 0 deletions
1
internal/importer/importer.go
Normal file
1
internal/importer/importer.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package importer
|
||||
90
internal/importer/maloja.go
Normal file
90
internal/importer/maloja.go
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
package importer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gabehf/koito/internal/catalog"
|
||||
"github.com/gabehf/koito/internal/cfg"
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/mbz"
|
||||
"github.com/gabehf/koito/internal/utils"
|
||||
)
|
||||
|
||||
type MalojaExport struct {
|
||||
Scrobbles []MalojaExportItem `json:"scrobbles"`
|
||||
}
|
||||
type MalojaExportItem struct {
|
||||
Time int64 `json:"time"`
|
||||
Track MalojaTrack `json:"track"`
|
||||
}
|
||||
type MalojaTrack struct {
|
||||
Artists []string `json:"artists"`
|
||||
Title string `json:"title"`
|
||||
Album struct {
|
||||
Title string `json:"albumtitle"`
|
||||
} `json:"album"`
|
||||
}
|
||||
|
||||
func ImportMalojaFile(ctx context.Context, store db.DB, filename string) error {
|
||||
l := logger.FromContext(ctx)
|
||||
l.Info().Msgf("Beginning maloja import on file: %s", filename)
|
||||
file, err := os.Open(path.Join(cfg.ConfigDir(), "import", filename))
|
||||
if err != nil {
|
||||
l.Err(err).Msgf("Failed to read import file: %s", filename)
|
||||
return err
|
||||
}
|
||||
export := new(MalojaExport)
|
||||
err = json.NewDecoder(file).Decode(&export)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, item := range export.Scrobbles {
|
||||
martists := make([]string, 0)
|
||||
// Maloja has a tendency to have the the artist order ['feature', 'main \u2022 feature'], so
|
||||
// here we try to turn that artist array into ['main', 'feature']
|
||||
item.Track.Artists = utils.MoveFirstMatchToFront(item.Track.Artists, " \u2022 ")
|
||||
for _, an := range item.Track.Artists {
|
||||
ans := strings.Split(an, " \u2022 ")
|
||||
martists = append(martists, ans...)
|
||||
}
|
||||
artists := utils.UniqueIgnoringCase(martists)
|
||||
if len(item.Track.Artists) < 1 || item.Track.Title == "" {
|
||||
l.Debug().Msg("Skipping invalid maloja import item")
|
||||
continue
|
||||
}
|
||||
ts := time.Unix(item.Time, 0)
|
||||
opts := catalog.SubmitListenOpts{
|
||||
MbzCaller: &mbz.MusicBrainzClient{},
|
||||
Artist: item.Track.Artists[0],
|
||||
ArtistNames: artists,
|
||||
TrackTitle: item.Track.Title,
|
||||
ReleaseTitle: item.Track.Album.Title,
|
||||
Time: ts,
|
||||
UserID: 1,
|
||||
}
|
||||
err = catalog.SubmitListen(ctx, store, opts)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to import maloja playback item")
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err = os.Stat(path.Join(cfg.ConfigDir(), "import_complete"))
|
||||
if err != nil {
|
||||
err = os.Mkdir(path.Join(cfg.ConfigDir(), "import_complete"), 0744)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to create import_complete dir! Import files must be removed from the import directory manually, or else the importer will run on every app start")
|
||||
}
|
||||
}
|
||||
err = os.Rename(path.Join(cfg.ConfigDir(), "import", filename), path.Join(cfg.ConfigDir(), "import_complete", filename))
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to move file to import_complete dir! Import files must be removed from the import directory manually, or else the importer will run on every app start")
|
||||
}
|
||||
l.Info().Msgf("Finished importing %s; imported %d items", filename, len(export.Scrobbles))
|
||||
return nil
|
||||
}
|
||||
76
internal/importer/spotify.go
Normal file
76
internal/importer/spotify.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package importer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/gabehf/koito/internal/catalog"
|
||||
"github.com/gabehf/koito/internal/cfg"
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/mbz"
|
||||
)
|
||||
|
||||
type SpotifyExportItem struct {
|
||||
Timestamp time.Time `json:"ts"`
|
||||
TrackName string `json:"master_metadata_track_name"`
|
||||
ArtistName string `json:"master_metadata_album_artist_name"`
|
||||
AlbumName string `json:"master_metadata_album_album_name"`
|
||||
ReasonEnd string `json:"reason_end"`
|
||||
MsPlayed int32 `json:"ms_played"`
|
||||
}
|
||||
|
||||
func ImportSpotifyFile(ctx context.Context, store db.DB, filename string) error {
|
||||
l := logger.FromContext(ctx)
|
||||
l.Info().Msgf("Beginning spotify import on file: %s", filename)
|
||||
file, err := os.Open(path.Join(cfg.ConfigDir(), "import", filename))
|
||||
if err != nil {
|
||||
l.Err(err).Msgf("Failed to read import file: %s", filename)
|
||||
return err
|
||||
}
|
||||
export := make([]SpotifyExportItem, 0)
|
||||
err = json.NewDecoder(file).Decode(&export)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, item := range export {
|
||||
if item.ReasonEnd != "trackdone" {
|
||||
continue
|
||||
}
|
||||
dur := item.MsPlayed
|
||||
if item.TrackName == "" || item.ArtistName == "" {
|
||||
l.Debug().Msg("Skipping non-track item")
|
||||
continue
|
||||
}
|
||||
opts := catalog.SubmitListenOpts{
|
||||
MbzCaller: &mbz.MusicBrainzClient{},
|
||||
Artist: item.ArtistName,
|
||||
TrackTitle: item.TrackName,
|
||||
ReleaseTitle: item.AlbumName,
|
||||
Duration: dur / 1000,
|
||||
Time: item.Timestamp,
|
||||
UserID: 1,
|
||||
}
|
||||
err = catalog.SubmitListen(ctx, store, opts)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to import spotify playback item")
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err = os.Stat(path.Join(cfg.ConfigDir(), "import_complete"))
|
||||
if err != nil {
|
||||
err = os.Mkdir(path.Join(cfg.ConfigDir(), "import_complete"), 0744)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to create import_complete dir! Import files must be removed from the import directory manually, or else the importer will run on every app start")
|
||||
}
|
||||
}
|
||||
err = os.Rename(path.Join(cfg.ConfigDir(), "import", filename), path.Join(cfg.ConfigDir(), "import_complete", filename))
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to move file to import_complete dir! Import files must be removed from the import directory manually, or else the importer will run on every app start")
|
||||
}
|
||||
l.Info().Msgf("Finished importing %s; imported %d items", filename, len(export))
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue