Compare commits

...

21 Commits

Author SHA1 Message Date
b8150a2f34 censor password on account creation and login 2026-02-06 18:44:06 -08:00
riwiwa
44d3ea1204 Update README 2026-02-05 03:27:39 -08:00
813f510a9e Added userID in history table for per profile history, reworked primary key. Profile recent history table 2026-02-05 03:16:05 -08:00
0043d83330 cleaned up project structure and optimized lastfm and spotify migration 2026-02-05 00:20:42 -08:00
4fa797d36a started working on user profiles 2026-01-29 20:20:36 -08:00
91c6bea0c6 improved error handling in importsongs/importsongs.go 2026-01-08 21:22:33 -08:00
ea1ac394d5 Merge branch 'main' of github.com:riwiwa/muzi
merging readme change from github
2026-01-08 19:21:05 -08:00
8c3bef6a43 lastfm import accepts dynamic track amount and now allows scrobbling while
importing
2026-01-08 19:18:19 -08:00
riwiwa
a8352c5bf9 Update README.md 2026-01-08 04:41:03 -08:00
679d7f9202 early working LastFM import functionality 2026-01-08 04:36:54 -08:00
5d8a480beb removed hardcoded local ip LOL 2025-12-20 05:54:11 -08:00
4cc7649772 fixed history bug where site didn't load if url query was blank 2025-12-20 05:49:04 -08:00
4cb8b94445 Cleaned up project structure more and simplified the app installation 2025-12-20 05:41:48 -08:00
ca767df960 Merge branch 'main' of github.com:riwiwa/muzi 2025-12-20 04:36:32 -08:00
9fd07b24f9 changed project structure, began web development 2025-12-20 04:34:39 -08:00
riwiwa
82960955c2 Update README.md 2025-12-11 22:31:32 -08:00
riwiwa
c48be0b8c0 Added dependecies and Spotify import instructions for developers/testers 2025-12-11 22:30:08 -08:00
f5d4da3527 remove c files 2025-12-11 22:20:58 -08:00
riwiwa
53966bbe2f Update README.md 2025-12-11 22:17:57 -08:00
riwiwa
8ccd37ffde Merge pull request #1 from riwiwa/go-port
Go porting finished
2025-12-11 22:17:15 -08:00
08fd4204b3 added gitignore 2025-11-30 01:29:30 -08:00
17 changed files with 1599 additions and 680 deletions

4
.gitignore vendored
View File

@@ -1,3 +1 @@
build
lastfm-data
spotify-data
imports

View File

@@ -1,25 +0,0 @@
CC=gcc
CFLAGS=-Wall -Wextra -Wpedantic -std=c11 -fdiagnostics-color=always -D_DEFAULT_SOURCE -g -I./include
LIBS=-Wl,--no-as-needed -lcjson -lzip -lpq
TARGET=muzi
SOURCES = muzi.c
OBJECTS = $(SOURCES:.c=.o)
OUTDIR=./build
OBJDIR=$(OUTDIR)/obj
$(shell mkdir -p $(OBJDIR))
%.o: %.c
$(CC) -c -o $(OBJDIR)/$@ $< $(CFLAGS)
$(TARGET): $(OBJECTS)
$(CC) -o $(OUTDIR)/$@ $(OBJDIR)/$(OBJECTS) $(CFLAGS) $(LIBS)
.PHONY: all
all: $(TARGET)
.DEFAULT_GOAL := all
clean:
rm -rf $(OUTDIR)

View File

@@ -1,16 +1,23 @@
# muzi (go port)
# Muzi
## Self-hosted music listening statistics
Self hosted music listening statistics
### Requirements
- Go 1.25.4+
- PostgreSQL
### plans:
- Ability to import all listening statistics and scrobbles from lastfm, spotify, apple music
- daily, weekly, monthly, yearly, lifetime presets for listening reports
- ability to specify a certain point in time from one datetime to another to list data
- grid maker (3x3-10x10)
- multi artist scrobbling
- ability to change artist image
- optional AI powered suggestions/recommendation algorithm
- webUI
- ability to "sync" scrobbles (send from a device to the server)
- live scrobbling to the server
- batch scrobble editor
### Roadmap:
- Ability to import all listening statistics and scrobbles from: \[In Progress\]
- LastFM \[Complete\]
- Spotify \[Complete\]
- Apple Music \[Planned\]
- WebUI \[In Progress\]
- Full listening history with time \[Functional\]
- Daily, weekly, monthly, yearly, lifetime presets for listening reports
- Ability to specify a certain point in time from one datetime to another to list data
- Grid maker (3x3-10x10)
- Ability to change artist image
- Multi artist scrobbling
- Ability to "sync" offline scrobbles (send from a device to the server)
- Live scrobbling to the server (Now playing)
- Batch scrobble editor

95
db/db.go Normal file
View File

@@ -0,0 +1,95 @@
package db
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5"
)
func TableExists(name string, conn *pgx.Conn) bool {
var exists bool
err := conn.QueryRow(
context.Background(),
`SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND
tablename = $1);`,
name,
).
Scan(&exists)
if err != nil {
fmt.Fprintf(os.Stderr, "SELECT EXISTS failed: %v\n", err)
return false
}
return exists
}
func DbExists() bool {
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
return false
}
defer conn.Close(context.Background())
return true
}
func CreateDB() error {
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to PostgreSQL: %v\n", err)
return err
}
defer conn.Close(context.Background())
_, err = conn.Exec(context.Background(), "CREATE DATABASE muzi")
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot create muzi database: %v\n", err)
return err
}
return nil
}
func CreateHistoryTable(conn *pgx.Conn) error {
_, err := conn.Exec(context.Background(),
`CREATE TABLE IF NOT EXISTS history (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
timestamp TIMESTAMPTZ NOT NULL,
song_name TEXT NOT NULL,
artist TEXT NOT NULL,
album_name TEXT,
ms_played INTEGER,
platform TEXT DEFAULT 'spotify',
UNIQUE (user_id, song_name, artist, timestamp)
);
CREATE INDEX IF NOT EXISTS idx_history_user_timestamp ON history(user_id, timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_history_user_artist ON history(user_id, artist);
CREATE INDEX IF NOT EXISTS idx_history_user_song ON history(user_id, song_name);`)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating history table: %v\n", err)
return err
}
return nil
}
func CreateUsersTable(conn *pgx.Conn) error {
_, err := conn.Exec(context.Background(),
`CREATE TABLE IF NOT EXISTS users (
username TEXT NOT NULL,
password TEXT NOT NULL,
bio TEXT DEFAULT 'This profile has no bio.',
pfp TEXT DEFAULT '/files/assets/default.png',
allow_duplicate_edits BOOLEAN DEFAULT FALSE,
pk SERIAL PRIMARY KEY
);`)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating users table: %v\n", err)
return err
}
return nil
}

10
go.mod
View File

