Skip to content

Commit

Permalink
Merge pull request #144 from M0NsTeRRR/master
Browse files Browse the repository at this point in the history
Fix for the hash error on song download (fixes #121)
  • Loading branch information
StarGazer1258 authored Jan 12, 2020
2 parents 2d3e0c5 + 4fd48e2 commit e13ca5b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 38 deletions.
9 changes: 5 additions & 4 deletions src/actions/detailsActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LOAD_DETAILS, CLEAR_DETAILS, SET_DETAILS_LOADING, DISPLAY_WARNING } from './types'
import { SONG_DETAILS } from '../constants/views'
import { BEATSAVER_BASE_URL, BSABER_BASE_URL } from '../constants/urls'

import AdmZip from 'adm-zip'
import { hashAndWriteToMetadata } from './queueActions'
Expand Down Expand Up @@ -60,7 +61,7 @@ export const loadDetailsFromKey = key => (dispatch, getState) => {
})
setView(SONG_DETAILS)(dispatch, getState)
if((/^[a-f0-9]+$/).test(key)) {
fetch(`https://beatsaver.com/api/maps/detail/${key}`)
fetch(`${BEATSAVER_BASE_URL}/api/maps/detail/${key}`)
.then(res => {
if(res.status === 404){
dispatch({
Expand All @@ -74,7 +75,7 @@ export const loadDetailsFromKey = key => (dispatch, getState) => {
})
.then(res => res.json())
.then(details => {
fetch(`http://beatsaver.com${details.downloadURL}`)
fetch(`${BEATSAVER_BASE_URL}${details.downloadURL}`)
.then(res => res.arrayBuffer())
.then(data => {
let zip = new AdmZip(new Buffer(data))
Expand Down Expand Up @@ -105,7 +106,7 @@ export const loadDetailsFromKey = key => (dispatch, getState) => {
}
})
})
fetch(`https://bsaber.com/wp-json/bsaber-api/songs/${key}/ratings`)
fetch(`${BSABER_BASE_URL}/wp-json/bsaber-api/songs/${key}/ratings`)
.then(res => res.json())
.then(bsaberData => {
dispatch({
Expand Down Expand Up @@ -139,4 +140,4 @@ export const loadDetailsFromKey = key => (dispatch, getState) => {
}
})
}
}
}
25 changes: 13 additions & 12 deletions src/actions/modActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SET_MOD_LIST, SET_RESOURCE, SET_LOADING, LOAD_MOD_DETAILS, INSTALL_MOD, SET_SCANNING_FOR_MODS, SET_INSTALLED_MODS, DISPLAY_WARNING, UNINSTALL_MOD, CLEAR_MODS, ADD_TO_QUEUE, UPDATE_PROGRESS, ADD_DEPENDENT, SET_MOD_ACTIVE, ADD_PENDING_MOD, SET_PATCHING, SET_MOD_UPDATE_AVAILABLE, CLEAR_MOD_UPDATES, SET_IGNORE_MOD_UPDATE } from './types'
import { MODS_VIEW, MOD_DETAILS } from '../constants/views'
import { BEATMODS_BASE_URL } from '../constants/urls'

import { BEATMODS, LIBRARY } from '../constants/resources'

Expand Down Expand Up @@ -27,7 +28,7 @@ export const fetchApprovedMods = () => (dispatch, getState) => {
type: SET_LOADING,
payload: true
})
fetch(`https://beatmods.com/api/v1/mod?status=approved&gameVersion=${getState().settings.gameVersion}`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?status=approved&gameVersion=${getState().settings.gameVersion}`)
.then(res => res.json())
.then(beatModsResponse => {
dispatch({
Expand All @@ -54,7 +55,7 @@ export const fetchRecommendedMods = () => (dispatch, getState) => {
let recommendedMods = ['CameraPlus', 'YUR Fit Calorie Tracker', 'SyncSaber', 'Custom Sabers', 'Custom Platforms', 'Custom Avatars', 'BeatSaberTweaks', 'PracticePlugin', 'Counters+']
let mods = []
for(let i = 0; i < recommendedMods.length; i++) {
fetch(`https://beatmods.com/api/v1/mod?name=${encodeURIComponent(recommendedMods[i])}&gameVersion=${getState().settings.gameVersion}`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?name=${encodeURIComponent(recommendedMods[i])}&gameVersion=${getState().settings.gameVersion}`)
.then(res => res.json())
.then(beatModsResponse => {
if(beatModsResponse.length === 0) { recommendedMods.splice(i, 1); return }
Expand Down Expand Up @@ -99,7 +100,7 @@ export const fetchModCategory = category => (dispatch, getState) => {
type: SET_LOADING,
payload: true
})
fetch(`https://beatmods.com/api/v1/mod?category=${ category }&status=approved&gameVersion=${getState().settings.gameVersion}`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?category=${ category }&status=approved&gameVersion=${getState().settings.gameVersion}`)
.then(res => res.json())
.then(beatModsResponse => {
dispatch({
Expand All @@ -123,7 +124,7 @@ export const fetchLocalMods = () => (dispatch, getState) => {
type: SET_LOADING,
payload: true
})
fetch(`https://beatmods.com/api/v1/mod?status=approved&status=inactive`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?status=approved&status=inactive`)
.then(res => res.json())
.then(beatModsResponse => {
let installedMods = getState().mods.installedMods
Expand Down Expand Up @@ -161,7 +162,7 @@ export const fetchActivatedMods = () => (dispatch, getState) => {
type: SET_LOADING,
payload: true
})
fetch(`https://beatmods.com/api/v1/mod?status=approved&status=inactive`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?status=approved&status=inactive`)
.then(res => res.json())
.then(beatModsResponse => {
let activatedMods = getState().mods.installedMods.filter(mod => mod.active === true)
Expand Down Expand Up @@ -195,7 +196,7 @@ export const loadModDetails = modId => (dispatch, getState) => {
type: SET_LOADING,
payload: true
})
fetch(`https://beatmods.com/api/v1/mod`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod`)
.then(res => res.json())
.then(beatModsResponse => {
dispatch({
Expand Down Expand Up @@ -233,7 +234,7 @@ export const installMod = (modName, version, dependencyOf = '') => (dispatch, ge
}
return
}
fetch(`https://beatmods.com/api/v1/mod?status=approved&status=inactive&name=${ encodeURIComponent(modName) }&gameVersion=${ getState().settings.gameVersion }`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?status=approved&status=inactivename=${ encodeURIComponent(modName) }&gameVersion=${ getState().settings.gameVersion }`)
.then(res => res.json())
.then(beatModsResponse => {
let latestVersion = '0.0.0'
Expand Down Expand Up @@ -269,7 +270,7 @@ export const installMod = (modName, version, dependencyOf = '') => (dispatch, ge

// Install Mod
if(mod.downloads.some(version => version.type === 'universal')) {
req = request.get({ url: `https://beatmods.com${mod.downloads.filter(version => version.type === 'universal')[0].url}`, encoding: null }, (err, r, data) => {
req = request.get({ url: `${BEATMODS_BASE_URL}${mod.downloads.filter(version => version.type === 'universal')[0].url}`, encoding: null }, (err, r, data) => {
if(err) {
dispatch({
type: DISPLAY_WARNING,
Expand Down Expand Up @@ -319,7 +320,7 @@ export const installMod = (modName, version, dependencyOf = '') => (dispatch, ge
} else {
let installationType = getState().settings.installationType
if(mod.downloads.some(version => version.type === installationType)) {
req = request.get({ url: `https://beatmods.com${mod.downloads.filter(version => version.type === installationType)[0].url}`, encoding: null }, (err, r, data) => {
req = request.get({ url: `${BEATMODS_BASE_URL}${mod.downloads.filter(version => version.type === installationType)[0].url}`, encoding: null }, (err, r, data) => {
if(err) {
dispatch({
type: DISPLAY_WARNING,
Expand Down Expand Up @@ -517,7 +518,7 @@ export const checkInstalledMods = () => (dispatch, getState) => {
let md5sum = crypto.createHash('md5')
md5sum.update(data)
let hash = md5sum.digest('hex')
fetch(`https://beatmods.com/api/v1/mod?hash=${hash}`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?hash=${hash}`)
.then(res => res.json())
.then(beatModsResponse => {
if(beatModsResponse.length > 0) {
Expand Down Expand Up @@ -572,7 +573,7 @@ export const checkInstalledMods = () => (dispatch, getState) => {
let md5sum = crypto.createHash('md5')
md5sum.update(data)
let hash = md5sum.digest('hex')
fetch(`https://beatmods.com/api/v1/mod?hash=${hash}`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?hash=${hash}`)
.then(res => res.json())
.then(beatModsResponse => {
if(beatModsResponse.length > 0) {
Expand Down Expand Up @@ -671,7 +672,7 @@ export const checkModsForUpdates = () => (dispatch, getState) => {
})
let installedMods = getState().mods.installedMods
for(let m = 0; m < installedMods.length; m++) {
fetch(`https://beatmods.com/api/v1/mod?status=approved&name=${ installedMods[m].name }&gameVersion=${ getState().settings.gameVersion }`)
fetch(`${BEATMODS_BASE_URL}/api/v1/mod?status=approved&name=${ installedMods[m].name }&gameVersion=${ getState().settings.gameVersion }`)
.then(res => res.json())
.then(beatModsResponse => {
if(beatModsResponse.length === 0) return
Expand Down
7 changes: 4 additions & 3 deletions src/actions/playlistsActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FETCH_LOCAL_PLAYLISTS, LOAD_NEW_PLAYLIST_IMAGE, SET_NEW_PLAYLIST_OPEN, SET_PLAYLIST_PICKER_OPEN, CLEAR_PLAYLIST_DIALOG, LOAD_PLAYLIST_DETAILS, LOAD_PLAYLIST_SONGS, CLEAR_PLAYLIST_DETAILS, SET_PLAYLIST_EDITING, SET_LOADING, DISPLAY_WARNING } from './types'
import { PLAYLIST_LIST, PLAYLIST_DETAILS } from '../constants/views'
import { BEATSAVER_BASE_URL } from '../constants/urls'
import { defaultPlaylistIcon } from '../b64Assets'
import { hashAndWriteToMetadata } from './queueActions'
import { setView } from './viewActions'
Expand Down Expand Up @@ -196,7 +197,7 @@ export const loadPlaylistDetails = playlistFile => (dispatch, getState) => {
})
})
} else {
fetch(`https://beatsaver.com/api/maps/by-hash/${playlist.songs[i].hash}`)
fetch(`${BEATSAVER_BASE_URL}/api/maps/by-hash/${playlist.songs[i].hash}`)
.then(res => res.json())
.then(song => {
song.coverURL = `https://beatsaver.com/${song.coverURL}`
Expand All @@ -213,10 +214,10 @@ export const loadPlaylistDetails = playlistFile => (dispatch, getState) => {
})
}
} else {
fetch(`https://beatsaver.com/api/maps/detail/${playlist.songs[i].key}`)
fetch(`${BEATSAVER_BASE_URL}/api/maps/detail/${playlist.songs[i].key}`)
.then(res => res.json())
.then(details => {
details.coverURL = `https://beatsaver.com/${details.coverURL}`
details.coverURL = `${BEATSAVER_BASE_URL}/${details.coverURL}`
if(state.songs.downloadedSongs.some(song => song.hash === details.hash)) {
let file = state.songs.downloadedSongs[state.songs.downloadedSongs.findIndex(song => song.hash === details.hash)].file
fs.readFile(file, 'UTF8', (err, data) => {
Expand Down
15 changes: 8 additions & 7 deletions src/actions/queueActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ADD_TO_QUEUE, CLEAR_QUEUE, UPDATE_PROGRESS, SET_DOWNLOADED_SONGS, SET_DOWNLOADING_COUNT, SET_WAIT_LIST, DISPLAY_WARNING, SET_SCANNING_FOR_SONGS, SET_DISCOVERED_FILES, SET_PROCESSED_FILES } from './types'
import { SONG_LIST } from '../constants/views'
import { BEATSAVER_BASE_URL } from '../constants/urls'
import { isModInstalled, installEssentialMods } from './modActions'
import { setView } from './viewActions'

Expand All @@ -19,7 +20,7 @@ export const downloadSong = (identity) => (dispatch, getState) => {
if(!isModInstalled('SongLoader')(dispatch, getState)) installEssentialMods()(dispatch, getState)
let hash = identity
if(identity) {
fetch(`https://beatsaver.com/api/maps/by-hash/${hash}`)
fetch(`${BEATSAVER_BASE_URL}/api/maps/by-hash/${hash}`)
.then(res => res.json())
.then(song => {
hash = song.hash
Expand All @@ -39,7 +40,7 @@ export const downloadSong = (identity) => (dispatch, getState) => {
setTimeout(() => {
document.getElementById('queue-button').classList.remove('notify')
}, 1000)
fetch(`https://beatsaver.com/api/maps/by-hash/${hash}`)
fetch(`${BEATSAVER_BASE_URL}/api/maps/by-hash/${hash}`)
.then(res => res.json())
.then(song => {
let utc = Date.now()
Expand All @@ -49,13 +50,13 @@ export const downloadSong = (identity) => (dispatch, getState) => {
payload: {
utc,
hash: song.hash,
image: `https://www.beatsaver.com${ song.coverURL }`,
image: `${BEATSAVER_BASE_URL}${ song.coverURL }`,
title: song.metadata.songName,
author: song.metadata.songAuthorName
}
})
let req = request.get({
url: `http://www.beatsaver.com${song.downloadURL}`,
url: `${BEATSAVER_BASE_URL}${song.downloadURL}`,
encoding: null
}, (err, r, data) => {
try {
Expand Down Expand Up @@ -237,7 +238,7 @@ export const downloadSong = (identity) => (dispatch, getState) => {
setTimeout(() => {
document.getElementById('queue-button').classList.remove('notify')
}, 1000)
fetch(`https://beatsaver.com/api/maps/by-hash/${hash}`)
fetch(`${BEATSAVER_BASE_URL}/api/maps/by-hash/${hash}`)
.then(res => res.json())
.then(song => {
let utc = Date.now()
Expand All @@ -246,13 +247,13 @@ export const downloadSong = (identity) => (dispatch, getState) => {
payload: {
utc,
hash: song.hash,
image: `https://www.beatsaver.com${ song.coverURL }`,
image: `${BEATSAVER_BASE_URL}${ song.coverURL }`,
title: song.metadata.songName,
author: song.metadata.songAuthorName
}
})
let req = request.get({
url: `https://beatsaver.com/${ song.downloadURL }`,
url: `${BEATSAVER_BASE_URL}/${ song.downloadURL }`,
encoding: null
}, (err, r, data) => {
try {
Expand Down
5 changes: 3 additions & 2 deletions src/actions/searchActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SET_SEARCH_SOURCES, SUBMIT_SEARCH, SET_LOADING } from './types'
import { BEATSAVER_BASE_URL } from '../constants/urls'

const { remote } = window.require('electron')
const fs = remote.require('fs')
Expand Down Expand Up @@ -48,7 +49,7 @@ export const submitSearch = keywords => (dispatch, getState) => {
localResultsReady = true

// BeatSaver Search
fetch('https://beatsaver.com/api/search/text/all?q=' + encodeURIComponent(keywords.replace('/', '\\')))
fetch(`${BEATSAVER_BASE_URL}/api/search/text/all?q=` + encodeURIComponent(keywords.replace('/', '\\')))
.then(res => res.json())
.then(data => {
beatSaverSongs = data.docs
Expand All @@ -68,7 +69,7 @@ export const submitSearch = keywords => (dispatch, getState) => {

// BeatSaver ID Search
if(isId) {
fetch('https://beatsaver.com/api/maps/detail/' + keywords)
fetch(`${BEATSAVER_BASE_URL}/api/maps/detail/` + keywords)
.then(res => { if(res.status !== 200) { beatSaverIdResultsReady = true; return } return res.json() })
.then(data => {
if(!data) { beatSaverIdResultsReady = true; return }
Expand Down
21 changes: 11 additions & 10 deletions src/actions/songListActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FETCH_NEW, FETCH_TOP_DOWNLOADS, FETCH_TOP_FINISHED, FETCH_LOCAL_SONGS, ADD_BSABER_RATING, SET_SCROLLTOP, SET_LOADING, SET_LOADING_MORE, LOAD_MORE, SET_RESOURCE, DISPLAY_WARNING } from './types'
import { SONG_LIST } from '../constants/views'
import { BEATSAVER, LIBRARY } from '../constants/resources'
import { BEATSAVER_BASE_URL, BSABER_BASE_URL } from '../constants/urls'
import { hashAndWriteToMetadata } from './queueActions'
import { setView } from './viewActions'

Expand All @@ -9,9 +10,9 @@ const fs = remote.require('fs')
const path = remote.require('path')

const resourceUrl = {
'BEATSAVER_NEW_SONGS': 'https://beatsaver.com/api/maps/latest',
'BEATSAVER_TOP_DOWNLOADED_SONGS': 'https://beatsaver.com/api/maps/downloads',
'BEATSAVER_TOP_FINISHED_SONGS': 'https://beatsaver.com/api/maps/plays'
'BEATSAVER_NEW_SONGS': `${BEATSAVER_BASE_URL}/api/maps/latest`,
'BEATSAVER_TOP_DOWNLOADED_SONGS': `${BEATSAVER_BASE_URL}/api/maps/downloads`,
'BEATSAVER_TOP_FINISHED_SONGS': `${BEATSAVER_BASE_URL}/api/maps/plays`
}

export const fetchNew = () => (dispatch, getState) => {
Expand All @@ -28,7 +29,7 @@ export const fetchNew = () => (dispatch, getState) => {
type: SET_RESOURCE,
payload: BEATSAVER.NEW_SONGS
})
fetch('https://beatsaver.com/api/maps/latest')
fetch(`${BEATSAVER_BASE_URL}api/maps/latest`)
.then(res => res.json())
.then(data => {
console.log(data)
Expand All @@ -42,7 +43,7 @@ export const fetchNew = () => (dispatch, getState) => {
})
console.log(data);
for(let i = 0; i < data.docs.length; i++) {
fetch(`https://bsaber.com/wp-json/bsaber-api/songs/${data.docs[i].key}/ratings`)
fetch(`${BSABER_BASE_URL}/wp-json/bsaber-api/songs/${data.docs[i].key}/ratings`)
.then(res => res.json())
.then(bsaberData => {
dispatch({
Expand Down Expand Up @@ -74,7 +75,7 @@ export const fetchTopDownloads = () => (dispatch, getState) => {
type: SET_RESOURCE,
payload: BEATSAVER.TOP_DOWNLOADED_SONGS
})
fetch('https://beatsaver.com/api/maps/downloads')
fetch(`${BEATSAVER_BASE_URL}/api/maps/downloads`)
.then(res => res.json())
.then(data => {
dispatch({
Expand All @@ -86,7 +87,7 @@ export const fetchTopDownloads = () => (dispatch, getState) => {
payload: false
})
for(let i = 0; i < data.songs.length; i++) {
fetch(`https://bsaber.com/wp-json/bsaber-api/songs/${data.docs[i].key}/ratings`)
fetch(`${BSABER_BASE_URL}/wp-json/bsaber-api/songs/${data.docs[i].key}/ratings`)
.then(res => res.json())
.then(bsaberData => {
dispatch({
Expand Down Expand Up @@ -118,7 +119,7 @@ export const fetchTopFinished = () => (dispatch, getState) => {
type: SET_RESOURCE,
payload: BEATSAVER.TOP_FINISHED_SONGS
})
fetch('https://beatsaver.com/api/maps/plays')
fetch(`${BEATSAVER_BASE_URL}/api/maps/plays`)
.then(res => res.json())
.then(data => {
dispatch({
Expand All @@ -130,7 +131,7 @@ export const fetchTopFinished = () => (dispatch, getState) => {
payload: false
})
for(let i = 0; i < data.docs.length; i++) {
fetch(`https://bsaber.com/wp-json/bsaber-api/songs/${data.docs[i].key}/ratings`)
fetch(`${BSABER_BASE_URL}/wp-json/bsaber-api/songs/${data.docs[i].key}/ratings`)
.then(res => res.json())
.then(bsaberData => {
dispatch({
Expand Down Expand Up @@ -230,7 +231,7 @@ export const loadMore = () => (dispatch, getState) => {
})
console.log(data)
for(let i = state.songs.songs.length; i < state.songs.songs.length + data.docs.length; i++) {
fetch(`https://bsaber.com/wp-json/bsaber-api/songs/${data.docs[i - state.songs.songs.length].key}/ratings`)
fetch(`${BSABER_BASE_URL}/wp-json/bsaber-api/songs/${data.docs[i - state.songs.songs.length].key}/ratings`)
.then(res => res.json())
.then(bsaberData => {
dispatch({
Expand Down
3 changes: 3 additions & 0 deletions src/constants/urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const BEATSAVER_BASE_URL = 'https://beatsaver.com'
export const BSABER_BASE_URL = 'https://bsaber.com'
export const BEATMODS_BASE_URL = 'https://beatmods.com'

0 comments on commit e13ca5b

Please sign in to comment.