-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (134 loc) · 4.64 KB
/
index.js
File metadata and controls
154 lines (134 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// @ts-check
import { YMApi } from 'ym-api'
import { z } from 'zod'
import { LastFMTrack } from 'lastfm-ts-api'
const envSchema = z.object({
YM_TOKEN: z.string(),
LASTFM_API_KEY: z.string(),
LASTFM_API_SECRET: z.string(),
LASTFM_SESSION: z.string(),
})
const env = envSchema.parse(process.env)
const ymApi = new YMApi()
const lastFMtrack = new LastFMTrack(
env.LASTFM_API_KEY,
env.LASTFM_API_SECRET,
env.LASTFM_SESSION
)
const HistoryReponseSchema = z.object({
historyTabs: z.array(
z.object({
date: z.string(),
items: z.array(
z.object({
context: z.object({ type: z.string() }),
tracks: z.array(
z.object({
type: z.string(),
data: z.object({
fullModel: z.object({
id: z.string(),
title: z.string(),
artists: z.array(
z.object({ name: z.string() })
),
durationMs: z.number().optional(),
albums: z.array(
z.object({ title: z.string() })
),
}),
}),
})
),
})
),
})
),
})
const getDateInfo = ({ daysShift }) => {
// TODO: UTC
const today = new Date()
const targetDate = new Date(today)
targetDate.setDate(today.getDate() + daysShift)
const year = targetDate.getFullYear()
const month = String(targetDate.getMonth() + 1).padStart(2, '0')
const day = String(targetDate.getDate()).padStart(2, '0')
const dateString = `${year}-${month}-${day}`
const timestamp =
new Date(
targetDate.getFullYear(),
targetDate.getMonth(),
targetDate.getDate(),
1,
0,
0
).getTime() / 1000 // 1 AM
return { dateString, timestamp }
}
const run = async () => {
await ymApi.init({ access_token: env.YM_TOKEN })
// @ts-ignore
const historyRawData = await ymApi.getHistory()
const history = HistoryReponseSchema.parse(historyRawData)
let {
dateString: yesterdayDateString,
timestamp: currentScrobbleTimestamp,
} = getDateInfo({ daysShift: -1 }) // -1 means yesterday
const yesterdayHistory = history.historyTabs.find(
(tab) => tab.date === yesterdayDateString
)
if (!yesterdayHistory) {
console.log(`No tracks found for ${yesterdayDateString}`)
return
}
const totalTracks = yesterdayHistory.items.reduce(
(acc, item) => acc + item.tracks.length,
0
)
console.log(`Found ${totalTracks} tracks for ${yesterdayDateString}`)
/** @type import('lastfm-ts-api').LastFMTrackScrobbleParams[] */
const tracksToScrobble = []
for (const item of yesterdayHistory.items) {
if (!item.tracks || item.tracks.length === 0) {
continue
}
const chosenByUser = item.context.type === 'wave' ? 0 : 1
for (const track of item.tracks) {
const fullModel = track.data.fullModel
if (!fullModel.durationMs) {
return
}
const artist = fullModel.artists[0].name
const title = fullModel.title
const durationSec = fullModel.durationMs / 1000
const album =
fullModel.albums && fullModel.albums.length > 0
? fullModel.albums[0].title
: undefined
if (
currentScrobbleTimestamp + durationSec >
new Date().getTime() / 1000
) {
console.log('Skipping scrobble due to time overflow')
return
}
tracksToScrobble.push({
artist,
track: title,
chosenByUser,
timestamp: Math.round(currentScrobbleTimestamp),
duration: durationSec,
album,
})
currentScrobbleTimestamp += durationSec
}
}
// Scrobble tracks in batches of 50
for (let i = 0; i < tracksToScrobble.length; i += 50) {
const batch = tracksToScrobble.slice(i, i + 50)
const result = await lastFMtrack.scrobbleMany(batch)
console.log('Successfully scrobbled tracks:')
console.dir(result, { depth: null })
}
}
run()