@@ -3,11 +3,15 @@ module muzi
go 1.25.4
require (
github.com/go-chi/chi/v5 v5.2.3
github.com/jackc/pgtype v1.14.4
github.com/jackc/pgx/v5 v5.7.6
)
require (
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx v3.6.2+incompatible // indirect
github.com/jackc/pgx/v5 v5.7.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/crypto v0.45.0 // indirect
golang.org/x/text v0.31.0 // indirect
)

207
go.sum
View File

@@ -1,21 +1,220 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA=
github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE=
github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s=
github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o=
github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY=
github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI=
github.com/jackc/pgconn v1.14.3 h1:bVoTr12EGANZz66nZPkMInAV/KHD2TxH9npjXXgiB3w=
github.com/jackc/pgconn v1.14.3/go.mod h1:RZbme4uasqzybK2RK5c65VsHxoyaml09lx3tXOcO/VM=
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c=
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=
github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM=
github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM=
github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.3.3 h1:1HLSx5H+tXR9pW3in3zaztoEwQYRC9SQaYUHjTSUOag=
github.com/jackc/pgproto3/v2 v2.3.3/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o=
github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg=
github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc=
github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw=
github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM=
github.com/jackc/pgtype v1.14.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgtype v1.14.4 h1:fKuNiCumbKTAIxQwXfB/nsrnkEI6bPJrrSiMKgbJ2j8=
github.com/jackc/pgtype v1.14.4/go.mod h1:aKeozOde08iifGosdJpz9MBZonJOUJxqNpPBcMJTlVA=
github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y=
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
github.com/jackc/pgx/v4 v4.18.2 h1:xVpYkNR5pk5bMCZGfClbO962UIqVABcAGt7ha1s/FeU=
github.com/jackc/pgx/v4 v4.18.2/go.mod h1:Ey4Oru5tH5sB6tV7hDmfWFahwF15Eb7DNXlRKx2CkVw=
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle v1.3.0 h1:eHK/5clGOatcjX3oWGBO/MpxpbHzSwud5EWTSCI+MX0=
github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

113
main.go Normal file
View File

@@ -0,0 +1,113 @@
package main
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"muzi/db"
"muzi/migrate"
"muzi/web"
"github.com/jackc/pgx/v5"
)
func dbCheck() error {
if !db.DbExists() {
err := db.CreateDB()
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating muzi DB: %v\n", err)
return err
}
}
return nil
}
func dirCheck(path string) error {
_, err := os.Stat(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
os.MkdirAll(path, os.ModePerm)
} else {
fmt.Fprintf(os.Stderr, "Error checking dir: %s: %v\n", path, err)
return err
}
}
return nil
}
func main() {
dirImports := filepath.Join(".", "imports")
dirSpotify := filepath.Join(".", "imports", "spotify")
dirSpotifyZip := filepath.Join(".", "imports", "spotify", "zip")
dirSpotifyExt := filepath.Join(".", "imports", "spotify", "extracted")
fmt.Printf("Checking if directory %s exists...\n", dirImports)
err := dirCheck(dirImports)
if err != nil {
return
}
fmt.Printf("Checking if directory %s exists...\n", dirSpotify)
err = dirCheck(dirSpotify)
if err != nil {
return
}
fmt.Printf("Checking if directory %s exists...\n", dirSpotifyZip)
err = dirCheck(dirSpotifyZip)
if err != nil {
return
}
fmt.Printf("Checking if directory %s exists...\n", dirSpotifyExt)
err = dirCheck(dirSpotifyExt)
if err != nil {
return
}
fmt.Println("Checking if muzi database exists...")
err = dbCheck()
if err != nil {
return
}
fmt.Println("Setting up database tables...")
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
return
}
defer conn.Close(context.Background())
err = db.CreateHistoryTable(conn)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating history table: %v\n", err)
return
}
err = db.CreateUsersTable(conn)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating users table: %v\n", err)
return
}
username := ""
apiKey := ""
fmt.Printf("Importing LastFM data for %s\n", username)
// TODO:
// remove hardcoded userID by creating webUI import pages and getting
// userID from login session
err = migrate.ImportLastFM(username, apiKey, 1)
if err != nil {
return
}
err = migrate.ImportSpotify(1)
if err != nil {
return
}
web.Start()
}

229
migrate/lastfm.go Normal file
View File

@@ -0,0 +1,229 @@
package migrate
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/jackc/pgx/v5"
)
type LastFMTrack struct {
UserId int
Timestamp time.Time
SongName string
Artist string
Album string
}
type pageResult struct {
pageNum int
tracks []LastFMTrack
err error
}
type Response struct {
Recenttracks struct {
Track []struct {
Artist struct {
Text string `json:"#text"`
} `json:"artist"`
Album struct {
Text string `json:"#text"`
} `json:"album"`
Name string `json:"name"`
Attr struct {
Nowplaying string `json:"nowplaying"`
} `json:"@attr,omitempty"`
Date struct {
Uts string `json:"uts"`
} `json:"date"`
} `json:"track"`
Attr struct {
TotalPages string `json:"totalPages"`
} `json:"@attr"`
} `json:"recenttracks"`
}
func ImportLastFM(username string, apiKey string, userId int) error {
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
return err
}
defer conn.Close(context.Background())
totalImported := 0
resp, err := http.Get(
"https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" +
username + "&api_key=" + apiKey + "&format=json&limit=100",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting LastFM HTTP response: %v\n", err)
return err
}
var initialData Response
json.NewDecoder(resp.Body).Decode(&initialData)
totalPages, err := strconv.Atoi(initialData.Recenttracks.Attr.TotalPages)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing total pages: %v\n", err)
return err
}
fmt.Printf("Total pages: %d\n", totalPages)
trackBatch := make([]LastFMTrack, 0, 1000)
pageChan := make(chan pageResult, 20)
var wg sync.WaitGroup
wg.Add(10)
for worker := range 10 {
go func(workerID int) {
defer wg.Done()
for page := workerID + 1; page <= totalPages; page += 10 {
resp, err := http.Get(
"https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" +
username + "&api_key=" + apiKey + "&format=json&limit=100&page=" + strconv.Itoa(page),
)
if err != nil {
pageChan <- pageResult{pageNum: page, err: err}
continue
}
var data Response
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
resp.Body.Close()
pageChan <- pageResult{pageNum: page, err: err}
continue
}
resp.Body.Close()
var pageTracks []LastFMTrack
for j := range data.Recenttracks.Track {
if data.Recenttracks.Track[j].Attr.Nowplaying == "true" {
continue
}
unixTime, err := strconv.ParseInt(data.Recenttracks.Track[j].Date.Uts, 10, 64)
if err != nil {
continue
}
pageTracks = append(pageTracks, LastFMTrack{
UserId: userId,
Timestamp: time.Unix(unixTime, 0),
SongName: data.Recenttracks.Track[j].Name,
Artist: data.Recenttracks.Track[j].Artist.Text,
Album: data.Recenttracks.Track[j].Album.Text,
})
}
pageChan <- pageResult{pageNum: page, tracks: pageTracks, err: nil}
}
}(worker)
}
go func() {
wg.Wait()
close(pageChan)
}()
batchSize := 500
for result := range pageChan {
if result.err != nil {
fmt.Fprintf(os.Stderr, "Error on page %d: %v\n", result.pageNum, result.err)
continue
}
trackBatch = append(trackBatch, result.tracks...)
for len(trackBatch) >= batchSize {
batch := trackBatch[:batchSize]
trackBatch = trackBatch[batchSize:]
err := insertBatch(conn, batch, &totalImported, batchSize)
if err != nil {
fmt.Fprintf(os.Stderr, "Batch insert failed: %v\n", err)
}
}
fmt.Printf("Processed page %d/%d\n", result.pageNum, totalPages)
}
if len(trackBatch) > 0 {
err := insertBatch(conn, trackBatch, &totalImported, batchSize)
if err != nil {
fmt.Fprintf(os.Stderr, "Final batch insert failed: %v\n", err)
}
}
fmt.Printf("%d tracks imported from LastFM for user %s\n", totalImported, username)
return nil
}
func insertBatch(conn *pgx.Conn, tracks []LastFMTrack, totalImported *int, batchSize int) error {
tx, err := conn.Begin(context.Background())
if err != nil {
return err
}
var batchValues []string
var batchArgs []any
for i, track := range tracks {
batchValues = append(batchValues, fmt.Sprintf(
"($%d, $%d, $%d, $%d, $%d, $%d, $%d)",
len(batchArgs)+1,
len(batchArgs)+2,
len(batchArgs)+3,
len(batchArgs)+4,
len(batchArgs)+5,
len(batchArgs)+6,
len(batchArgs)+7,
))
// lastfm doesn't store playtime for each track, so set to 0
batchArgs = append(
batchArgs,
track.UserId,
track.Timestamp,
track.SongName,
track.Artist,
track.Album,
0,
"lastfm",
)
if len(batchValues) >= batchSize || i == len(tracks)-1 {
result, err := tx.Exec(
context.Background(),
`INSERT INTO history (user_id, timestamp, song_name, artist, album_name, ms_played, platform) VALUES `+
strings.Join(
batchValues,
", ",
)+` ON CONFLICT ON CONSTRAINT history_user_id_song_name_artist_timestamp_key DO NOTHING;`,
batchArgs...,
)
if err != nil {
tx.Rollback(context.Background())
return err
}
rowsAffected := result.RowsAffected()
if rowsAffected > 0 {
*totalImported += int(rowsAffected)
}
batchValues = batchValues[:0]
batchArgs = batchArgs[:0]
}
}
if err := tx.Commit(context.Background()); err != nil {
return err
}
return nil
}

