@@ -22,11 +22,6 @@ export async function scrobbles(
2222
2323 const startFrom : number | null = await listened . getLastListenedDate ( hoomanId ) ;
2424
25- const size = 50 ;
26- let totalPages = 1 ;
27- let total = 0 ;
28- let page = 1 ;
29-
3025 if ( startFrom ) {
3126 console . log (
3227 "Starting from last listened date:" ,
@@ -36,6 +31,7 @@ export async function scrobbles(
3631 console . log ( "Database is empty, starting from the last page" ) ;
3732 }
3833
34+ const size = 50 ;
3935 const fmInitial = await getRecentTracks (
4036 env . LASTFM_API_KEY ,
4137 lastFmUserToUse ,
@@ -46,41 +42,47 @@ export async function scrobbles(
4642 if ( fmInitial === null ) {
4743 throw new Error ( "Fail - No tracks returned from initial api request." ) ;
4844 }
45+
4946 const paginationInitial = fmInitial . recenttracks [ "@attr" ] ;
50- totalPages = parseInt ( paginationInitial . totalPages ) ;
51- total = parseInt ( paginationInitial . total ) ;
52- page = totalPages ;
47+ const totalPages = parseInt ( paginationInitial . totalPages ) ;
5348
54- if ( total === 0 ) {
49+ // deno-lint-ignore prefer-const
50+ let count = {
51+ total : parseInt ( paginationInitial . total ) ,
52+ page : totalPages
53+ }
54+
55+ if ( count . total === 0 ) {
5556 console . log ( "Nothing new to save." ) ;
5657 return "ok" ;
5758 }
5859
5960 if ( startFrom ) {
6061 console . log (
61- `Found ${ total } new tracks to save! Starting from page ${ totalPages } .` ,
62+ `Found ${ count . total } new tracks to save! Starting from page ${ totalPages } .` ,
6263 ) ;
6364 } else {
6465 console . log (
65- `Found ${ total } tracks in total. Starting from page ${ totalPages } .` ,
66+ `Found ${ count . total } tracks in total. Starting from page ${ totalPages } .` ,
6667 ) ;
6768 }
6869
6970 let processedPages = 0 ;
71+ let processedItems = 0 ;
7072 do {
71- if ( processedPages === 10 ) {
73+
74+ // bee good to api server, download only a few pages and wait for the next invocation
75+ if ( processedPages === 11 ) {
7276 console . log (
73- `Already inserted ${
74- processedPages * size
75- } items to db. Stopped. See ya in next cron.`,
77+ `Already inserted ${ processedItems } items to db. Stopped. See ya at next cron.` ,
7678 ) ;
7779 break ;
7880 }
7981
8082 const fm = await getRecentTracks (
8183 env . LASTFM_API_KEY ,
8284 lastFmUserToUse ,
83- page ,
85+ count . page ,
8486 size ,
8587 startFrom ,
8688 ) ;
@@ -91,11 +93,11 @@ export async function scrobbles(
9193 const data = fm . recenttracks ;
9294 const tracks = data . track ;
9395
94- if ( total === 0 || tracks . length === 0 ) {
96+ if ( count . total === 0 || tracks . length === 0 ) {
9597 break ;
9698 }
9799
98- console . log ( `Fetching page ${ page } /${ totalPages } ` ) ;
100+ console . log ( `Fetching page ${ count . page } /${ totalPages } ` ) ;
99101
100102 const toInsert : ListenedRow [ ] = tracks
101103 . filter ( ( track ) => ! ( track [ "@attr" ] && track [ "@attr" ] . nowplaying ) )
@@ -116,11 +118,12 @@ export async function scrobbles(
116118
117119 if ( message ) {
118120 console . log ( message ) ;
121+ processedItems = processedItems + toInsert . length ;
119122 }
120123
121124 processedPages ++ ;
122- page -- ;
123- } while ( page >= 1 ) ;
125+ count . page -- ;
126+ } while ( count . page >= 1 ) ;
124127
125128 return "ok" ;
126129}
0 commit comments