Skip to content

Commit

Permalink
Add artist details
Browse files Browse the repository at this point in the history
  • Loading branch information
Davilarek committed May 27, 2024
1 parent 5703583 commit 71c1cdf
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This API has been designed for both Node.JS and Browserside JS.
- [x] Comments
- [ ] Popular songs list
- [ ] Popular artists list
- [ ] Artist details
- [X] Artist details
- [ ] Missing translations list
- [ ] Missing lyrics list
- [ ] Translation edit history
Expand Down
31 changes: 31 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<dd></dd>
<dt><a href="#TekstowoAPISearchResults">TekstowoAPISearchResults</a></dt>
<dd></dd>
<dt><a href="#TekstowoAPIArtistProfile">TekstowoAPIArtistProfile</a></dt>
<dd></dd>
<dt><a href="#TekstowoAPILyrics">TekstowoAPILyrics</a></dt>
<dd></dd>
<dt><a href="#TekstowoAPI">TekstowoAPI</a></dt>
Expand Down Expand Up @@ -83,6 +85,23 @@
| artists | <code>Object.&lt;string, TekstowoAPIArtistID&gt;</code> |
| pageCount | <code>number</code> |

<a name="TekstowoAPIArtistProfile"></a>

## TekstowoAPIArtistProfile
**Kind**: global class
<a name="new_TekstowoAPIArtistProfile_new"></a>

### new TekstowoAPIArtistProfile(displayName, artistDescription, images, discography, comments, internalId)

| Param | Type |
| --- | --- |
| displayName | <code>string</code> |
| artistDescription | <code>string</code> |
| images | <code>Array.&lt;{hd: string, small: string}&gt;</code> |
| discography | <code>Array.&lt;{year: number, name: string}&gt;</code> |
| comments | <code>number</code> \| <code>undefined</code> |
| internalId | <code>string</code> \| <code>undefined</code> |

<a name="TekstowoAPILyrics"></a>