361
migrate/spotify.go Normal file
View File

@@ -0,0 +1,361 @@
package migrate
import (
"archive/zip"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/jackc/pgx/v5"
)
type SpotifyTrack struct {
Timestamp string `json:"ts"`
Platform string `json:"-"`
Played int `json:"ms_played"`
Country string `json:"-"`
IP string `json:"-"`
Name string `json:"master_metadata_track_name"`
Artist string `json:"master_metadata_album_artist_name"`
Album string `json:"master_metadata_album_album_name"`
TrackURI string `json:"-"`
Episode string `json:"-"`
Show string `json:"-"`
EpisodeURI string `json:"-"`
Audiobook string `json:"-"`
AudiobookURI string `json:"-"`
AudiobookChapterURI string `json:"-"`
AudiobookChapter string `json:"-"`
ReasonStart string `json:"-"`
ReasonEnd string `json:"-"`
Shuffle bool `json:"-"`
Skipped bool `json:"-"`
Offline bool `json:"-"`
OfflineTimestamp int `json:"-"`
Incognito bool `json:"-"`
}
type existingTrack struct {
Timestamp time.Time
SongName string
Artist string
}
func getExistingTracks(conn *pgx.Conn, userId int, tracks []SpotifyTrack) (map[string]bool, error) {
if len(tracks) == 0 {
return map[string]bool{}, nil
}
// find min/max timestamps in this batch to create time window
var minTs, maxTs time.Time
for _, t := range tracks {
ts, err := time.Parse(time.RFC3339Nano, t.Timestamp)
if err != nil {
continue
}
if minTs.IsZero() || ts.Before(minTs) {
minTs = ts
}
if ts.After(maxTs) {
maxTs = ts
}
}
if minTs.IsZero() {
return map[string]bool{}, nil
}
// query only tracks within [min-20s, max+20s] window using timestamp index
rows, err := conn.Query(context.Background(),
`SELECT song_name, artist, timestamp
FROM history
WHERE user_id = $1
AND timestamp BETWEEN $2 AND $3`,
userId,
minTs.Add(-20*time.Second),
maxTs.Add(20*time.Second))
if err != nil {
return nil, err
}
defer rows.Close()
existing := make(map[string]bool)
var existingTracks []existingTrack
for rows.Next() {
var t existingTrack
if err := rows.Scan(&t.SongName, &t.Artist, &t.Timestamp); err != nil {
continue
}
existingTracks = append(existingTracks, t)
}
// check each incoming track against existing ones within 20 second window
for _, newTrack := range tracks {
newTs, err := time.Parse(time.RFC3339Nano, newTrack.Timestamp)
if err != nil {
continue
}
for _, existTrack := range existingTracks {
if newTrack.Name == existTrack.SongName && newTrack.Artist == existTrack.Artist {
diff := newTs.Sub(existTrack.Timestamp)
if diff < 0 {
diff = -diff
}
if diff < 20*time.Second {
key := fmt.Sprintf("%s|%s|%s", newTrack.Artist, newTrack.Name, newTrack.Timestamp)
existing[key] = true
break
}
}
}
}
return existing, nil
}
func JsonToDB(jsonFile string, userId int) error {
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
panic(err)
}
defer conn.Close(context.Background())
jsonData, err := os.ReadFile(jsonFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read %s: %v\n", jsonFile, err)
return err
}
var tracks []SpotifyTrack
err = json.Unmarshal(jsonData, &tracks)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot unmarshal %s: %v\n", jsonFile, err)
return err
}
totalImported := 0
batchSize := 1000
for batchStart := 0; batchStart < len(tracks); batchStart += batchSize {
batchEnd := batchStart + batchSize
if batchEnd > len(tracks) {
batchEnd = len(tracks)
}
var validTracks []SpotifyTrack
for i := batchStart; i < batchEnd; i++ {
if tracks[i].Played >= 20000 && tracks[i].Name != "" && tracks[i].Artist != "" {
validTracks = append(validTracks, tracks[i])
}
}
if len(validTracks) == 0 {
continue
}
existing, err := getExistingTracks(conn, userId, validTracks)
if err != nil {
fmt.Fprintf(os.Stderr, "Error checking existing tracks: %v\n", err)
continue
}
var batchValues []string
var batchArgs []any
for _, t := range validTracks {
key := fmt.Sprintf("%s|%s|%s", t.Artist, t.Name, t.Timestamp)
if existing[key] {
continue
}
batchValues = append(batchValues, fmt.Sprintf(
"($%d, $%d, $%d, $%d, $%d, $%d, $%d)",
len(batchArgs)+1,
len(batchArgs)+2,
len(batchArgs)+3,
len(batchArgs)+4,
len(batchArgs)+5,
len(batchArgs)+6,
len(batchArgs)+7,
))
batchArgs = append(
batchArgs,
userId,
t.Timestamp,
t.Name,
t.Artist,
t.Album,
t.Played,
"spotify",
)
}
if len(batchValues) == 0 {
continue
}
_, err = conn.Exec(
context.Background(),
`INSERT INTO history (user_id, timestamp, song_name, artist, album_name, ms_played, platform) VALUES `+
strings.Join(
batchValues,
", ",
)+
` ON CONFLICT ON CONSTRAINT history_user_id_song_name_artist_timestamp_key DO NOTHING;`,
batchArgs...,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Batch insert failed: %v\n", err)
} else {
totalImported += len(batchValues)
}
}
fmt.Printf("%d tracks imported from %s\n", totalImported, jsonFile)
return nil
}
func AddDirToDB(path string, userId int) error {
dirs, err := os.ReadDir(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while reading path: %s: %v\n", path, err)
return err
}
for _, dir := range dirs {
subPath := filepath.Join(
path,
dir.Name(),
"Spotify Extended Streaming History",
)
entries, err := os.ReadDir(subPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while reading path: %s: %v\n", subPath, err)
return err
}
for _, f := range entries {
jsonFileName := f.Name()
if !strings.Contains(jsonFileName, ".json") {
continue
}
if strings.Contains(jsonFileName, "Video") {
continue
}
jsonFilePath := filepath.Join(subPath, jsonFileName)
err = JsonToDB(jsonFilePath, userId)
if err != nil {
fmt.Fprintf(os.Stderr,
"Error adding json data (%s) to muzi database: %v", jsonFilePath, err)
return err
}
}
}
return nil
}
func ImportSpotify(userId int) error {
path := filepath.Join(".", "imports", "spotify", "zip")
targetBase := filepath.Join(".", "imports", "spotify", "extracted")
entries, err := os.ReadDir(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading path: %s: %v\n", path, err)
return err
}
for _, f := range entries {
_, err := zip.OpenReader(filepath.Join(path, f.Name()))
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening zip: %s: %v\n",
filepath.Join(path, f.Name()), err)
continue
}
fileName := f.Name()
fileFullPath := filepath.Join(path, fileName)
fileBaseName := fileName[:(strings.LastIndex(fileName, "."))]
targetDirFullPath := filepath.Join(targetBase, fileBaseName)
err = Extract(fileFullPath, targetDirFullPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error extracting %s to %s: %v\n",
fileFullPath, targetDirFullPath, err)
return err
}
}
err = AddDirToDB(targetBase, userId)
if err != nil {
fmt.Fprintf(os.Stderr,
"Error adding directory of data (%s) to muzi database: %v\n",
targetBase, err)
return err
}
return nil
}
func Extract(path string, target string) error {
archive, err := zip.OpenReader(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening zip: %s: %v\n", path, err)
return err
}
defer archive.Close()
zipDir := filepath.Base(path)
zipDir = zipDir[:(strings.LastIndex(zipDir, "."))]
for _, f := range archive.File {
filePath := filepath.Join(target, f.Name)
fmt.Println("extracting:", filePath)
if !strings.HasPrefix(
filePath,
filepath.Clean(target)+string(os.PathSeparator),
) {
err = fmt.Errorf("Invalid file path: %s", filePath)
fmt.Fprintf(os.Stderr, "%v\n", err)
return err
}
if f.FileInfo().IsDir() {
fmt.Println("Creating Directory", filePath)
os.MkdirAll(filePath, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
fmt.Fprintf(os.Stderr, "Error making directory: %s: %v\n",
filepath.Dir(filePath), err)
return err
}
fileToExtract, err := os.OpenFile(
filePath,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
f.Mode(),
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening file: %s: %v\n", filePath, err)
return err
}
extractedFile, err := f.Open()
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening file: %s: %v\n", f.Name, err)
return err
}
if _, err := io.Copy(fileToExtract, extractedFile); err != nil {
fmt.Fprintf(
os.Stderr,
"Error while copying file: %s to: %s: %v\n",
fileToExtract.Name(),
extractedFile,
err,
)
return err
}
fileToExtract.Close()
extractedFile.Close()
}
return nil
}

