Skip to content

Commit

Permalink
New BeatSaver API
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJAllan committed Aug 4, 2021
1 parent 193c8b0 commit e662537
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 52 deletions.
38 changes: 15 additions & 23 deletions src/api/beatsaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function get_data_by_hash(song_hash: string): Promise<IBeatSaverDat
if (cached_data !== undefined)
return cached_data;
try {
const data_str = await fetch2(`https://beatsaver.com/api/maps/by-hash/${song_hash}`);
const data_str = await fetch2(`https://api.beatsaver.com/maps/hash/${song_hash}`);
const data = JSON.parse(data_str);
api_cache.set(song_hash, data);
return data;
Expand All @@ -20,35 +20,27 @@ export async function get_data_by_hash(song_hash: string): Promise<IBeatSaverDat
}

export interface IBeatSaverData {
key: string;
id: string;
name: string;
hash: string;
downloadURL: string;
stats: {
downloads: number;
plays: number;
downVotes: number;
upVotes: number;
heat: number;
rating: number;
downloads: number;
upvotes: number;
downvotes: number;
score: number;
};
versions: IBeatSaverSongVersion[];
metadata: {
duration: number;
characteristics: IBeatSaverSongCharacteristic[];
};
}

export interface IBeatSaverSongCharacteristic {
name: string;
difficulties: {
easy: IBeatSaverSongDifficulty | null,
normal: IBeatSaverSongDifficulty | null,
hard: IBeatSaverSongDifficulty | null,
expert: IBeatSaverSongDifficulty | null,
expertPlus: IBeatSaverSongDifficulty | null,
};
}