## TekstowoAPILyrics
Expand Down Expand Up @@ -121,6 +140,7 @@
* [.getLyrics(artist, songName)](#TekstowoAPI+getLyrics) ⇒ <code>Promise.&lt;(TekstowoAPILyrics\|null)&gt;</code>
* [.getMetadata(songIdOrHtml, useHTML)](#TekstowoAPI+getMetadata) ⇒ <code>Promise.&lt;(TekstowoAPILyricsMetadata\|null)&gt;</code>
* [.getArtistsSongList(artistId, options)](#TekstowoAPI+getArtistsSongList) ⇒ <code>Promise.&lt;{pageCount: number, results: Array.&lt;KVPair.&lt;string, TekstowoAPIArtistID&gt;&gt;}&gt;</code>
* [.requestComments(internalId, offset, mode)](#TekstowoAPI+requestComments)

<a name="new_TekstowoAPI_new"></a>

Expand Down Expand Up @@ -269,6 +289,17 @@ Downloads and parses artist song list
| options.sortDir | <code>string</code> |
| options.sortMode | <code>string</code> |

<a name="TekstowoAPI+requestComments"></a>

### tekstowoAPI.requestComments(internalId, offset, mode)
**Kind**: instance method of [<code>TekstowoAPI</code>](#TekstowoAPI)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| internalId | <code>string</code> | | The post id we are pulling comments for. Can be song id, artist id. |
| offset | <code>\*</code> | <code>0</code> | How many comments have we already acknowledged? |
| mode | <code>\*</code> | <code>S</code> | What type is the post? 'S' for songs, 'A' for artists. |

<a name="KVPair"></a>

## KVPair
Expand Down
63 changes: 59 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ class TekstowoAPISearchResults {
}
}

class TekstowoAPIArtistProfile {
/**
* @param {string} displayName
* @param {string} artistDescription
* @param {Array<{ hd: string, small: string }>} images
* @param {Array<{ year: number, name: string }>} discography
* @param {number | undefined} comments
* @param {string | undefined} internalId
*/
constructor(displayName, artistDescription, images, discography, commentCount, internalId) {
this.displayName = displayName;
this.artistDescription = artistDescription;
this.images = images;
this.discography = discography;
this.commentCount = commentCount;
this.internalId = internalId;
}
}

const SortMode = {
get alphabetically() {
return "alfabetycznie";
Expand Down Expand Up @@ -125,9 +144,15 @@ const TekstowoAPIUrls = {
ARTIST_SONGS: (id, sortMode = SortMode.alphabetically, sortDir = (AutomaticSortDirection(sortMode)), page = 1) => {
return `https://www.tekstowo.pl/${ConstantURLPaths.artistSongs},${id},${sortMode},${sortDir},strona,${page}.html`;
},
/**
* @param {TekstowoAPIArtistID} id
*/
ARTIST_PROFILE: (id) => {
return `https://www.tekstowo.pl/wykonawca,${id}.html`;
},
__TEKSTOWO_OFFICIAL_API_USE_RARELY: {
MORE_COMMENTS: (internalSongId, offset = 0) => {
return `https://www.tekstowo.pl/js,moreComments,S,${internalSongId},${offset}`;
MORE_COMMENTS: (internalId, offset = 0, mode = 'S') => {
return `https://www.tekstowo.pl/js,moreComments,${mode},${internalId},${offset}`;
},
},
};
Expand Down Expand Up @@ -547,14 +572,44 @@ class TekstowoAPI {
// debugger;
return { pageCount, results: base2 };
}
async requestComments(internalSongId, offset = 0) {
/**
* @param {string} internalId The post id we are pulling comments for. Can be song id, artist id.
* @param {*} offset How many comments have we already acknowledged?
* @param {*} mode What type is the post? 'S' for songs, 'A' for artists.
*/
async requestComments(internalId, offset = 0, mode = 'S') {
const requestOptions = new TekstowoAPIRequestOptions(
this.proxyThisUrl(TekstowoAPIUrls.__TEKSTOWO_OFFICIAL_API_USE_RARELY.MORE_COMMENTS(internalSongId, offset)), { method: "GET" },
this.proxyThisUrl(TekstowoAPIUrls.__TEKSTOWO_OFFICIAL_API_USE_RARELY.MORE_COMMENTS(internalId, offset, mode)), { method: "GET" },
);
const response = await this.makeRequest(requestOptions);
const responseText = unescapeJsonString(await response.text());
return parseComments(responseText);
}
async getArtistProfile(artistId) {
const requestOptions = new TekstowoAPIRequestOptions(
this.proxyThisUrl(TekstowoAPIUrls.ARTIST_PROFILE(artistId)), { method: "GET" },
);
const response = await this.makeRequest(requestOptions);
const responseText = unescapeJsonString(await response.text());
const displayName = responseText.matchAll(/<div\sclass=".*?">\s+?<strong>(.*?)<\/strong>\s+?<\/div>/g);
const artistDescription = (responseText.split(`by="opis-tab">`)[1].split("</div>")[0].replace(/\n/g, '').replace(/<br \/>/g, '\n')).replace(/\r/g, '');
const artistDescriptionMoreMod = artistDescription.replace(/\t/g, '').trim();
const artistDiscography = getTextBetween(responseText.split(`by="dyskografia-tab">`)[1].split("</div>")[0], "<p>", "</p>").map(x => {
const base = x.match(/\d+?\.&nbsp;<b>(.*?)<\/b>\s*?\((\d+)\)/);
return { year: parseInt(base[2]), name: base[1] };
});
const artistMainPhoto = responseText.split(`id="main_art_photo" src="`)[1].split("\"")[0];
const artistPhotosRegex = /data-lightbox="artist" href="(\/.*?)"><img\s*?src="(\/.*?)"/g;
const artistPhotosRegexMatches = responseText.matchAll(artistPhotosRegex);
const artistPhotosResults = [];
for (const match of artistPhotosRegexMatches) {
artistPhotosResults.push({ hd: match[1], small: match[2] });
}
const commentCount = responseText.includes("Komentarze (") ? responseText.split("Komentarze (")[1].split("):")[0] : undefined;
const artistInternalId = responseText.includes("ajxShowMoreComments") ? responseText.split("ajxShowMoreComments('A',")[1].split(")")[0] : undefined;
const result = new TekstowoAPIArtistProfile(displayName.next().value[1], artistDescriptionMoreMod, [{ hd: artistMainPhoto, small: artistMainPhoto }, ...artistPhotosResults], artistDiscography, commentCount, artistInternalId);
return result;
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions test6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const TekstowoAPI = require("./index");
const TekstowoAPIInstance = new TekstowoAPI(fetch, 0);
(async () => {
const artistProfileData = await TekstowoAPIInstance.getArtistProfile("metallica");
console.log(artistProfileData);
console.log(await TekstowoAPIInstance.requestComments(artistProfileData.internalId, 0, 'A'));
})();

0 comments on commit 71c1cdf

Please sign in to comment.