412
muzi.c
View File

@@ -1,412 +0,0 @@
#include <cjson/cJSON.h>
#include <dirent.h>
#include <errno.h>
#include <libpq-fe.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <zip.h>
#define MAX_FILENAME_SIZE 255
enum Platform {
SPOTIFY = 0,
LASTFM = 1,
};
int extract(const char *path, const char *target);
int get_artist_plays(const char *json_file, const char *artist);
int import_spotify(void);
int add_dir_to_db(const char *path, int platform);
int json_to_db(const char *json_file, int platform);
int create_db(void);
bool db_exists(void);
bool table_exists(const char *name, PGconn *conn);
int create_table(const char *name, PGconn *conn);
bool table_exists(const char *name, PGconn *conn) {
PGresult *result =
PQexecParams(conn,
"SELECT EXISTS (SELECT 1 FROM pg_tables "
"WHERE schemaname = 'public' AND tablename = $1);",
1, NULL, &name, NULL, NULL, 0);
if (PQresultStatus(result) != PGRES_TUPLES_OK) {
printf("SELECT EXISTS failed: %s\n", PQerrorMessage(conn));
PQclear(result);
return false;
}
bool exists = strcmp(PQgetvalue(result, 0, 0), "t") == 0;
PQclear(result);
return exists;
}
bool db_exists(void) {
PGconn *tmp_conn =
PQconnectdb("host=localhost port=5432 dbname=postgres user=postgres "
"password=postgres");
if (PQstatus(tmp_conn) != CONNECTION_OK) {
printf("Temporary database connection failed: %s\n",
PQerrorMessage(tmp_conn));
PQfinish(tmp_conn);
return false;
}
PGresult *result =
PQexec(tmp_conn, "SELECT 1 FROM pg_database WHERE datname = 'muzi'");
if (PQresultStatus(result) != PGRES_TUPLES_OK) {
printf("SELECT command failed: %s\n", PQerrorMessage(tmp_conn));
PQclear(result);
PQfinish(tmp_conn);
return false;
}
if (PQntuples(result) > 0) {
return true;
} else {
return false;
}
}
int create_db(void) {
PGconn *tmp_conn =
PQconnectdb("host=localhost port=5432 dbname=postgres user=postgres "
"password=postgres");
if (PQstatus(tmp_conn) != CONNECTION_OK) {
printf("Temporary database connection failed: %s\n",
PQerrorMessage(tmp_conn));
PQfinish(tmp_conn);
return EXIT_FAILURE;
}
if (PQresultStatus(PQexec(tmp_conn, "CREATE DATABASE muzi")) !=
PGRES_COMMAND_OK) {
printf("CREATE DATABASE muzi failed: %s\n", PQerrorMessage(tmp_conn));
PQfinish(tmp_conn);
return EXIT_FAILURE;
}
PQfinish(tmp_conn);
printf("muzi database created successfully.\n");
return EXIT_SUCCESS;
}
int json_to_db(const char *json_file, int platform) {
if (db_exists() == false) {
create_db();
}
PGconn *conn =
PQconnectdb("host=localhost port=5432 dbname=muzi user=postgres "
"password=postgres");
if (PQstatus(conn) != CONNECTION_OK) {
printf("Temporary database connection failed: %s\n", PQerrorMessage(conn));
PQfinish(conn);
return EXIT_FAILURE;
}
bool e = table_exists("history", conn);
if (!e) {
PGresult *result =
PQexec(conn, "CREATE TABLE history ( ms_played INTEGER, timestamp "
"TIMESTAMPTZ, song_name "
"TEXT, artist TEXT, "
"album_name TEXT, PRIMARY KEY (timestamp, ms_played, "
"artist, song_name));");
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
printf("History table creation failed: %s\n", PQerrorMessage(conn));
PQclear(result);
PQfinish(conn);
return EXIT_FAILURE;
}
printf("Created history table.\n");
PQclear(result);
}
FILE *fp = fopen(json_file, "r");
if (fp == NULL) {
printf("Error while opening file\n");
return 1;
}
fseek(fp, 0, SEEK_END);
long fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buffer = (char *)malloc(fileSize + 1);
fread(buffer, 1, fileSize, fp);
buffer[fileSize] = '\0';
fclose(fp);
cJSON *json = cJSON_Parse(buffer);
if (json == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
printf("Error: %s\n", error_ptr);
}
cJSON_Delete(json);
free(buffer);
return EXIT_FAILURE;
}
if (platform == SPOTIFY) {
cJSON *play = NULL;
cJSON_ArrayForEach(play, json) {
// supports up to 2.7k hour long songs
char ms_played[10];
char *timestamp;
char *song_name;
char *album_artist;
char *album;
cJSON *ms_played_obj =
cJSON_GetObjectItemCaseSensitive(play, "ms_played");
if (cJSON_IsNumber(ms_played_obj)) {
sprintf(ms_played, "%d", ms_played_obj->valueint);
}
// do not add to database if played for only 20 seconds
if (ms_played_obj->valueint < 20000) {
continue;
}
cJSON *timestamp_obj = cJSON_GetObjectItemCaseSensitive(play, "ts");
if (cJSON_IsString(timestamp_obj)) {
timestamp = timestamp_obj->valuestring;
}
cJSON *song_name_obj =
cJSON_GetObjectItemCaseSensitive(play, "master_metadata_track_name");
if (cJSON_IsString(song_name_obj)) {
song_name = song_name_obj->valuestring;
}
cJSON *artist_obj = cJSON_GetObjectItemCaseSensitive(
play, "master_metadata_album_artist_name");
if (cJSON_IsString(artist_obj)) {
album_artist = artist_obj->valuestring;
}
cJSON *album_obj = cJSON_GetObjectItemCaseSensitive(
play, "master_metadata_album_album_name");
if (cJSON_IsString(album_obj)) {
album = album_obj->valuestring;
}
const char *data[5] = {timestamp, song_name, album_artist, album,
ms_played};
PGresult *result =
PQexecParams(conn,
"INSERT INTO history (timestamp, song_name, artist, "
"album_name, ms_played) VALUES ($1, $2, $3, $4, $5);",
5, NULL, data, NULL, NULL, 0);
if (PQresultStatus(result) != PGRES_COMMAND_OK) {
printf("Attempt to insert data for track failed: %s\n",
PQerrorMessage(conn));
}
PQclear(result);
}
}
PQfinish(conn);
cJSON_Delete(json);
free(buffer);
printf("Added file: '%s' to muzi database.\n", json_file);
return EXIT_SUCCESS;
}
int add_dir_to_db(const char *path, int platform) {
DIR *dir = opendir(path);
struct dirent *data_dir = NULL;
while ((data_dir = readdir(dir)) != NULL) {
if (data_dir->d_type == DT_DIR) {
if ((strcmp(data_dir->d_name, ".") != 0) &&
(strcmp(data_dir->d_name, "..") != 0)) {
char data_dir_path[MAX_FILENAME_SIZE];
if (snprintf(data_dir_path, MAX_FILENAME_SIZE, "%s/%s/%s", path,
data_dir->d_name,
"Spotify Extended Streaming History") < 0) {
return EXIT_FAILURE;
}
DIR *json_dir = opendir(data_dir_path);
struct dirent *json_file = NULL;
while ((json_file = readdir(json_dir)) != NULL) {
if (json_file->d_type != DT_DIR) {
char *ext = strrchr(json_file->d_name, '.');
if (strcmp(ext, ".json") == 0) {
// prevents parsing spotify video data that causes duplicates
if (platform == SPOTIFY && strstr(json_file->d_name, "Video")) {
continue;
}
char json_file_path[MAX_FILENAME_SIZE];
if (snprintf(json_file_path, MAX_FILENAME_SIZE, "%s/%s",
data_dir_path, json_file->d_name) < 0) {
return EXIT_FAILURE;
}
json_to_db(json_file_path, platform);
}
}
}
}
}
}
return EXIT_SUCCESS;
}
int get_artist_plays(const char *json_file, const char *artist) {
FILE *fp = fopen(json_file, "r");
if (fp == NULL) {
printf("Error while opening file\n");
return 1;
}
fseek(fp, 0, SEEK_END);
long fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buffer = (char *)malloc(fileSize + 1);
fread(buffer, 1, fileSize, fp);
buffer[fileSize] = '\0';
fclose(fp);
cJSON *json = cJSON_Parse(buffer);
if (json == NULL) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
printf("Error: %s\n", error_ptr);
}
cJSON_Delete(json);
free(buffer);
return EXIT_FAILURE;
}
cJSON *track = NULL;
int i = 0;
cJSON_ArrayForEach(track, json) {
cJSON *trackName = cJSON_GetObjectItemCaseSensitive(
track, "master_metadata_album_artist_name");
if (cJSON_IsString(trackName)) {
if (strcasecmp(artist, (trackName->valuestring)) == 0) {
i++;
}
}
}
printf("\"%s\" count: %d\n", artist, i);
cJSON_Delete(json);
free(buffer);
return EXIT_SUCCESS;
}
int extract(const char *path, const char *target) {
mkdir(target, 0777);
zip_t *za;
int err;
if ((za = zip_open(path, 0, &err)) == NULL) {
zip_error_t error;
zip_error_init_with_code(&error, err);
fprintf(stderr, "Error opening zip archive: %s\n",
zip_error_strerror(&error));
zip_error_fini(&error);
return EXIT_FAILURE;
}
int archived_files = zip_get_num_entries(za, ZIP_FL_UNCHANGED);
for (int i = 0; i < archived_files; i++) {
struct zip_stat st;
if (zip_stat_index(za, i, 0, &st) < 0) {
fprintf(stderr, "Error getting file info for index %d\n", i);
continue;
}
char file_target[MAX_FILENAME_SIZE];
if (snprintf(file_target, MAX_FILENAME_SIZE, "%s/%s", target, st.name) <
0) {
return EXIT_FAILURE;
}
char *search = strchr(st.name, '/');
if (search != NULL) {
int index = search - st.name;
int end = 0;
char dir[strlen(st.name)];
for (int j = 0; j < index; j++) {
dir[j] = st.name[j];
end = j;
}
dir[end + 1] = '\0';
char dir_target[MAX_FILENAME_SIZE];
if (snprintf(dir_target, MAX_FILENAME_SIZE, "%s/%s", target, dir) < 0) {
return EXIT_FAILURE;
}
mkdir(dir_target, 0777);
}
if (file_target[strlen(file_target) - 1] == '/') {
mkdir(file_target, 0777);
continue;
}
zip_file_t *zf = zip_fopen_index(za, i, 0);
if (!zf) {
fprintf(stderr, "Error opening file in zip: %s\n", file_target);
continue;
}
FILE *outfile = fopen(file_target, "w+");
if (!outfile) {
fprintf(stderr, "Error creating output file: %s\n", file_target);
zip_fclose(zf);
continue;
}
char buffer[4096];
zip_int64_t bytes_read;
while ((bytes_read = zip_fread(zf, buffer, sizeof(buffer))) > 0) {
fwrite(buffer, 1, bytes_read, outfile);
}
fclose(outfile);
zip_fclose(zf);
}
return EXIT_SUCCESS;
}
int import_spotify(void) {
const char *path = "./spotify-data/zip";
const char *target_base = "./spotify-data/extracted";
DIR *dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "Error opening directory: %s (%d)\n", path, errno);
return errno;
}
struct dirent *entry = NULL;
while ((entry = readdir(dir)) != NULL) {
char full_name[MAX_FILENAME_SIZE];
if (snprintf(full_name, MAX_FILENAME_SIZE, "%s/%s", path, entry->d_name) <
0) {
return EXIT_FAILURE;
}
if (entry->d_type != DT_DIR) {
char *ext = strrchr(entry->d_name, '.');
if (strcmp(ext, ".zip") == 0) {
char *zip_dir_name = entry->d_name;
int len = strlen(zip_dir_name);
zip_dir_name[len - 4] = '\0';
char target[MAX_FILENAME_SIZE];
if (snprintf(target, MAX_FILENAME_SIZE, "%s/%s", target_base,
zip_dir_name) < 0) {
return EXIT_FAILURE;
}
extract(full_name, target);
}
}
}
closedir(dir);
add_dir_to_db(target_base, SPOTIFY);
return EXIT_SUCCESS;
}
int main(void) {
import_spotify();
return EXIT_SUCCESS;
}

