forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1import axios from "axios";
2import { API_URL } from "../consts";
3
4function useLibrary() {
5 const getSongByUri = async (uri: string) => {
6 const response = await axios.get(`${API_URL}/users/${uri}`);
7 return {
8 id: response.data?.xata_id,
9 title: response.data?.title,
10 artist: response.data?.artist,
11 albumArtist: response.data?.album_artist,
12 album: response.data?.album,
13 cover: response.data?.album_art,
14 tags: [],
15 artistUri: response.data?.artist_uri,
16 albumUri: response.data?.album_uri,
17 listeners: response.data?.listeners || 1,
18 scrobbles: response.data?.scrobbles || 1,
19 lyrics: response.data?.lyrics,
20 spotifyLink: response.data?.spotify_link,
21 };
22 };
23
24 const getArtistTracks = async (
25 uri: string,
26 limit = 10
27 ): Promise<
28 {
29 xata_id: string;
30 title: string;
31 artist: string;
32 album_artist: string;
33 album_art: string;
34 uri: string;
35 scrobbles: number;
36 album_uri?: string;
37 artist_uri?: string;
38 }[]
39 > => {
40 const response = await axios.get(
41 `${API_URL}/users/${uri}/tracks?size=${limit}`
42 );
43 return response.data;
44 };
45
46 const getArtistAlbums = async (
47 uri: string,
48 limit = 10
49 ): Promise<
50 {
51 xata_id: string;
52 title: string;
53 artist: string;
54 album_art: string;
55 artist_uri: string;
56 uri: string;
57 }[]
58 > => {
59 const response = await axios.get(
60 `${API_URL}/users/${uri}/albums?size=${limit}`
61 );
62 return response.data;
63 };
64
65 const getArtists = async (did: string, offset = 0, limit = 30) => {
66 const response = await axios.get(
67 `${API_URL}/users/${did}/artists?size=${limit}&offset=${offset}`
68 );
69 return response.data;
70 };
71
72 const getAlbums = async (did: string, offset = 0, limit = 12) => {
73 const response = await axios.get(
74 `${API_URL}/users/${did}/albums?size=${limit}&offset=${offset}`
75 );
76 return response.data;
77 };
78
79 const getTracks = async (did: string, offset = 0, limit = 20) => {
80 const response = await axios.get(
81 `${API_URL}/users/${did}/tracks?size=${limit}&offset=${offset}`
82 );
83 return response.data;
84 };
85
86 const getLovedTracks = async (did: string, offset = 0, limit = 20) => {
87 const response = await axios.get(
88 `${API_URL}/users/${did}/likes?size=${limit}&offset=${offset}`
89 );
90 return response.data;
91 };
92
93 const getAlbum = async (did: string, rkey: string) => {
94 const response = await axios.get(
95 `${API_URL}/users/${did}/app.rocksky.album/${rkey}`
96 );
97 return response.data;
98 };
99
100 const getArtist = async (did: string, rkey: string) => {
101 const response = await axios.get(
102 `${API_URL}/users/${did}/app.rocksky.artist/${rkey}`
103 );
104 return response.data;
105 };
106
107 return {
108 getSongByUri,
109 getArtists,
110 getAlbums,
111 getTracks,
112 getLovedTracks,
113 getAlbum,
114 getArtist,
115 getArtistTracks,
116 getArtistAlbums,
117 };
118}
119
120export default useLibrary;