11import { createClient } from "https://esm.sh/@supabase/supabase-js@2" ;
22import { getRecentTracks } from "./lastfm.ts" ;
3- import { Row , TableListened } from "./table.ts" ;
43import { Variables } from "./env.ts" ;
4+ import { TableHooman } from "./db.hooman.ts" ;
5+ import { ListenedRow , TableListened } from "./db.listened.ts" ;
56
6- export async function scrobbles ( env : Variables ) : Promise < string > {
7+ /**
8+ * Sync data from Last.fm to Supabase Database.
9+ */
10+ export async function scrobbles (
11+ env : Variables ,
12+ lastFmUser : string | null = null ,
13+ ) : Promise < string > {
714 const size = 50 ;
815 const supabaseClient = createClient ( env . SUPABASE_URL , env . SUPABASE_ANON_KEY ) ;
9- const table = new TableListened ( supabaseClient ) ;
10- const startFrom : number | null = await table . getLastListenedDate ( ) ;
16+ const listened = new TableListened ( supabaseClient ) ;
17+ const hooman = new TableHooman ( supabaseClient ) ;
18+ const startFrom : number | null = await listened . getLastListenedDate ( ) ;
19+
1120 let totalPages = 1 ;
1221 let total = 0 ;
1322 let page = 1 ;
@@ -21,9 +30,14 @@ export async function scrobbles(env: Variables): Promise<string> {
2130 console . log ( "Database is empty, starting from the last page" ) ;
2231 }
2332
33+ const lastFmUserToUse = lastFmUser ? lastFmUser : env . LASTFM_USERNAME ;
34+ console . log ( `Last.fm user: ${ lastFmUserToUse } ` ) ;
35+
36+ const hoomanId = await hooman . findOrCreateByLastFmUser ( lastFmUserToUse ) ;
37+
2438 const fmInitial = await getRecentTracks (
2539 env . LASTFM_API_KEY ,
26- env . LASTFM_USERNAME ,
40+ lastFmUserToUse ,
2741 1 ,
2842 size ,
2943 startFrom ,
@@ -37,8 +51,8 @@ export async function scrobbles(env: Variables): Promise<string> {
3751 page = totalPages ;
3852
3953 if ( total === 0 ) {
40- console . log ( ' Nothing new to save.' ) ;
41- return 'ok' ;
54+ console . log ( " Nothing new to save." ) ;
55+ return "ok" ;
4256 }
4357
4458 if ( startFrom ) {
@@ -64,7 +78,7 @@ export async function scrobbles(env: Variables): Promise<string> {
6478
6579 const fm = await getRecentTracks (
6680 env . LASTFM_API_KEY ,
67- env . LASTFM_USERNAME ,
81+ lastFmUserToUse ,
6882 page ,
6983 size ,
7084 startFrom ,
@@ -82,7 +96,7 @@ export async function scrobbles(env: Variables): Promise<string> {
8296
8397 console . log ( `Fetching page ${ page } /${ totalPages } ` ) ;
8498
85- const toInsert : Row [ ] = tracks
99+ const toInsert : ListenedRow [ ] = tracks
86100 . filter ( ( track ) => ! ( track [ "@attr" ] && track [ "@attr" ] . nowplaying ) )
87101 . map ( ( track ) => ( {
88102 created_at : new Date ( ) . toISOString ( ) ,
@@ -91,9 +105,10 @@ export async function scrobbles(env: Variables): Promise<string> {
91105 track_name : track . name ,
92106 album_name : track . album [ "#text" ] ,
93107 lastfm_data : track ,
108+ hooman_id : hoomanId ,
94109 } ) ) ;
95110
96- const { error, message } = await table . save ( toInsert ) ;
111+ const { error, message } = await listened . save ( toInsert ) ;
97112 if ( error ) {
98113 throw new Error ( error . toString ( ) ) ;
99114 }
0 commit comments