219
muzi.go
View File

@@ -1,219 +0,0 @@
package main
import (
"archive/zip"
"context"
"encoding/json"
"fmt"
"github.com/jackc/pgx/v5"
"io"
"os"
"path/filepath"
"strings"
)
const (
spotify = iota
lastfm
apple
)
func tableExists(name string, conn *pgx.Conn) bool {
var exists bool
err := conn.QueryRow(context.Background(), "SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = $1);", name).Scan(&exists)
if err != nil {
fmt.Fprintf(os.Stderr, "SELECT EXISTS failed: %v\n", err)
return false
}
return exists
}
func dbExists() bool {
conn, err := pgx.Connect(context.Background(), "postgres://postgres:postgres@localhost:5432/muzi")
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
return false
}
defer conn.Close(context.Background())
return true
}
func createDB() error {
conn, err := pgx.Connect(context.Background(), "postgres://postgres:postgres@localhost:5432")
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to PostgreSQL: %v\n", err)
return err
}
defer conn.Close(context.Background())
_, err = conn.Exec(context.Background(), "CREATE DATABASE muzi")
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot create muzi database: %v\n", err)
return err
}
return nil
}
func jsonToDB(jsonFile string, platform int) {
if !dbExists() {
err := createDB()
if err != nil {
panic(err)
}
}
conn, err := pgx.Connect(context.Background(), "postgres://postgres:postgres@localhost:5432/muzi")
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
panic(err)
}
defer conn.Close(context.Background())
if !tableExists("history", conn) {
_, err = conn.Exec(context.Background(), "CREATE TABLE history ( ms_played INTEGER, timestamp TIMESTAMPTZ, song_name TEXT, artist TEXT, album_name TEXT, PRIMARY KEY (timestamp, ms_played, artist, song_name));")
}
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot create history table: %v\n", err)
panic(err)
}
jsonData, err := os.ReadFile(jsonFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read %s: %v\n", jsonFile, err)
panic(err)
}
if platform == spotify {
type Track struct {
Timestamp string `json:"ts"`
//Platform string `json:"platform"`
Played int `json:"ms_played"`
//Country string `json:"conn_country"`
//IP string `json:"ip_addr"`
Name string `json:"master_metadata_track_name"`
Artist string `json:"master_metadata_album_artist_name"`
Album string `json:"master_metadata_album_album_name"`
//TrackURI string `json:"spotify_track_uri"`
//Episode string `json:"episode_name"`
//Show string `json:"episode_show_name"`
//EpisodeURI string `json:"spotify_episode_uri"`
//Audiobook string `json:"audiobook_title"`
//AudiobookURI string `json:"audiobook_uri"`
//AudiobookChapterURI string `json:"audiobook_chapter_uri"`
//AudiobookChapter string `json:"audiobook_chapter_title"`
//ReasonStart string `json:"reason_start"`
//ReasonEnd string `json:"reason_end"`
//Shuffle bool `json:"shuffle"`
//Skipped bool `json:"skipped"`
//Offline bool `json:"offline"`
//OfflineTimestamp int `json:"offline_timestamp"`
//Incognito bool `json:"incognito_mode"`
}
var tracks []Track
err := json.Unmarshal(jsonData, &tracks)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot unmarshal %s: %v\n", jsonFile, err)
panic(err)
}
for _, track := range tracks {
// skip adding a song if it was only listed to for less than 20 seconds
if track.Played < 20000 {
continue
}
_, err = conn.Exec(context.Background(), "INSERT INTO history (timestamp, song_name, artist, album_name, ms_played) VALUES ($1, $2, $3, $4, $5);", track.Timestamp, track.Name, track.Artist, track.Album, track.Played)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't add track to muzi DB (%s): %v\n", (track.Artist + " - " + track.Name), err)
}
}
}
}
func addDirToDB(path string, platform int) {
dirs, err := os.ReadDir(path)
if err != nil {
panic(err)
}
for _, dir := range dirs {
subPath := filepath.Join(path, dir.Name(), "Spotify Extended Streaming History")
entries, err := os.ReadDir(subPath)
if err != nil {
panic(err)
}
for _, f := range entries {
jsonFileName := f.Name()
if platform == spotify {
if !strings.Contains(jsonFileName, ".json") {
continue
}
// prevents parsing spotify video data that causes duplicates
if strings.Contains(jsonFileName, "Video") {
continue
}
}
jsonFilePath := filepath.Join(subPath, jsonFileName)
jsonToDB(jsonFilePath, platform)
}
}
}
func importSpotify() {
path := filepath.Join(".", "spotify-data", "zip")
targetBase := filepath.Join(".", "spotify-data", "extracted")
entries, err := os.ReadDir(path)
if err != nil {
panic(err)
}
for _, f := range entries {
_, err := zip.OpenReader(filepath.Join(path, f.Name()))
if err == nil {
fileName := f.Name()
fileFullPath := filepath.Join(path, fileName)
fileBaseName := fileName[:(strings.LastIndex(fileName, "."))]
targetDirFullPath := filepath.Join(targetBase, fileBaseName)
extract(fileFullPath, targetDirFullPath)
}
}
addDirToDB(targetBase, spotify)
}
func extract(path string, target string) {
archive, err := zip.OpenReader(path)
if err != nil {
panic(err)
}
defer archive.Close()
zipDir := filepath.Base(path)
zipDir = zipDir[:(strings.LastIndex(zipDir, "."))]
for _, f := range archive.File {
filePath := filepath.Join(target, f.Name)
fmt.Println("extracting:", filePath)
if !strings.HasPrefix(filePath, filepath.Clean(target)+string(os.PathSeparator)) {
fmt.Println("Invalid file path")
return
}
if f.FileInfo().IsDir() {
fmt.Println("Creating Directory", filePath)
os.MkdirAll(filePath, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
panic(err)
}
fileToExtract, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
panic(err)
}
extractedFile, err := f.Open()
if err != nil {
panic(err)
}
if _, err := io.Copy(fileToExtract, extractedFile); err != nil {
panic(err)
}
fileToExtract.Close()
extractedFile.Close()
}
}
func main() {
importSpotify()
}

