Reimplement Last.fm, minor design improvements #2
19
README.md
19
README.md
@ -29,28 +29,13 @@ Any contribution is welcome. If you want to test the website in your own machine
|
|||||||
- `bun install` - Install dependencies
|
- `bun install` - Install dependencies
|
||||||
- `bun dev` - Run dev server
|
- `bun dev` - Run dev server
|
||||||
|
|
||||||
The development server will be available at http://localhost:5173.
|
The development server will be available at <http://localhost:5173>.
|
||||||
|
|
||||||
### Environment Variables
|
|
||||||
|
|
||||||
**The LastFM feature is disabled, and this environment variable is not required yet.**
|
|
||||||
|
|
||||||
To run the project, you need to set up the following environment variables:
|
|
||||||
|
|
||||||
| Variable | Description |
|
|
||||||
|-----------------------------|--------------------------|
|
|
||||||
| `VITE_LASTFM_API_KEY` | Your [Last.fm API Key]() |
|
|
||||||
|
|
||||||
Create a `.env` file in the root of the project and add the required variables like so:
|
|
||||||
|
|
||||||
```env
|
|
||||||
VITE_LASTFM_API_KEY=your-lastfm-api-key-here
|
|
||||||
```
|
|
||||||
|
|
||||||
## Special Thanks
|
## Special Thanks
|
||||||
|
|
||||||
- [Aidan](https://github.com/ihatenodejs)
|
- [Aidan](https://github.com/ihatenodejs)
|
||||||
- [lucmsilva](https://github.com/lucmsilva651/)
|
- [lucmsilva](https://github.com/lucmsilva651/)
|
||||||
- NineTailedFox
|
- NineTailedFox
|
||||||
|
- [biancarosa](https://github.com/biancarosa) - Last.fm API
|
||||||
|
|
||||||
**Enjoy!**
|
**Enjoy!**
|
||||||
|
@ -1,45 +1,61 @@
|
|||||||
import { api } from "../lib/axios";
|
import { api } from "../lib/axios";
|
||||||
|
|
||||||
const api_key = null;
|
export interface TrackResponse {
|
||||||
|
track: {
|
||||||
|
album: {
|
||||||
|
mbid: string,
|
||||||
|
"#text": string
|
||||||
|
},
|
||||||
|
artist: {
|
||||||
|
mbid: string,
|
||||||
|
"#text": string
|
||||||
|
},
|
||||||
|
date: {
|
||||||
|
uts: string,
|
||||||
|
"#text": string
|
||||||
|
},
|
||||||
|
image: [{
|
||||||
|
"#text": string,
|
||||||
|
size: string
|
||||||
|
}],
|
||||||
|
mbid: string,
|
||||||
|
name: string,
|
||||||
|
streamable: string,
|
||||||
|
url: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export interface getRecentTracksResponse {
|
export interface getRecentTracksResponse {
|
||||||
recenttracks: {
|
recenttracks: {
|
||||||
track: [
|
track: [{
|
||||||
{
|
artist: {
|
||||||
artist: {
|
|
||||||
mbid: string,
|
|
||||||
"#text": string
|
|
||||||
},
|
|
||||||
streamable: string,
|
|
||||||
image: [],
|
|
||||||
mbid: string,
|
mbid: string,
|
||||||
name: string,
|
"#text": string
|
||||||
url: string,
|
},
|
||||||
date: {
|
streamable: string,
|
||||||
uts: string,
|
image: [{
|
||||||
"#text": string
|
"#text": string,
|
||||||
}
|
size: string
|
||||||
|
}],
|
||||||
|
mbid: string,
|
||||||
|
name: string,
|
||||||
|
url: string,
|
||||||
|
date: {
|
||||||
|
uts: string,
|
||||||
|
"#text": string
|
||||||
}
|
}
|
||||||
],
|
}]
|
||||||
"@attr": {
|
|
||||||
user: string,
|
|
||||||
totalPages: string,
|
|
||||||
page: string,
|
|
||||||
perPage: string,
|
|
||||||
total: string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRecentTracks() {
|
export async function getRecentTracks() {
|
||||||
const response = await api.get<getRecentTracksResponse>('/', {
|
try {
|
||||||
params: {
|
const response = await api.get<TrackResponse>('');
|
||||||
api_key,
|
console.log('Last.fm data:', response.data);
|
||||||
method: 'user.getrecenttracks',
|
|
||||||
user: 'givfnz',
|
return response.data;
|
||||||
format: 'json',
|
} catch (error) {
|
||||||
limit: 1
|
console.error('Err while fetching Last.fm data:', error);
|
||||||
}
|
throw error;
|
||||||
})
|
}
|
||||||
return response.data
|
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
|
||||||
|
const defaultApiUrl = 'https://lastfm-last-played.biancarosa.com.br/givfnz/latest-song';
|
||||||
|
|
||||||
export const api = axios.create({
|
export const api = axios.create({
|
||||||
baseURL: import.meta.env.VITE_LASTFM_API_URL,
|
baseURL: import.meta.env.VITE_LASTFM_API_URL || defaultApiUrl,
|
||||||
})
|
})
|
@ -10,8 +10,10 @@
|
|||||||
"send": "Send",
|
"send": "Send",
|
||||||
"myLast": "My Last.FM Status",
|
"myLast": "My Last.FM Status",
|
||||||
"myLastDescription": "I'm listening to:",
|
"myLastDescription": "I'm listening to:",
|
||||||
|
"lastPlayed": "Last played song:",
|
||||||
"lastLink": "View on Last.FM",
|
"lastLink": "View on Last.FM",
|
||||||
"music": "Music",
|
"music": "Music",
|
||||||
"currentlyListening": "I'm currently listening to it!",
|
"currentlyListening": "I'm currently listening to it!",
|
||||||
"lastUpdate": "Last update: "
|
"lastUpdate": "Last update: ",
|
||||||
|
"noRecentTracks": "No recent tracks found"
|
||||||
}
|
}
|
@ -10,8 +10,10 @@
|
|||||||
"send": "Enviar",
|
"send": "Enviar",
|
||||||
"myLast": "Meu status do Last.FM",
|
"myLast": "Meu status do Last.FM",
|
||||||
"myLastDescription": "Dê uma olhada na música que eu estava ouvindo:",
|
"myLastDescription": "Dê uma olhada na música que eu estava ouvindo:",
|
||||||
|
"lastPlayed": "Última música ouvida:",
|
||||||
"lastLink": "Clique aqui para ver a música no Last.FM",
|
"lastLink": "Clique aqui para ver a música no Last.FM",
|
||||||
"music": "Música",
|
"music": "Música",
|
||||||
"currentlyListening": "Eu estou ouvindo neste momento!",
|
"currentlyListening": "Eu estou ouvindo neste momento!",
|
||||||
"lastUpdate": "Última atualização: "
|
"lastUpdate": "Última atualização: ",
|
||||||
|
"noRecentTracks": "Nenhuma música recente encontrada"
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { getRecentTracks } from "../../api/lastfm";
|
import { getRecentTracks, TrackResponse } from "../../api/lastfm";
|
||||||
import { MainContainer } from "../../components/MainContent/styles";
|
import { MainContainer } from "../../components/MainContent/styles";
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { Paragraph } from "../../components/Paragraph/styles";
|
import { Paragraph } from "../../components/Paragraph/styles";
|
||||||
@ -8,22 +8,48 @@ import { MusicDescription, MusicTitle } from "./styles";
|
|||||||
|
|
||||||
export default function Music() {
|
export default function Music() {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const { data: lastResponse } = useQuery({
|
const { data: lastResponse, isLoading, isError, error } = useQuery<TrackResponse>({
|
||||||
queryKey: ['song'],
|
queryKey: ['song'],
|
||||||
queryFn: getRecentTracks
|
queryFn: getRecentTracks,
|
||||||
|
retry: 1,
|
||||||
|
refetchOnWindowFocus: false
|
||||||
})
|
})
|
||||||
const isAvailable = false;
|
|
||||||
if(isAvailable){
|
const isAvailable = true;
|
||||||
|
if (isAvailable){
|
||||||
|
const track = lastResponse?.track;
|
||||||
|
const isCurrentlyPlaying = !track?.date?.["#text"];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MainContainer>
|
<MainContainer>
|
||||||
<MusicTitle>{t("myLast")}</MusicTitle>
|
<MusicTitle>{t("myLast")}</MusicTitle>
|
||||||
<MusicDescription>{t("myLastDescription")}</MusicDescription>
|
|
||||||
<Paragraph>{lastResponse?.recenttracks?.track[0].name} - {lastResponse?.recenttracks?.track[0].artist["#text"]}</Paragraph>
|
{isLoading ? (
|
||||||
<IconLink target="blank" href={lastResponse?.recenttracks?.track[0].url}>{t("lastLink")}</IconLink>
|
<Paragraph>Loading music data...</Paragraph>
|
||||||
{lastResponse?.recenttracks?.track[0]?.date?.["#text"] ?
|
) : isError ? (
|
||||||
<Paragraph>{t("lastUpdate")}{lastResponse?.recenttracks?.track[0]?.date["#text"]} UTC</Paragraph>
|
<>
|
||||||
: <Paragraph>{t("currentlyListening")}</Paragraph>
|
<Paragraph>Error loading music data</Paragraph>
|
||||||
}
|
<Paragraph>Error details: {error instanceof Error ? error.message : String(error)}</Paragraph>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<MusicDescription>
|
||||||
|
{isCurrentlyPlaying ? t("myLastDescription") : t("lastPlayed")}
|
||||||
|
</MusicDescription>
|
||||||
|
{track ? (
|
||||||
|
<>
|
||||||
|
<Paragraph>{track.name} - {track.artist["#text"]}</Paragraph>
|
||||||
|
<IconLink target="blank" href={track.url}>{t("lastLink")}</IconLink>
|
||||||
|
{isCurrentlyPlaying ?
|
||||||
|
<Paragraph>{t("currentlyListening")}</Paragraph>
|
||||||
|
: <Paragraph>{t("lastUpdate")}{track.date?.["#text"]} UTC</Paragraph>
|
||||||
|
}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<Paragraph>{t("noRecentTracks")}</Paragraph>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</MainContainer>
|
</MainContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user