Skip to content

Commit

Permalink
feat(lastFm): create new types and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Feb 4, 2024
1 parent 5a20f4b commit cc0bcee
Showing 1 changed file with 98 additions and 4 deletions.
102 changes: 98 additions & 4 deletions src/shared/lib/lastFm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,30 @@ export type Track = {
'@attr': { rank: string }
}

type ImageSize = 'small' | 'medium' | 'large' | 'extralarge'
type Period = 'overall' | '7day' | '1month' | '3month' | '6month' | '12month'

type RecentTrack = {
artist: {
mbid: string
'#text': string
}
streamable: '0' | '1'
image: {
size: ImageSize
'#text': string
}[]

mbid: string
album: { mbid: string; '#text': string }
name: string
url: string
date: { uts: string; '#text': string }
}

type RecentTracksBody = {
recenttracks: {
track: Track[]
track: RecentTrack[]
}
}

Expand Down Expand Up @@ -88,15 +109,34 @@ export async function getLastFmTopArtists() {
return artist
}

type TopTrack = {
streamable: { fulltrack: '0' | '1'; '#text': '0' | '1' }
mbid: string
name: string
image: {
size: ImageSize
'#text': string
}[]
artist: {
url: string
name: string
mbid: string
}
url: string
duration: string
'@attr': { rank: string }
playcount: string
}

type TopTracksBody = {
toptracks: {
track: Track[]
track: TopTrack[]
}
}

export async function getLastFmTopTracks() {
export async function getLastFmTopTracks(period: Period = '6month') {
const lastFmApiRequest = await fetch(
`http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=mateusfg7&api_key=${process.env.LASTFM_API_KEY}&format=json&period=6month`
`http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=mateusfg7&api_key=${process.env.LASTFM_API_KEY}&format=json&period=${period}`
)

if (!lastFmApiRequest.ok) {
Expand All @@ -115,6 +155,60 @@ export async function getLastFmTopTracks() {
return tracks
}

type TrackInfoBody = {
track: {
name: string
mbid: string
url: string
duration: string
streamable: { '#text': '0' | '1'; fulltrack: '0' | '1' }
listeners: string
playcount: string
artist: {
name: string
mbid: string
url: string
}
album: {
artist: string
title: string
mbid: string
url: string
image: {
size: ImageSize
'#text': string
}[]
'@attr': { position: string }
}
toptags: { tag: { name: string; url: string }[] }
}
}

export async function getTrackInfo({
trackName,
artistName
}: {
trackName: string
artistName: string
}) {
const lastFmApiRequest = await fetch(
`http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=${process.env.LASTFM_API_KEY}&artist=${artistName}&track=${trackName}&format=json`
)

if (!lastFmApiRequest.ok) {
console.log(lastFmApiRequest)
throw new ApiError({
message: lastFmApiRequest.statusText,
status: lastFmApiRequest.status,
url: lastFmApiRequest.url
})
}

const { track }: TrackInfoBody = await lastFmApiRequest.json()

return track
}

type User = {
name: string
playcount: string
Expand Down

0 comments on commit cc0bcee

Please sign in to comment.