BIN
static/assets/default.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

92
static/style.css Normal file
View File

@@ -0,0 +1,92 @@
body {
display: flex;
flex-direction: column;
background-color: #222;
color: #AFA;
align-content: center;
justify-content: center;
align-items: center;
text-align: center;
max-width: 70vw;
margin: 0;
width: 70vw;
font-family: sans-serif;
}
.page_buttons {
display: flex;
flex-direction: row;
justify-content: center;
a {
margin: 10px;
background-color: #111;
color: #EEE;
text-decoration: none;
padding: 5px;
}
}
.user-stats-top {
display: inline-block;
}
.username-bio {
display: flex;
flex-direction: column;
justify-content: center;
margin-left: 40px;
}
.profile-top {
display: flex;
flex-direction: row;
align-content: center;
h1 {
color: #FFFFFF;
margin: 0;
}
h2 {
color: #777777;
font-size: 15px;
margin: 0;
}
h3 {
color: #AAAAAA;
font-size: 25px;
margin: 0;
}
img {
object-fit: cover;
width: 250px;
height: 250px;
border-radius: 100%;
}
}
.login-error {
color: #AA0000;
}
.history {
display: flex;
justify-content: center;
width: 100vw;
table {
width: 90%;
}
tr {
display: flex;
flex-direction: row;
justify-content: space-between;
margin: 5px;
}
th, td {
display: flex;
justify-content: center;
margin: 5px;
width: 30%;
}
tr:nth-child(even) {
background-color: #111;
}
}

