Skip to content

Commit

Permalink
Refactor fetching tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeop committed Oct 27, 2023
1 parent 89826a3 commit 723f7a1
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export const LOAD_TAG_INFO_SUCCESS = 'LOAD_TAG_INFO_SUCCESS';
export const LOAD_TAG_INFO_ERROR = 'LOAD_TAG_INFO_ERROR';
const lastfm = new rest.LastFmApi(globals.lastfmApiKey, globals.lastfmApiSecret);

export function loadTagInfoStart(tag) {
export function loadTagInfoStart(tag: string) {
return {
type: LOAD_TAG_INFO_START,
payload: tag
};
}

export function loadTagInfoSuccess(tag, data) {
export function loadTagInfoSuccess(tag: string, data) {
return {
type: LOAD_TAG_INFO_SUCCESS,
payload: {
Expand All @@ -24,14 +24,14 @@ export function loadTagInfoSuccess(tag, data) {
};
}

export function loadTagInfoError(tag) {
export function loadTagInfoError(tag: string) {
return {
type: LOAD_TAG_INFO_ERROR,
payload: tag
};
}

export function loadTagInfo(tag) {
export function loadTagInfo(tag: string) {
return dispatch => {
dispatch(loadTagInfoStart(tag));

Expand All @@ -41,8 +41,7 @@ export function loadTagInfo(tag) {
lastfm.getTagAlbums(tag),
lastfm.getTagArtists(tag)
])
.then(results => Promise.all(results.map(r => r.json())))
.then(results => {
.then((results) => {
dispatch(loadTagInfoSuccess(tag, results));
})
.catch(error => {
Expand Down
10 changes: 4 additions & 6 deletions packages/app/app/components/TagView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class TagView extends React.Component {
);
}


renderTopAlbums (topAlbums) {
return (
<TagTopList
Expand All @@ -54,7 +53,6 @@ class TagView extends React.Component {
}

renderTagTopTracks (topTracks, addToQueue) {

return (
<TagTopTracks
tracks={topTracks}
Expand Down Expand Up @@ -85,10 +83,10 @@ class TagView extends React.Component {
const { addToQueue, tag, tags } = this.props;
let tagInfo, topTracks, topAlbums, topArtists;
if (tags[tag] && tags[tag].loading !== true) {
tagInfo = tags[tag][0].tag;
topTracks = tags[tag][1].tracks.track;
topAlbums = tags[tag][2].albums.album;
topArtists = tags[tag][3].topartists.artist;
tagInfo = tags[tag][0];
topTracks = tags[tag][1];
topAlbums = tags[tag][2];
topArtists = tags[tag][3];
}
return (
<div className={styles.tag_view_container}>
Expand Down
File renamed without changes.
124 changes: 66 additions & 58 deletions packages/core/src/rest/Lastfm.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import md5 from 'md5';
import { LastFmArtistInfo, LastfmAlbum, LastfmArtistShort, LastfmTag, LastfmTrackMatch } from './Lastfm.types';

const scrobblingApiUrl = 'https://ws.audioscrobbler.com/2.0/';

class LastFmApi {
constructor(
private key: string,
private secret: string
) {}
) { }

sign(url: string): string {
const tokens = decodeURIComponent((url.split('?')[1].split('&').sort().join()).replace(/,/g, '').replace(/=/g, ''));
Expand All @@ -16,8 +17,8 @@ class LastFmApi {

prepareUrl(url: string): string {
const withApiKey = `${url}&api_key=${this.key}`;
return `${withApiKey}&api_sig=${this.sign(withApiKey)}` ;

return `${withApiKey}&api_sig=${this.sign(withApiKey)}`;
}

addApiKey(url: string): string {
Expand All @@ -29,20 +30,20 @@ class LastFmApi {
}

lastFmLogin(authToken: string): Promise<Response> {
return fetch(this.prepareUrl(scrobblingApiUrl + '?method=auth.getSession&token=' + authToken)+'&format=json');
return fetch(this.prepareUrl(scrobblingApiUrl + '?method=auth.getSession&token=' + authToken) + '&format=json');
}

scrobble(artist: string, track: string, session: string): Promise<Response> {
return fetch(this.prepareUrl(
scrobblingApiUrl +
'?method=track.scrobble&sk=' +
session +
'&artist=' +
encodeURIComponent(artist) +
'&track=' +
encodeURIComponent(track) +
'&timestamp=' +
(Math.floor(Date.now() /1000 - 540))),
'?method=track.scrobble&sk=' +
session +
'&artist=' +
encodeURIComponent(artist) +
'&track=' +
encodeURIComponent(track) +
'&timestamp=' +
(Math.floor(Date.now() / 1000 - 540))),
{
method: 'POST'
}
Expand All @@ -52,12 +53,12 @@ class LastFmApi {
updateNowPlaying(artist: string, track: string, session: string): Promise<Response> {
return fetch(this.prepareUrl(
scrobblingApiUrl +
'?method=track.updateNowPlaying&sk=' +
session +
'&artist=' +
encodeURIComponent(artist) +
'&track=' +
encodeURIComponent(track)),
'?method=track.updateNowPlaying&sk=' +
session +
'&artist=' +
encodeURIComponent(artist) +
'&track=' +
encodeURIComponent(track)),
{
method: 'POST'
}
Expand All @@ -67,94 +68,101 @@ class LastFmApi {
getArtistInfo(artist: string): Promise<Response> {
return fetch(this.addApiKey(
scrobblingApiUrl +
'?method=artist.getinfo&artist=' +
encodeURIComponent(artist) +
'&format=json'
'?method=artist.getinfo&artist=' +
encodeURIComponent(artist) +
'&format=json'
));
}

getArtistTopTracks(artist: string): Promise<Response> {
return fetch(this.addApiKey(
scrobblingApiUrl +
'?method=artist.gettoptracks&artist=' +
encodeURIComponent(artist) +
'&format=json'
'?method=artist.gettoptracks&artist=' +
encodeURIComponent(artist) +
'&format=json'
));
}

getTopTags(): Promise<Response> {
return fetch(this.addApiKey(
scrobblingApiUrl +
'?method=tag.getTopTags&format=json'
'?method=tag.getTopTags&format=json'
));
}

getTopTracks(): Promise<Response> {
return fetch(this.addApiKey(
scrobblingApiUrl +
'?method=chart.getTopTracks&format=json'
'?method=chart.getTopTracks&format=json'
));
}

getTagInfo(tag: string): Promise<Response> {
return fetch(this.addApiKey(
async getTagInfo(tag: string): Promise<LastfmTag> {
const result = await (await fetch(this.addApiKey(
scrobblingApiUrl +
'?method=tag.getInfo&format=json&tag=' +
tag
));
'?method=tag.getInfo&format=json&tag=' +
tag
))).json();

return result.tag as LastfmTag;
}

getTagTracks(tag: string): Promise<Response> {
return fetch(this.addApiKey(
async getTagTracks(tag: string): Promise<LastfmTrackMatch[]> {
const result = await (await fetch(this.addApiKey(
scrobblingApiUrl +
'?method=tag.getTopTracks&format=json&tag=' +
tag
));
'?method=tag.getTopTracks&format=json&tag=' +
tag
))).json();

return result.tracks.track as LastfmTrackMatch[];
}

getTagAlbums(tag: string): Promise<Response> {
return fetch(this.addApiKey(
async getTagAlbums(tag: string): Promise<LastfmAlbum[]> {
const result = await (await fetch(this.addApiKey(
scrobblingApiUrl +
'?method=tag.getTopAlbums&format=json&tag=' +
tag
));
'?method=tag.getTopAlbums&format=json&tag=' +
tag
))).json();
return result.albums.album as LastfmAlbum[];
}

getTagArtists(tag: string): Promise<Response> {
return fetch(this.addApiKey(
async getTagArtists(tag: string): Promise<LastfmArtistShort[]> {
const result = await (await fetch(this.addApiKey(
scrobblingApiUrl +
'?method=tag.getTopArtists&format=json&tag=' +
tag
));
'?method=tag.getTopArtists&format=json&tag=' +
tag
))).json();

return result.topartists.artist as LastfmArtistShort[];
}

getSimilarTags(tag: string): Promise<Response> {
return fetch(this.addApiKey(
scrobblingApiUrl +
'?method=tag.getSimilar&format=json&tag=' +
tag
'?method=tag.getSimilar&format=json&tag=' +
tag
));
}

getSimilarTracks(artist: string, track: string, limit = 100): Promise<Response> {
return fetch(this.addApiKey(
scrobblingApiUrl +
'?method=track.getSimilar&format=json&artist=' +
artist +
'&track=' +
track +
'&limit=' +
limit
'?method=track.getSimilar&format=json&artist=' +
artist +
'&track=' +
track +
'&limit=' +
limit
));
}

searchTracks(terms: string, limit = 30): Promise<Response> {
return fetch(this.addApiKey(
scrobblingApiUrl +
'?method=track.search&format=json&track=' +
encodeURIComponent(terms) +
'&limit=' +
limit
'?method=track.search&format=json&track=' +
encodeURIComponent(terms) +
'&limit=' +
limit
));
}

Expand Down
28 changes: 21 additions & 7 deletions packages/core/src/rest/Lastfm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export type LastfmArtistShort = {
export type LastfmTag = {
name: string;
url: string;
reach: number;
taggings: number;
wiki: {
summary: string;
content: string;
};
}

export type LastFmArtistInfo = {
Expand Down Expand Up @@ -49,13 +55,13 @@ export type LastfmTopTracks = {
}

export type LastfmTrackMatch = {
name: string
artist: string
image: LastfmImage[]
listeners: string
mbid: string
streamable: string
url: string
name: string;
mbid: string;
url: string;
streamable: string;
artist: string;
image: LastfmImage[];
listeners: string;
}

export type LastfmTrackMatchInternal = LastfmTrackMatch & { thumbnail: string }
Expand All @@ -65,3 +71,11 @@ export type LastfmTopTag = {
count: number
reach: number
}

export type LastfmAlbum = {
name: string;
mbid: string;
url: string;
artist: string;
image: LastfmImage[];
}

0 comments on commit 723f7a1

Please sign in to comment.