initial commit
This commit is contained in:
27
src/lib/database.js
Normal file
27
src/lib/database.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
const dbName = './db/strimserve.db';
|
||||
|
||||
// Create a new database object and initialize it with the schema
|
||||
const db = new Database(dbName);
|
||||
|
||||
export function getStreams() {
|
||||
const indexData = db.prepare('SELECT id, stream_date, title, tags, length_seconds ' +
|
||||
'FROM Stream ORDER BY id DESC').all();
|
||||
indexData.forEach(stream =>
|
||||
stream['stream_date'] = Date.parse(stream['stream_date'])
|
||||
);
|
||||
return indexData;
|
||||
}
|
||||
|
||||
export function getStreamInfo(streamId) {
|
||||
const streamData = db.prepare("SELECT id, stream_date, filename, " +
|
||||
"format, title, description, tags, " +
|
||||
"length_seconds, tracks FROM Stream " +
|
||||
"WHERE id = ?").get(streamId);
|
||||
streamData['stream_date'] = Date.parse(streamData['stream_date']);
|
||||
streamData['tracks'] = JSON.parse(streamData['tracks']);
|
||||
streamData['tags'] = JSON.parse(streamData['tags']);
|
||||
streamData['tracks_num'] = streamData['tracks'].length;
|
||||
return streamData;
|
||||
}
|
||||
49
src/lib/stores.js
Normal file
49
src/lib/stores.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
export const currentStream = writable({});
|
||||
export const currentSongIndex = writable(0);
|
||||
|
||||
let timestampList;
|
||||
|
||||
// utility
|
||||
|
||||
function locationOf(element, array, start, end) {
|
||||
start = start || 0;
|
||||
end = end || array.length;
|
||||
var pivot = parseInt(start + (end - start) / 2, 10);
|
||||
if (end - start <= 1 || array[pivot] === element) return pivot;
|
||||
if (array[pivot] < element) {
|
||||
return locationOf(element, array, pivot, end);
|
||||
} else {
|
||||
return locationOf(element, array, start, pivot);
|
||||
}
|
||||
}
|
||||
|
||||
// exported methods
|
||||
|
||||
export function getSongAtTime(currentTime) {
|
||||
return locationOf(currentTime, timestampList) - 1;
|
||||
|
||||
}
|
||||
|
||||
// less operations needed when doing regular lookups
|
||||
export function updateCurrentSong(currentTime, songIndex) {
|
||||
function updateCurrentSongRecurse(songIndex) {
|
||||
if (currentTime >= timestampList[songIndex + 2]) {
|
||||
return updateCurrentSongRecurse(songIndex + 1);
|
||||
}
|
||||
else if (currentTime < timestampList[songIndex + 1]) {
|
||||
return updateCurrentSongRecurse(songIndex - 1);
|
||||
}
|
||||
return songIndex;
|
||||
}
|
||||
|
||||
currentSongIndex.set(updateCurrentSongRecurse(songIndex));
|
||||
|
||||
}
|
||||
|
||||
export function updateCurrentStream(stream) {
|
||||
currentStream.set(stream);
|
||||
timestampList = [-Infinity, ...stream.tracks.map(track => track[0]), Infinity];
|
||||
currentSongIndex.set(0);
|
||||
}
|
||||
Reference in New Issue
Block a user