View File

@@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/files/style.css" type="text/css">
<title>
muzi | Create Account
</title>
</head>
<body>
<div class=login-form>
<form action="/createaccountsubmit" method="POST">
<label for="uname">Username:</label>
<input type="text" id="uname" name="uname"> <br> <br>
<label for="pass">Password:</label>
<input type="password" id="pass" name="pass"> <br> <br>
<input type="submit" value="Create Account">
</form>
</div>
</body>
</html>

25
templates/login.gohtml Normal file
View File

@@ -0,0 +1,25 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/files/style.css" type="text/css">
<title>
muzi | Login
</title>
</head>
<body>
<div class=login-form>
<form action="/loginsubmit" method="POST">
<label for="uname">Username:</label>
<input type="text" id="uname" name="uname"> <br> <br>
<label for="pass">Password:</label>
<input type="password" id="pass" name="pass"> <br> <br>
<input type="submit" value="Login">
{{if .ShowError}}
<div class="login-error">
Invalid credentials. Please try again.
</div>
{{end}}
</form>
</div>
</body>
</html>

45
templates/profile.gohtml Normal file
View File

@@ -0,0 +1,45 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/files/style.css" type="text/css">
<title>
muzi | {{.Username}}'s Profile
</title>
</head>
<body>
<div class="profile-top">
<img src="{{.Pfp}}" alt="{{.Username}}'s avatar">
<div class="username-bio">
<h1>{{.Username}}</h1>
<h2>{{.Bio}}</h2>
</div>
<div class="user-stats-top">
<h3>{{.ScrobbleCount}} Listens</h3>
<h3>{{.ArtistCount}} Artists</h3>
</div>
</div>
<div class="history">
<h3>Listening History</h3>
<table>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Timestamp</th>
</tr>
{{$artists := .Artists}}
{{$times := .Times}}
{{range $index, $title := .Titles}}
<tr>
<td>{{index $artists $index}}</td>
<td>{{$title}}</td>
<td>{{index $times $index}}</td>
</tr>
{{end}}
</table>
</div>
<div class="page_buttons">
<a href="/profile/{{.Username}}?page={{Sub .Page 1}}">Prev Page</a>
<a href="/profile/{{.Username}}?page={{Add .Page 1}}">Next Page</a>
</div>
</body>
</html>

387
web/web.go Normal file
View File