interface IBeatSaverSongDifficulty {
notes: number;
export interface IBeatSaverSongVersion {
hash: string;
diffs: {
characteristic: string;
difficulty: string;
notes: number;
}[];
downloadURL: string;
}
12 changes: 6 additions & 6 deletions src/components/QuickButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@
try {
const song_info = await checked_hash_to_song_info(song_hash);
if (type === "BS") {
new_page(g.beatsaver_link + song_info.key);
new_page(g.beatsaver_link + song_info.id);
} else if (type === "OC") {
await oneclick_install(song_info.key);
await oneclick_install(song_info.id);
ok_after_download();
} else if (type === "Beast") {
new_page(g.bsaber_songs_link + song_info.key);
new_page(g.bsaber_songs_link + song_info.id);
} else if (type === "BeastBook") {
new_page(g.bsaber_songs_link + song_info.key);
new_page(g.bsaber_songs_link + song_info.id);
} else if (type === "Preview") {
new_page(
"https://skystudioapps.com/bs-viewer/?id=" + song_info.key
"https://skystudioapps.com/bs-viewer/?id=" + song_info.id
);
} else if (type === "BSR") {
txtDummyNode.value = `!bsr ${song_info.key}`;
txtDummyNode.value = `!bsr ${song_info.id}`;
txtDummyNode.select();
txtDummyNode.setSelectionRange(0, 99999);
document.execCommand("copy");
Expand Down
2 changes: 1 addition & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Global {
public static song_table_backup: HTMLElement | undefined;

public static readonly scoresaber_link = "https://scoresaber.com";
public static readonly beatsaver_link = "https://beatsaver.com/beatmap/";
public static readonly beatsaver_link = "https://beatsaver.com/maps/";
public static readonly bsaber_songs_link = "https://bsaber.com/songs/";
public static readonly song_hash_reg = /\/([\da-zA-Z]{40})\.png/;
public static readonly score_reg = /(score|accuracy):\s*([\d.,]+)%?\s*(\(([\w,]*)\))?/;
Expand Down
16 changes: 8 additions & 8 deletions src/pages/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function setup_dl_link_leaderboard(): void {
if (!data)
return;
show_beatsaver_song_data(beatsaver_box, data);
const data2 = await beastsaber.get_data(data.key);
const data2 = await beastsaber.get_data(data.id);
if (!data2)
return;
show_beastsaber_song_data(beastsaber_box, data2);
Expand All @@ -124,9 +124,9 @@ export function setup_dl_link_leaderboard(): void {
function show_beatsaver_song_data(elem: HTMLElement, data: beatsaver.IBeatSaverData) {
intor(elem,
create("div", { title: "Downloads" }, `${data.stats.downloads} 💾`),
create("div", { title: "Upvotes" }, `${data.stats.upVotes} 👍`),
create("div", { title: "Downvotes" }, `${data.stats.downVotes} 👎`),
create("div", { title: "Beatmap Rating" }, `${(data.stats.rating * 100).toFixed(2)}% 💯`),
create("div", { title: "Upvotes" }, `${data.stats.upvotes} 👍`),
create("div", { title: "Downvotes" }, `${data.stats.downvotes} 👎`),
create("div", { title: "Beatmap Rating" }, `${(data.stats.score * 100).toFixed(2)}% 💯`),
create("div", { title: "Beatmap Duration" }, `${number_to_timespan(data.metadata.duration)} ⏱`),
);
}
Expand Down Expand Up @@ -220,10 +220,10 @@ export function add_percentage(): void {
// - only expert+ shown, but actual diff is missing: https://scoresaber.com/leaderboard/314128
if (!diff_name)
return;
const standard_characteristic = data.metadata.characteristics.find(c => c.name === "Standard");
if (!diff_name || !standard_characteristic)
return;
const notes = get_notes_count(diff_name, standard_characteristic);
const version = data.versions.find((v) => v.hash === song_hash.toLowerCase());
if (!diff_name || !version)
return;
const notes = get_notes_count(diff_name, "Standard", version);
if (notes < 0)
return;
const max_score = calculate_max_score(notes);
Expand Down
8 changes: 4 additions & 4 deletions src/pages/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ export function add_percentage(): void {
return;
const song_column = check(row.querySelector(`th.song`));
const diff_name = check(song_column.querySelector(`span > span`)).innerText;
const standard_characteristic = data.metadata.characteristics.find(c => c.name === "Standard");
if (!diff_name || !standard_characteristic)
return;
const notes = get_notes_count(diff_name, standard_characteristic);
const version = data.versions.find((v) => v.hash === song_hash.toLowerCase());
if (!diff_name || !version)
return;
const notes = get_notes_count(diff_name, "Standard", version);
if (notes < 0)
return;
const max_score = calculate_max_score(notes);
Expand Down
13 changes: 3 additions & 10 deletions src/util/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function fetch_hash(link: string): Promise<string | undefined> {
export async function oneclick_install_byhash(song_hash: string): Promise<boolean> {
const song_info = await beatsaver.get_data_by_hash(song_hash);
if (!song_info) return false;
await oneclick_install(song_info.key);
await oneclick_install(song_info.id);
return true;
}

Expand Down Expand Up @@ -134,15 +134,8 @@ export function parse_score_bottom(text: string): { score?: number, accuracy?: n
return { score, accuracy, mods };
}

export function get_notes_count(diff_name: string, characteristic: beatsaver.IBeatSaverSongCharacteristic): number {
let diff;
switch (diff_name) {
case "Easy": diff = characteristic.difficulties.easy; break;
case "Normal": diff = characteristic.difficulties.normal; break;
case "Hard": diff = characteristic.difficulties.hard; break;
case "Expert": diff = characteristic.difficulties.expert; break;
case "Expert+": diff = characteristic.difficulties.expertPlus; break;
}
export function get_notes_count(diff_name: string, characteristic: string, version: beatsaver.IBeatSaverSongVersion): number {
const diff = version.diffs.find((d) => (d.characteristic === characteristic && d.difficulty === diff_name));
return diff?.notes ?? -1;
}

Expand Down

0 comments on commit e662537

Please sign in to comment.