Playback; client Admin; minor changes

This commit is contained in:
Mateusz Pstrucha committed 2020-09-28 20:17:18 +02:00
1 parent 9c2b0fe83f
commit 1e25901663
13 files changed
+559 -381

No files matched your search

+1 -1
View File
@@ -289,7 +289,7 @@ module.exports = {
history: {
get() {
return new Promise((resolve, reject) => {
Previous.find({}, {}, { limit: 20 }).then(previous => {
Previous.find({}).sort({ createdAt: -1 }).limit(20).then(previous => {
resolve(previous)
})
})
+205 -63
View File
@@ -7,7 +7,6 @@ const dotenv = require('dotenv').config(),
fs = require('fs'),
pretty = require('pretty-log'),
io = require('socket.io'),
crypto = require('crypto'),
SpotifyWebApi = require('spotify-web-api-node'),
db = require('./db');
@@ -87,7 +86,7 @@ class User {
function apiSearch(id) {
return new Promise((resolve, reject) => {
if (typeof id != 'string') return reject(new Error('Incorrect id, must by type String'))
spotify.getTrack(id).then(data => {
spotifySearch.getTrack(id).then(data => {
resolve(data.body)
}).catch(err => {
reject(err)
@@ -96,51 +95,73 @@ function apiSearch(id) {
}
function refreshToken() {
spotify.clientCredentialsGrant().then(
function (data) {
pretty.log({
data: `Spotify: new token granted`,
action: 'info'
})
function tokenWrite(content, path) {
fs.writeFile(path, JSON.stringify(content, null, 4), function (err) {
if (err) throw err
})
}
credentials.spotify.lastToken = {
value: data.body['access_token'],
expires: new Date().getTime() + (data.body['expires_in'] * 1e3)
}
function getAccess() {
spotifySearch.clientCredentialsGrant().then(function (data) {
pretty.log({ data: `Spotify: (search) new Access`, action: 'info' })
fs.writeFile('spotify-cred.json', JSON.stringify(credentials.spotify, null, 4), function (err) {
if (err) throw err
})
spotify.setAccessToken(credentials.spotify.lastToken.value)
setTimeout(function () {
refreshToken()
}, data.body['expires_in'] * 1e3)
},
function (err) {
pretty.log({
data: 'Spotify: something went wrong when retrieving an access token',
action: 'error'
});
credentials.spotify.Access = {
value: data.body['access_token'],
expires: new Date().getTime() + (data.body['expires_in'] * 1e3)
}
)
tokenWrite(credentials.spotify, './spotify-cred.json')
spotifySearch.setAccessToken(credentials.spotify.Access.value)
setTimeout(function () {
getAccess()
}, data.body['expires_in'] * 1e3)
}
).catch(err => {
console.error(err)
})
}
function refreshAccess() {
spotifyPlayer.refreshAccessToken().then(function (data) {
pretty.log({ data: `Spotify: (player) new Access`, action: 'info' })
spotifyPlayer.setAccessToken(data.body['access_token']);
credentials.player.Access = {
value: data.body['access_token'],
expires: new Date().getTime() + (data.body['expires_in'] * 1e3)
}
tokenWrite(credentials.player, './player-cred.json')
}
).catch(err => {
console.error(err)
})
}
const credentials = {
spotify: require('./spotify-cred.json'),
player: require('./player-cred.json'),
api: require('./api-cred.json')
}
const Previous = {
tracks: [],
new(tid, played = new Date) {
var previous = { tid: tid, played: played.getTime() }
new(tid, played) {
if (this.tracks[this.tracks.length - 1]) if (this.tracks[this.tracks.length - 1].tid == tid) {
return
}
db.history.add(tid)
var previous = this._create(tid, played)
this.tracks.push(previous)
this._send(previous)
},
_create(tid, played = new Date) {
return { tid: tid, played: played.getTime() }
},
_send(previous) {
server.emit('previous', {
new: previous,
@@ -149,8 +170,11 @@ const Previous = {
async update() {
this.tracks = []
for (previous of await db.history.get()) {
this.new(previous.tid, previous.createdAt)
var previous = this._create(previous.tid, previous.createdAt)
this.tracks.unshift(previous)
this._send(previous)
}
return this.tracks
},
get() {
return {
@@ -159,20 +183,22 @@ const Previous = {
}
},
Player = {
now: {},
current: {},
status: 'paused',
set(track) {
this.now = track
Previous.new(this.now.id)
pretty.log(`Previous: new track`, 2)
set(track, status = this.status) {
this.current = track
this.status = status
Previous.new(this.current.id)
pretty.log(`Player: new track - ${this.current.title}`, 2)
server.emit('player', {
tid: this.now.id,
tid: this.current.id,
status: this.status
})
db.history.add(track.id)
},
update(status) {
if (this.status === status) return
this.status = status
pretty.log(`Player: status update: ${status}`, 2)
server.emit('player', {
status: this.status
})
@@ -290,31 +316,108 @@ const Previous = {
delete this._authed[socket.user.id][socket.id]
this._auth--
}
},
Playback = {
current: {},
_poll: process.env.playback_poll,
_timings: (function (timings) {
let out = []
for (let p of timings) {
out.push([(p[0][0] * 60) + p[0][1], (p[1][0] * 60) + p[1][1]])
}
return out
})(require('./timings.json')),
interval() {
if(this._poll) this.update()
clearTimeout(this._timeout)
this._timeout = setTimeout(function () { Playback.interval() }, this.getDelay() * 1e3)
},
update() {
spotifyPlayer.getMyCurrentPlaybackState().then(data => {
Playback.current = data.body
Playback.checkTrack()
}).catch(err => {
console.error(err)
})
},
checkTrack() {
if (this.current.is_playing === undefined) return Player.update('stopped')
if (this.current.item.id != Player.current.id) {
if (this.current.is_playing) Tracks.get(this.current.item.id).then(track => {
Player.set(track, 'playing')
})
else this.checkPlaying()
} else this.checkPlaying()
},
checkPlaying() {
if (this.current.is_playing ^ Player.status == 'playing') {
Player.update(this.current.is_playing ? 'playing' : 'paused')
}
},
getDelay() {
let date = new Date(),
minutes = date.getMinutes() + (date.getHours() * 60),
delay, last
for (let i in this._timings) {
let [start, end] = this._timings[i]
if (start - minutes <= 0 && end - minutes >= 0) {
delay = 0
break
} else {
if (last < 0 && start - minutes > 0) {
delay = start - minutes
break
}
delay = start - minutes
last = end - minutes
}
}
if (delay < 0) {
delay = 1440 - minutes
}
if (last <= 1 && last >= 0) return 5
if (delay <= 1) return 5
else return (delay - 1) * 60
}
}
var spotify = new SpotifyWebApi({
var spotifySearch = new SpotifyWebApi({
clientId: credentials.spotify.clientId,
clientSecret: credentials.spotify.clientSecret,
redirectUri: "https://radio-rolnik.mtps.pl/logedin"
})
if (new Date().getTime() >= credentials.spotify.lastToken.expires) {
pretty.log({
data: `Spotify: lastToken is expired`,
action: 'info'
clientSecret: credentials.spotify.clientSecret
}),
spotifyPlayer = new SpotifyWebApi({
clientId: credentials.spotify.clientId,
clientSecret: credentials.spotify.clientSecret,
refreshToken: credentials.player.Refresh.value,
redirectUri: 'https://radio-rolnik.mtps.pl/spotify/done'
})
refreshToken()
// search token
if (new Date().getTime() >= credentials.spotify.Access.expires) {
pretty.log({ data: `Spotify: (search) last Access is expired`, action: 'info' })
getAccess(spotifySearch)
} else {
spotify.setAccessToken(credentials.spotify.lastToken.value)
spotifySearch.setAccessToken(credentials.spotify.Access.value)
setTimeout(function () {
pretty.log({
data: `Spotify: current token expired`,
action: 'info'
})
refreshToken()
}, (credentials.spotify.lastToken.expires - new Date().getTime()))
pretty.log({ data: `Spotify: (search) current token expired`, action: 'info' })
getAccess(spotifySearch)
}, (credentials.spotify.Access.expires - new Date().getTime()))
}
// player token
if (new Date().getTime() >= credentials.player.Access.expires) {
pretty.log({ data: `Spotify: (player) last Access is expired`, action: 'info' })
refreshAccess()
} else {
spotifyPlayer.setAccessToken(credentials.player.Access.value)
setTimeout(function () {
pretty.log({ data: `Spotify: (player) current token expired`, action: 'info' })
refreshAccess()
}, (credentials.player.Access.expires - new Date().getTime()))
}
var app = express(),
www = http.createServer(app),
@@ -381,8 +484,21 @@ app.get('/api/player/status', function (req, res) {
}
})
app.get('/api/spotify/login', function (req, res) {
var scopes = ['user-read-private', 'user-read-email', 'user-read-playback-state', 'user-read-currently-playing'],
redirect = spotifyPlayer.createAuthorizeURL(scopes, 'what')
res.end(redirect)
})
app.get('/spotify/done', function (req, res) {
res.end('<script>window.onload=function(){window.exit(location)}</script>')
})
Previous.update().then(tracks => {
Playback.interval()
})
Chart.interval()
Previous.update()
server.on('connection', socket => {
@@ -397,7 +513,7 @@ server.on('connection', socket => {
socket.on('player', function () {
socket.emit('player', {
tid: Player.now.id,
tid: Player.current.id,
status: Player.status
})
})
@@ -476,20 +592,20 @@ server.on('connection', socket => {
if (typeof data.source != 'string') return
if (data.source == 'spotify') {
spotify.searchTracks(data.query)
.then(function (response) {
spotifySearch.searchTracks(data.query)
.then(response => {
socket.emit('results', {
tids: response.body.tracks.items.map(tdata => tdata.id),
total: response.body.tracks.total
})
}, function (err) {
}).catch(err => {
socket.emit('meta', {
type: 'error',
action: 'search',
message: `'${data.query}' not found: ${err.message}`
})
pretty.log({ data: err.message, action: 'error' });
});
})
}
});
@@ -587,6 +703,32 @@ server.on('connection', socket => {
})
})
break;
case 'spotify':
if (data.spotify.code) {
spotifyPlayer.authorizationCodeGrant(data.spotify.code).then(
function (data) {
pretty.log({ data: `Spotify: (player) log in`, action: 'info' })
credentials.player.Access.value = data.body['access_token']
credentials.player.Access.expires = new Date().getTime() + (data.body['expires_in'] * 1e3)
credentials.player.Refresh.value = data.body['refresh_token']
tokenWrite(credentials.player, './player-cred.json')
spotifyPlayer.setAccessToken(data.body['access_token']);
spotifyPlayer.setRefreshToken(data.body['refresh_token']);
},
function (err) {
console.log('Something went wrong!', err);
}
);
}
if (data.spotify.playing) {
spotifyPlayer.getMyCurrentPlaybackState().then(data => {
console.log(data)
}).catch(err => {
console.error(err)
})
}
break;
}
})
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "radio-rolnik",
"version": "1.2.0",
"version": "1.3.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "radio-rolnik",
"version": "1.2.0",
"version": "1.3.0",
"description": "rolniknysa radio",
"main": "index.js",
"scripts": {
View File
Whitespace-only changes.
-40
View File
@@ -1,40 +0,0 @@
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#ca3e47">
<meta name="description" content="Radio Rolnik Nysa">
<title>radio-rolnik</title>
<link rel="stylesheet" href="/admin/index.css">
<link rel="stylesheet" href="/fontello.css">
<script src="/admin/index.js"></script>
<script src="/socket.io/socket.io.js"></script>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
</head>
<body>
<div id="search">
<div class="controls">
<input type="text" id="text">
<input type="date" id="datein">
<input type="date" id="dateout">
<input type="submit" value="search">
<br>
<label for="user">user</label>
<input type="checkbox" id="user">
<label for="track">track</label>
<input type="checkbox" id="track">
<label for="vote">vote</label>
<input type="checkbox" id="vote">
</div>
<div class="list">
<div class="element">
<div class="image"></div>
<div class="title"></div>
</div>
</div>
</div>
<div id="preview"></div>
</body>
</html>
-17
View File
@@ -1,17 +0,0 @@
class Element{
constructor() {
}
el(){
}
}
var Socket
window.onload = function () {
Socket = io(`/admin`, {
autoConnect: true
})
}
+8 -1
View File
@@ -73,8 +73,9 @@ body {
white-space: nowrap;
text-overflow: ellipsis;
border-bottom: solid white 2px;
transition-duration: 1s;
transition-duration: .5s;
transition-property: height,background;
transition-timing-function: ease-in-out;
}
#player.off>.current {
@@ -524,4 +525,10 @@ a:hover {
text-align: center;
padding: .5rem;
cursor: pointer;
}
/* Admin */
.pane.admin {
padding: .5rem;
}
+45 -3
View File
@@ -9,7 +9,8 @@
<title>radio-rolnik</title>
<link rel="stylesheet" href="/index.css">
<link rel="stylesheet" href="fontello.css">
<link rel="stylesheet" href="/tree.min.css">
<link rel="stylesheet" href="/fontello.css">
<script src="/index.js"></script>
<script src="/socket.io/socket.io.js"></script>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
@@ -136,17 +137,55 @@
<div class="value"><a target="_blank" href="/regulamin.html">Regulamin</a></div>
</div>
<div class="section">Facebook
<div class="value"><a target="_blank" href="https://www.facebook.com/Radio-Rolnik-Nysa-116248583541852">Radio Rolnik Nysa</a></div>
<div class="value"><a target="_blank"
href="https://www.facebook.com/Radio-Rolnik-Nysa-116248583541852">Radio Rolnik Nysa</a></div>
</div>
<div class="section">E-Mail
<div class="value"><a target="_blank" href="mailto:radio-rolnik@mtps.pl">radio-rolnik@mtps.pl</a></div>
</div>
<div class="section">Wersja
<div class="value">v1.2.0</div>
<div class="value">v1.3.0</div>
</div>
</div>
</div>
<div class="pane admin">
<div class="search">
<div class="controls">
<input type="text" id="text">
<input type="date" id="datein">
<input type="date" id="dateout">
<input type="submit" value="search" onclick="Admin.send();">
<br>
<label for="search">search</label>
<input type="radio" name='action' id="search" checked>
<label for="get">get</label>
<input type="radio" name='action' id="get">
<br>
<label for="user">user</label>
<input type="checkbox" id="user">
<label for="track">track</label>
<input type="checkbox" id="track">
<label for="vote">vote</label>
<input type="checkbox" id="vote">
</div>
<div class="list">
<div class="element">
<div class="image"></div>
<div class="title"></div>
</div>
</div>
</div>
<div class="preview"></div>
<div class="autoplayer">
<input type="button" value="Spotify Login" onclick="Admin.spotify.login()">
</div>
</div>
</div>
<div id="nav">
@@ -167,6 +206,9 @@
<div class="button" data-target='settings'>
<i class="icon-cog-alt"></i>
<span class="label">Ustawienia</span></div>
<div class="button hidden" data-target='admin'>
<i class="icon-flag"></i>
<span class="label">Admin</span></div>
</div>
</div>
</body>
+285 -253
View File
@@ -95,7 +95,7 @@ class Track {
if (options.played) {
var played = new Date(options.played)
iPlayed.classList.add('played')
iPlayed.append(`${['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'Sb'][played.getDay()]}, ${played.getHours()}:${played.getMinutes()}`)
iPlayed.append(`${['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'Sb'][played.getDay()]}, ${played.getHours()}:${played.getMinutes() < 10 ? '0' : ''}${played.getMinutes()}`)
}
// icons.append(this.votes)
@@ -259,11 +259,11 @@ class Icon {
pretty = {
info(where, text) {
console.log(`%c# ${where}${'\t'.repeat(4 - (((where.length + 2) - (where.length + 2) % 4) / 4))}${text}`, 'color: cyan')
console.log(`%c# ${where}${'\t'.repeat(4 - (((where.length + 2) - (where.length + 2) % 4) / 4))}${text}`, 'color: azure')
}
}
Tracks = {
const Tracks = {
_list: {},
_promises: {},
get(tid) {
@@ -302,255 +302,171 @@ Tracks = {
this._list[tid].flag = flag
if (Preview.track.id == tid) Preview.flags.set(flag)
},
}
},
Hash = {
current: location.hash.slice(1),
set(target) {
location.hash = target
},
change() {
this.current = location.hash.slice(1)
if (this.current.indexOf(':')) this.current = this.current.split(':')[0]
switch (this.current) {
case 'history':
case 'search':
case 'chart':
case 'settings':
case 'admin':
document.title = `radio-rolnik - ${{
history: 'Historia',
search: 'Wyszukaj',
chart: 'Top',
settings: 'Ustawienia',
admin: 'Administracja'
}[this.current]}`
Hash = {
current: location.hash.slice(1),
set(target) {
switch (target) {
case 'preview':
location.hash = this.setTrack(Preview.track.id)
break;
case 'history':
case 'search':
case 'chart':
case 'settings':
location.hash = target
break;
default:
if (target.startsWith('track:')) {
location.hash = target
}
}
},
change() {
this.current = location.hash.slice(1)
if(this.current.indexOf(':')) this.current = this.current.split(':')[0]
switch (this.current) {
case 'history':
case 'search':
case 'chart':
case 'settings':
document.title = `radio-rolnik - ${{
history: 'Historia',
search: 'Wyszukaj',
chart: 'Top',
settings: 'Ustawienia',
}[this.current]}`
Panes.switch(this.current)
gtag('event', 'pageview', {
'page_path': location.pathname + location.hash
});
break;
case 'track':
let tid = this.getTrack()
if (tid == 'undefined') break;
Tracks.get(tid).then(track => {
document.title = `radio-rolnik - "${track.title}"`
if (Preview.track.id != track.id) Preview.goto(track)
Panes.switch(this.current)
gtag('event', 'pageview', {
'page_path': location.pathname + location.hash
});
})
break;
}
},
setTrack(tid) {
return `track:${tid}`
},
getTrack() {
let hash = location.hash.slice(1)
if (!hash.startsWith('track:')) return
return hash.slice(6)
}
}
break;
Vote = {
send(track, flag) {
this._emit(track, flag)
},
_emit(track, flag) {
pretty.info('Vote', `'${flag}' on ${track.title}`)
Socket.emit('vote', {
tid: track.id,
flag: flag
})
}
}
Player = {
current: null,
status: 'stopped',
set(tid, status) {
if (!tid && status) {
this._status(status)
return
}
Tracks.get(tid).then(track => {
this.current = track
Elements.player.current.innerHTML = ''
Elements.player.current.append(track.el())
if (!Object.keys(Preview.track).length) Preview.change(track)
this._status(status)
}).catch(err => {
console.error(err)
})
},
_status(status) {
this.status = status
switch (status) {
case 'playing': this._on(); break;
case 'paused': this._pause(); break;
case 'stopped': this._off(); break;
}
},
_off() {
Elements.player.container.classList.add('off')
Elements.player.volume.change('stop-circle')
},
_pause() {
Elements.player.container.classList.add('off')
Elements.player.volume.change('pause-circle')
},
_on() {
Elements.player.container.classList.remove('off')
Elements.player.volume.change('play-circled')
}
}
Panes = {
current: 'history',
switch(id) {
document.querySelector(`.pane.${this.current}`).classList.remove('active')
document.querySelector(`#nav>.button[data-target=${this.current}]`).classList.remove('active')
document.querySelector(`.pane.${id}`).classList.add('active')
document.querySelector(`#nav>.button[data-target=${id}]`).classList.add('active')
this.current = id
}
}
Previous = {
_list: [],
add(previous) {
pretty.info('Previous', `new: ${previous.tid}`)
Tracks.get(previous.tid).then(track => {
Elements.previous.prepend(track.el({ played: previous.played }))
})
},
refresh(previous) {
pretty.info('Previous', `refresh`)
Elements.previous.innerHTML = ''
this._list = previous
for (let tdata of previous) {
Tracks.get(tdata.tid).then(track => {
Elements.previous.prepend(track.el({ played: tdata.played }))
})
}
}
}
Search = {
list: [],
update(tids, total) {
pretty.info('Search', `${total} results`)
Elements.search.container.innerHTML = ''
Elements.search.info.textContent = `${total} utworów`
for (let tid of tids) {
Tracks.get(tid).then(track => {
this._append(track)
})
}
},
submit() {
var value = Elements.search.input.value
if (value === '') return
Elements.search.info.textContent = ''
Socket.emit('search', {
query: value,
source: 'spotify'
})
},
_append(result) {
this.list.push(result)
Elements.search.container.append(result.el())
}
}
var Preview, Socket, Offline, Auth, Chart;
var Elements;
window.onload = function () {
Elements = {
player: {
container: document.querySelector('#player'),
current: document.querySelector('#player>.current'),
volume: new Icon(document.querySelector('#player>.status>.volume'))
},
panes: {
history: {
pane: document.querySelector('.pane.history'),
button: document.querySelector('#nav>.button.history')
},
search: {
pane: document.querySelector('.pane.search'),
button: document.querySelector('#nav>.button.search')
},
chart: {
pane: document.querySelector('.pane.chart'),
button: document.querySelector('#nav>.button.chart')
},
preview: {
pane: document.querySelector('.pane.preview'),
button: document.querySelector('#nav>.button.preview')
},
settings: {
pane: document.querySelector('.pane.settings'),
button: document.querySelector('#nav>.button.settings')
case 'track':
let tid = this.getTrack()
if (tid == 'undefined') break;
Tracks.get(tid).then(track => {
document.title = `radio-rolnik - "${track.title}"`
if (Preview.track.id != track.id) Preview.goto(track)
gtag('event', 'pageview', {
'page_path': location.pathname + location.hash
});
})
break;
}
},
previous: document.querySelector('#previous'),
search: {
container: document.querySelector('#results'),
info: document.querySelector('.pane.search>.query>.icons>.info'),
input: document.querySelector('.pane.search>.query>.input'),
setTrack(tid) {
return `track:${tid}`
},
preview: {
albumart: document.querySelector('.pane.preview>.albumart'),
title: document.querySelector('.pane.preview>.title'),
artists: document.querySelector('.pane.preview>.artists'),
table: document.querySelector('.pane.preview>.params')
getTrack() {
let hash = location.hash.slice(1)
if (!hash.startsWith('track:')) return
return hash.slice(6)
}
},
Vote = {
send(track, flag) {
this._emit(track, flag)
},
flags: {
up: document.querySelector('.pane.preview>.buttons>.icon-thumbs-up'),
down: document.querySelector('.pane.preview>.buttons>.icon-thumbs-down'),
report: document.querySelector('.pane.preview>.buttons>.icon-flag')
},
offline: document.querySelector('#offline'),
auth: {
prof: document.querySelector('#profilePic'),
username: document.querySelector('#username'),
buttons: document.querySelector('.pane.preview>.buttons'),
params: document.querySelector('.pane.settings>.params'),
fbLogin: document.querySelector('.pane.settings>.actions>[data-target=fb]')
},
chart: document.querySelector('.chart>.list')
}
_emit(track, flag) {
pretty.info('Vote', `'${flag}' on ${track.title}`)
Socket.emit('vote', {
tid: track.id,
flag: flag
})
}
},
Player = {
current: null,
status: 'stopped',
set(tid, status) {
if (!tid && status) {
this._status(status)
return
}
Tracks.get(tid).then(track => {
this.current = track
Elements.player.current.innerHTML = ''
Elements.player.current.append(track.el())
if (!Object.keys(Preview.track).length) Preview.change(track)
this._status(status)
}).catch(err => {
console.error(err)
})
},
_status(status) {
this.status = status
switch (status) {
case 'playing': this._on(); break;
case 'paused': this._pause(); break;
case 'stopped': this._off(); break;
}
},
_off() {
Elements.player.container.classList.add('off')
Elements.player.volume.change('stop-circle')
},
_pause() {
Elements.player.container.classList.add('off')
Elements.player.volume.change('pause-circle')
},
_on() {
Elements.player.container.classList.remove('off')
Elements.player.volume.change('play-circled')
}
},
Panes = {
current: 'history',
switch(id) {
document.querySelector(`.pane.${this.current}`).classList.remove('active')
document.querySelector(`#nav>.button[data-target=${this.current}]`).classList.remove('active')
document.querySelector(`.pane.${id}`).classList.add('active')
document.querySelector(`#nav>.button[data-target=${id}]`).classList.add('active')
this.current = id
}
},
Previous = {
_list: [],
add(previous) {
pretty.info('Previous', `new: ${previous.tid}`)
Tracks.get(previous.tid).then(track => {
Elements.previous.prepend(track.el({ played: previous.played }))
})
},
async refresh(previous) {
pretty.info('Previous', `refresh`)
Elements.previous.innerHTML = ''
this._list = previous
for (let tdata of previous) {
let track = await Tracks.get(tdata.tid)
Elements.previous.prepend(track.el({ played: tdata.played }))
}
}
},
Search = {
list: [],
update(tids, total) {
pretty.info('Search', `${total} results`)
Elements.search.container.innerHTML = ''
Elements.search.info.textContent = `${total} utworów`
for (let tid of tids) {
Tracks.get(tid).then(track => {
this._append(track)
})
}
},
submit() {
var value = Elements.search.input.value
if (value === '') return
Elements.search.info.textContent = ''
Socket.emit('search', {
query: value,
source: 'spotify'
})
},
_append(result) {
this.list.push(result)
Elements.search.container.append(result.el())
}
},
Preview = {
track: {},
flags: {
el: {
up: document.querySelector('.pane.preview>.buttons>.icon-thumbs-up'),
down: document.querySelector('.pane.preview>.buttons>.icon-thumbs-down'),
report: document.querySelector('.pane.preview>.buttons>.icon-flag'),
},
status: null,
_clicked(flag, status = false) {
if (status) Elements.flags[flag].classList.add('clicked')
@@ -596,8 +512,7 @@ window.onload = function () {
Elements.preview.table.innerHTML = ''
Elements.preview.table.append(...this.track.tbody().children)
}
}
},
Offline = {
_status: true,
change(status) {
@@ -613,8 +528,7 @@ window.onload = function () {
_false() {
Elements.offline.classList.add('online')
},
}
},
Auth = {
successful: false,
autologin: Storage.Local.get('autologin') === false ? false : true,
@@ -670,12 +584,16 @@ window.onload = function () {
this.successful = true
Tracks.expireFlags()
if(Preview.track.id) Tracks.getFlag(Preview.track.id)
if (Preview.track.id) Tracks.getFlag(Preview.track.id)
Elements.auth.buttons.classList.remove('disabled')
Elements.auth.prof.style.backgroundImage = `url('https://graph.facebook.com/${this.info.id}/picture?type=large')`
Elements.auth.username.textContent = this.info.name
Elements.auth.fbLogin.classList.add('hidden')
if (this.info.admin > 0) {
document.querySelector(`#nav>.button[data-target=admin]`).classList.remove('hidden')
}
} else {
this.successful = false
Elements.auth.buttons.classList.add('disabled')
@@ -715,10 +633,8 @@ window.onload = function () {
this.finally(false)
Socket.emit('clear', true)
}
}
},
Chart = {
el: document.querySelector('.chart>.list'),
tids: [],
values: {},
serial: null,
@@ -730,15 +646,129 @@ window.onload = function () {
this.values = values
this.serial = serial
this.el.innerHTML = ''
Elements.chart.innerHTML = ''
for (let tid of tids) {
let track = await Tracks.get(tid)
track.votes = values[tid]
track.placement = tids.indexOf(tid) + 1
this.el.append(track.el())
Elements.chart.append(track.el())
if (Preview.track.id == tid) Preview.update()
}
}
},
Admin = {
get: {
track() { },
user() { },
vote() { },
},
spotify: {
login() {
fetch('/api/spotify/login', {
headers: new Headers().set('Authorization', 'Basic ' + btoa(`${'radio-rolnik'}:${'u9023tnjiowrg'}`).toString('base64'))
}).then(response => response.text())
.then(url => {
var loginpage = window.open(url)
loginpage.exit = function(data){
loginpage.close()
data = data.search.slice(1).split('&').map(param => param.split('='))
params = {}
for(let param of data){
params[param[0]] = param[1]
}
Admin.spotify.code(params.code)
}
})
},
code(code){
Socket.emit('admin', {
action: 'spotify',
spotify: {
code: code
}
})
}
},
search(query, tags) {
Socket.emit('admin', {
action: 'search',
search: {
query: query,
tags: tags
}
})
},
receive(data) {
console.log(data)
},
send() {
console.log(Elements.admin.search.query.value)
}
}
var Elements, Socket;
window.onload = function () {
Elements = {
player: {
container: document.querySelector('#player'),
current: document.querySelector('#player>.current'),
volume: new Icon(document.querySelector('#player>.status>.volume'))
},
panes: {
history: {
pane: document.querySelector('.pane.history'),
button: document.querySelector('#nav>.button.history')
},
search: {
pane: document.querySelector('.pane.search'),
button: document.querySelector('#nav>.button.search')
},
chart: {
pane: document.querySelector('.pane.chart'),
button: document.querySelector('#nav>.button.chart')
},
preview: {
pane: document.querySelector('.pane.preview'),
button: document.querySelector('#nav>.button.preview')
},
settings: {
pane: document.querySelector('.pane.settings'),
button: document.querySelector('#nav>.button.settings')
}
},
previous: document.querySelector('#previous'),
search: {
container: document.querySelector('#results'),
info: document.querySelector('.pane.search>.query>.icons>.info'),
input: document.querySelector('.pane.search>.query>.input'),
},
preview: {
albumart: document.querySelector('.pane.preview>.albumart'),
title: document.querySelector('.pane.preview>.title'),
artists: document.querySelector('.pane.preview>.artists'),
table: document.querySelector('.pane.preview>.params')
},
flags: {
up: document.querySelector('.pane.preview>.buttons>.icon-thumbs-up'),
down: document.querySelector('.pane.preview>.buttons>.icon-thumbs-down'),
report: document.querySelector('.pane.preview>.buttons>.icon-flag')
},
offline: document.querySelector('#offline'),
auth: {
prof: document.querySelector('#profilePic'),
username: document.querySelector('#username'),
buttons: document.querySelector('.pane.preview>.buttons'),
params: document.querySelector('.pane.settings>.params'),
fbLogin: document.querySelector('.pane.settings>.actions>[data-target=fb]')
},
chart: document.querySelector('.chart>.list'),
admin: {
search: {
query: document.querySelector('#text')
}
}
}
Socket = io(`/`, {
@@ -792,10 +822,10 @@ window.onload = function () {
text: `Sprawdź "${Preview.track.title}" na radio-rolnik`,
url: location.href,
})
} else if(navigator.clipboard) {
navigator.clipboard.writeText(location.href).then(function() {
} else if (navigator.clipboard) {
navigator.clipboard.writeText(location.href).then(function () {
new Modal('Skopiowano do schowka', `Link do "${Preview.track.title}" został skopiowany do schowka`)
}, function(err) {
}, function (err) {
new Modal('Nie można skopiować do schowka', `Wystąpił błąd podczas kopiowania linku do schowka`)
});
}
@@ -951,6 +981,8 @@ window.onload = function () {
console.log('meta', data)
});
Socket.on('admin', data => console.log(data))
Socket.on('admin', function (data) {
Admin.receive(data)
})
}
+1 -1
View File
@@ -1,5 +1,5 @@
{
"version": "1.2.0",
"version": "1.3.0",
"short_name": "radio-rolnik",
"name": "Radio Rolnik Nysa",
"description": "",
+1
View File
@@ -0,0 +1 @@
ul.ascii{margin:0;padding:0}ul.ascii,ul.ascii ul{margin-left:0;padding-left:0;list-style:none}ul.ascii li{margin:0;padding:0;white-space:nowrap;overflow:hidden}.ascii,.ascii ol{padding-left:0;list-style:none}.ascii li::before{content:""}.ascii li li::before{content:'├──\a0'}.ascii li li li::before{content:'│\a0\a0\a0├──\a0'}.ascii li li li li::before{content:'│\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li li li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li li li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li li li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li li li li:last-child li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0└──\a0'}.ascii li li li li li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li li li:last-child li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li li:last-child li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li li:last-child li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0└──\a0'}.ascii li li li li li li:last-child li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li li li:last-child li:last-child li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0└──\a0'}.ascii li li li li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li li:last-child li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li:last-child li li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li:last-child li li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li:last-child li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li:last-child li li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li li:last-child li li:last-child li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0└──\a0'}.ascii li li li li li:last-child li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0└──\a0'}.ascii li li li li li:last-child li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li li:last-child li:last-child li li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li li:last-child li:last-child li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li li:last-child li:last-child li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0└──\a0'}.ascii li li li li li:last-child li:last-child li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li li:last-child li:last-child li:last-child li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0└──\a0'}.ascii li li li li:last-child::before{content:'│\a0\a0\a0│\a0\a0\a0└──\a0'}.ascii li li li li:last-child li::before{content:'│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0├──\a0'}.ascii li li li li:last-child li li::before{content:'│\a0\a0\a0│\a0\a0\a0\a0\a0\a0\a0│\a0\a0\a0├──\a0'}.ascii li li li li:last-child li li li::before{content:'Line truncated
+11
View File
@@ -0,0 +1,11 @@
[
[[7, 45], [8, 0]],
[[8, 45], [8, 55]],
[[9, 40], [9, 50]],
[[10, 35], [10, 50]],
[[11, 35], [11, 45]],
[[12, 30], [12, 40]],
[[13, 25], [13, 30]],
[[14, 15], [14, 20]],
[[18, 0], [22, 0]]
]