@@ -0,0 +1,387 @@
package web
import (
"context"
"fmt"
"html/template"
"net/http"
"os"
"strconv"
"muzi/db"
"golang.org/x/crypto/bcrypt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v5"
)
type ProfileData struct {
Username string
Bio string
Pfp string
AllowDuplicateEdits bool
ScrobbleCount int
ArtistCount int
Artists []string
Titles []string
Times []string
Page int
}
func Sub(a int, b int) int {
return a - b
}
func Add(a int, b int) int {
return a + b
}
func getUserIdByUsername(conn *pgx.Conn, username string) (int, error) {
var userId int
err := conn.QueryRow(context.Background(), "SELECT pk FROM users WHERE username = $1;", username).
Scan(&userId)
return userId, err
}
func getTimes(conn *pgx.Conn, userId int, lim int, off int) []string {
var times []string
rows, err := conn.Query(
context.Background(),
"SELECT timestamp FROM history WHERE user_id = $1 ORDER BY timestamp DESC LIMIT $2 OFFSET $3;",
userId,
lim,
off,
)
if err != nil {
fmt.Fprintf(os.Stderr, "SELECT timestamp failed: %v\n", err)
return nil
}
for rows.Next() {
var time pgtype.Timestamptz
err = rows.Scan(&time)
if err != nil {
fmt.Fprintf(os.Stderr, "Scanning time failed: %v\n", err)
return nil
}
times = append(times, time.Time.String())
}
return times
}
func getTitles(conn *pgx.Conn, userId int, lim int, off int) []string {
var titles []string
rows, err := conn.Query(
context.Background(),
"SELECT song_name FROM history WHERE user_id = $1 ORDER BY timestamp DESC LIMIT $2 OFFSET $3;",
userId,
lim,
off,
)
if err != nil {
fmt.Fprintf(os.Stderr, "SELECT song_name failed: %v\n", err)
return nil
}
for rows.Next() {
var title string
err = rows.Scan(&title)
if err != nil {
fmt.Fprintf(os.Stderr, "Scanning title failed: %v\n", err)
return nil
}
titles = append(titles, title)
}
return titles
}
func getArtists(conn *pgx.Conn, userId int, lim int, off int) []string {
var artists []string
rows, err := conn.Query(
context.Background(),
"SELECT artist FROM history WHERE user_id = $1 ORDER BY timestamp DESC LIMIT $2 OFFSET $3;",
userId,
lim,
off,
)
if err != nil {
fmt.Fprintf(os.Stderr, "SELECT artist failed: %v\n", err)
return nil
}
for rows.Next() {
var artist string
err = rows.Scan(&artist)
if err != nil {
fmt.Fprintf(os.Stderr, "Scanning artist name failed: %v\n", err)
return nil
}
artists = append(artists, artist)
}
return artists
}
func getScrobbles(conn *pgx.Conn, userId int) int {
var count int
err := conn.QueryRow(context.Background(), "SELECT COUNT(*) FROM history WHERE user_id = $1;", userId).
Scan(&count)
if err != nil {
fmt.Fprintf(os.Stderr, "SELECT COUNT failed: %v\n", err)
return 0
}
return count
}
func getArtistCount(conn *pgx.Conn, userId int) int {
var count int
err := conn.QueryRow(context.Background(), "SELECT COUNT(DISTINCT artist) FROM history WHERE user_id = $1;", userId).
Scan(&count)
if err != nil {
fmt.Fprintf(os.Stderr, "SELECT artist count failed: %v\n", err)
return 0
}
return count
}
func hashPassword(pass []byte) string {
hashedPassword, err := bcrypt.GenerateFromPassword(pass, bcrypt.DefaultCost)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't hash password: %v\n", err)
}
return string(hashedPassword)
}
func verifyPassword(hashedPassword string, enteredPassword []byte) bool {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), enteredPassword)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while comparing passwords: %v\n", err)
return false
}
return true
}
func createAccount(w http.ResponseWriter, r *http.Request) {
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
return
}
defer conn.Close(context.Background())
if r.Method == "POST" {
r.ParseForm()
username := r.FormValue("uname")
hashedPassword := hashPassword([]byte(r.FormValue("pass")))
err = db.CreateUsersTable(conn)
if err != nil {
fmt.Fprintf(os.Stderr, "Error ensuring users table exists: %v\n", err)
http.Redirect(w, r, "/createaccount", http.StatusSeeOther)
return
}
_, err = conn.Exec(
context.Background(),
`INSERT INTO users (username, password) VALUES ($1, $2);`,
username,
hashedPassword,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot add new user to users table: %v\n", err)
http.Redirect(w, r, "/createaccount", http.StatusSeeOther)
} else {
http.Redirect(w, r, "/profile/"+username, http.StatusSeeOther)
}
}
}
func createAccountPageHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tmp, err := template.New("create_account.gohtml").
ParseFiles("./templates/create_account.gohtml")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmp.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func loginSubmit(w http.ResponseWriter, r *http.Request) {
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
return
}
defer conn.Close(context.Background())
if r.Method == "POST" {
r.ParseForm()
username := r.FormValue("uname")
password := r.FormValue("pass")
var storedPassword string
err := conn.QueryRow(context.Background(), "SELECT password FROM users WHERE username = $1;", username).
Scan(&storedPassword)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot get password for entered username: %v\n", err)
}
if verifyPassword(storedPassword, []byte(password)) {
http.Redirect(w, r, "/profile/"+username, http.StatusSeeOther)
} else {
http.Redirect(w, r, "/login?error=1", http.StatusSeeOther)
}
}
}
func loginPageHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type data struct {
ShowError bool
}
d := data{ShowError: false}
if r.URL.Query().Get("error") != "" {
d.ShowError = true
}
tmp, err := template.New("login.gohtml").ParseFiles("./templates/login.gohtml")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmp.Execute(w, d)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
func profilePageHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
username := chi.URLParam(r, "username")
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close(context.Background())
userId, err := getUserIdByUsername(conn, username)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot find user %s: %v\n", username, err)
http.Error(w, "User not found", http.StatusNotFound)
return
}
pageStr := r.URL.Query().Get("page")
var pageInt int
if pageStr == "" {
pageInt = 1
} else {
pageInt, err = strconv.Atoi(pageStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot convert page URL query from string to int: %v\n", err)
pageInt = 1
}
}
lim := 15
off := (pageInt - 1) * lim
var profileData ProfileData
err = conn.QueryRow(
context.Background(),
"SELECT bio, pfp, allow_duplicate_edits FROM users WHERE pk = $1;",
userId,
).Scan(&profileData.Bio, &profileData.Pfp, &profileData.AllowDuplicateEdits)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot get profile for %s: %v\n", username, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
profileData.Username = username
profileData.ScrobbleCount = getScrobbles(conn, userId)
profileData.ArtistCount = getArtistCount(conn, userId)
profileData.Artists = getArtists(conn, userId, lim, off)
profileData.Titles = getTitles(conn, userId, lim, off)
profileData.Times = getTimes(conn, userId, lim, off)
profileData.Page = pageInt
funcMap := template.FuncMap{
"Sub": Sub,
"Add": Add,
}
tmp, err := template.New("profile.gohtml").
Funcs(funcMap).
ParseFiles("./templates/profile.gohtml")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmp.Execute(w, profileData)
}
}
func updateDuplicateEditsSetting(w http.ResponseWriter, r *http.Request) {
conn, err := pgx.Connect(
context.Background(),
"postgres://postgres:postgres@localhost:5432/muzi",
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
return
}
defer conn.Close(context.Background())
if r.Method == "POST" {
r.ParseForm()
username := r.FormValue("username")
allow := r.FormValue("allow") == "true"
_, err = conn.Exec(
context.Background(),
`UPDATE users SET allow_duplicate_edits = $1 WHERE username = $2;`,
allow,
username,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error updating setting: %v\n", err)
}
http.Redirect(w, r, "/profile/"+username, http.StatusSeeOther)
}
}
func Start() {
addr := ":1234"
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Handle("/files/*", http.StripPrefix("/files", http.FileServer(http.Dir("./static"))))
r.Get("/login", loginPageHandler())
r.Get("/createaccount", createAccountPageHandler())
r.Get("/profile/{username}", profilePageHandler())
r.Post("/loginsubmit", loginSubmit)
r.Post("/createaccountsubmit", createAccount)
r.Post("/settings/duplicate-edits", updateDuplicateEditsSetting)
fmt.Printf("WebUI starting on %s\n", addr)
http.ListenAndServe(addr, r)
}