Skip to content

Commit

Permalink
add feauture to use new genre from custom
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav committed Nov 25, 2023
1 parent f2235bb commit e0b0a31
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
7 changes: 3 additions & 4 deletions web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ServerMusic struct {
Genre string `json:"genre"`
MaxRand int `json:"max_rand"`
NumIter int `json:"num_iter"`
IsVoice bool `json:"is_voice`
}

func main() {
Expand Down Expand Up @@ -68,15 +69,13 @@ func main() {
numVoice := make(map[string][]string)

for music := range c {
if music.Genre == "custom" {
genreMap["custom"] = append(genreMap["custom"], music.URL)
if !music.IsVoice {
genreMap[music.Genre] = append(genreMap[music.Genre], music.URL)
} else {
numVoice[music.Genre] = append(numVoice[music.Genre], music.URL)
}
}

fmt.Printf("Files today: %d\n", len(genreMap["custom"]))

for key, val := range numVoice {
fmt.Printf("Key: %s, Num Values: %d\n", key, len(val))
}
Expand Down
87 changes: 62 additions & 25 deletions web/static/js/defer.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
// GET MULTI-SELECT OPTIONS //
// Load the radio data from radio.json
fetch('/static/radio/radio.json')
.then(response => response.json())
.then(data => {
let optionsSet = {};
data.forEach(item => {
optionsSet[item.genre] = item.name;
});
function fetchAndProcessData(jsonUrl, source) {
return fetch(jsonUrl)
.then(response => response.json())
.then(data => {
let optionsSet = {};
data.forEach(item => {
if (!item.is_voice) {
optionsSet[item.genre] = { name: item.name, source: source };
}
});

let options = [`<option value="custom">Wladradchenko</option>`];
for (let genre in optionsSet) {
options.push(`<option value="${genre}">${optionsSet[genre]}</option>`);
}
let options = [];
for (let genre in optionsSet) {
options.push(`<option value="${genre}" data-source="${optionsSet[genre].source}">${optionsSet[genre].name}</option>`);
}

return options;
})
.catch(error => {
console.error('Error fetching radio data:', error);
return [];
});
}

Promise.all([fetchAndProcessData('/static/radio/custom.json', 'custom'), fetchAndProcessData('/static/radio/radio.json', 'radio')])
.then(results => {
let combinedOptions = results.reduce((acc, cur) => acc.concat(cur), []);

let multiSelect = document.querySelector('#name-filter');
let rangeSelect = document.createRange();
let fragmentSelect = rangeSelect.createContextualFragment(options.join('\n'));
let fragmentSelect = rangeSelect.createContextualFragment(combinedOptions.join('\n'));
multiSelect.appendChild(fragmentSelect);
})
.catch(error => {
console.error('Error fetching radio data:', error);

// Set the first option as selected by default if none is selected
if (!multiSelect.querySelector('option[selected]')) {
multiSelect.querySelector('option').setAttribute('selected', true);
}
});


// Function to change the option's text
function changeOptionTextCustom(option) {
const originalText = option.textContent;
if (option.dataset.source === 'custom') {
option.textContent = 'Wladradchenko';
setTimeout(() => {
option.textContent = originalText;
}, 5000);
}
}

// Interval function to animate text change for the selected option
setInterval(() => {
const selectedOption = document.querySelector('#name-filter option:checked');
changeOptionTextCustom(selectedOption);
}, 5000);
// GET MULTI-SELECT OPTIONS //


Expand Down Expand Up @@ -54,24 +89,26 @@ fetch('/static/assets/')
.then(html => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const backgroundNames= Array.from(doc.querySelectorAll('a'))
const backgroundNames = Array.from(doc.querySelectorAll('a'))
.map(link => '/static/assets/' + link.textContent.trim());

let backgroundImg = backgroundNames[Math.floor(Math.random() * backgroundNames.length)];
// Get the img element
const imgElement = document.querySelector('.background-img');

// Get the src attribute value
imgElement.src = `${backgroundImg}`;

// Check if the select element has options
const nameFilterSelect = document.querySelector('#name-filter');
if (nameFilterSelect && nameFilterSelect.options.length > 0) {
imgElement.addEventListener('load', () => {
setMediaMetadata(null);
});
}

setInterval(() => {
backgroundImg = backgroundNames[Math.floor(Math.random() * backgroundNames.length)];
imgElement.src = `${backgroundImg}`;
}, 60000); // update background every minute (60000 milliseconds)

// Listen for changes to the imgElement.src property
imgElement.addEventListener('load', () => {
setMediaMetadata(null);
});
}, 60000);
})
.catch(error => console.error(error));

// GET RANDOM BACKGROUND //
9 changes: 5 additions & 4 deletions web/static/radio/custom.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{"url": "https://wladradchenko.ru/static/radio.wladradchenko.ru/lofi/", "genre": "custom", "max_rand": 60, "num_iter": 30},
{"url": "https://wladradchenko.ru/static/radio.wladradchenko.ru/voice/rus/", "genre": "rus", "max_rand": 12, "num_iter": 12},
{"url": "https://wladradchenko.ru/static/radio.wladradchenko.ru/voice/eng/", "genre": "eng", "max_rand": 10, "num_iter": 10}
]
{"url": "https://wladradchenko.ru/static/radio.wladradchenko.ru/lofi/", "genre": "custom", "max_rand": 221, "num_iter": 50, "is_voice": false, "name": "Neural Lo-Fi"},
{"url": "https://wladradchenko.ru/static/radio.wladradchenko.ru/techno/", "genre": "techno", "max_rand": 74, "num_iter": 50, "is_voice": false, "name": "Neural Techno"},
{"url": "https://wladradchenko.ru/static/radio.wladradchenko.ru/voice/rus/", "genre": "rus", "max_rand": 10, "num_iter": 10, "is_voice": true, "name": "Neural Voice"},
{"url": "https://wladradchenko.ru/static/radio.wladradchenko.ru/voice/eng/", "genre": "eng", "max_rand": 10, "num_iter": 10, "is_voice": true, "name": "Neural Voice"}
]

0 comments on commit e0b0a31

Please sign in to comment.