Question:
I have some jSon info being called and displaying correctly on the website.
The info is currently playing music / artist / time stamp / show name for a radio station.
I want to log this info into a database so it updates each time a new track is played.
I have it sort of working but there are issues.
First off is thst the way it is requires that at least one person is on the website, this is not a major deal as there seems to be always someone!
The database does update BUT it is unpredicatle as to how many results it will show. Sometimes one entry and sometimes many duplicates.
If this was a manual entry feild then I have found the code that will prevent this but as its automated that code wont work or at least i cant make it work as that code requires everything to be a data set.
I may be over thinking the problem but if anyone has ideas or pointers then please help!
Best
Lee
Product:
Wix Editor
Sorry i took a while to reply . . .
Here is the code that lives just on the home page
import { gettherecentlyplayed } from “backend/recentlyplayed”
import { getcurrentlyplaying } from “backend/currentlyplaying”
import { getliveinfo } from “backend/Showinfo”
import wixData from ‘wix-data’;
$w.onReady(function () {
let currentplayimgnow = “”
setInterval(function () {
function allReplace(str, obj) {
for (const x in obj) {
str = str.replace(new RegExp(x, 'g'), obj[x]);
}
return str;
}
const getrecentinfo = async () => {
const recent = await gettherecentlyplayed();
const nowon = await getcurrentlyplaying();
const showname = await getliveinfo();
const artistnowon = nowon.artist
const tracknowon = nowon.title
const timestamped = nowon.start
const show = showname.shows.current.name
let timestamp = allReplace(timestamped.slice(0, -4), { 'T': ' - ' })
//console.log(timestamp)
let toInsert = {
"title": tracknowon,
"artist": artistnowon,
"timestamp": timestamp,
"show": show
};
console.log(toInsert)
if (currentplayimgnow != tracknowon)
wixData.insert("playout", toInsert)
.then((item) => {
// console.log(item); //see item below
// console.log(tracknowon);
})
.catch((err) => {
console.log(err);
});
currentplayimgnow = tracknowon
for (let n = 0; n < 10; n++) {
$w(`#spot${n}`).link = 'https://open.spotify.com/search/' + recent[n].title + " " + recent[n].artist
$w(`#am${n}`).link = 'https://music.amazon.co.uk/search/' + recent[n].artist + " " + recent[n].title
$w(`#apple${n}`).link = 'https://music.apple.com/us/search?term=' + recent[n].artist + " " + recent[n].title
$w(`#you${n}`).link = 'https://www.youtube.com/results?search_query=' + recent[n].artist + " " + recent[n].title
var artistraw = recent[n].artist
var titleraw = recent[n].title
// console.log(artistraw, titleraw)
let artist1 = artistraw.replace(/\(.*?\)/, '')
let title1 = titleraw.replace(/\(.*?\)/, '')
let artist = artist1.replace(/\[.*?\]/, '')
let title = title1.replace(/\[.*?\]/, '')
$w(`#art${n}`).text = artist
$w(`#title${n}`).text = title
if (recent[n].artwork_url == "null") { $w(`#album${n}`).src = "https://static.wixstatic.com/media/1499c9_3e06c37a03964d1b8174103e7b546077~mv2.jpeg" } else { $w(`#album${n}`).src = recent[n].artwork_url }
$w(`#album${n}`).tooltip = recent[n].artist + " " + "- " + recent[n].album
// console.log(artist)
// console.log(title)
}
}
getrecentinfo();
}, 1000);
});
This does work but i get a number of duplicates in the database.
Ive tried to follow other code examples where the database is connected to a dataset but without any luck.
I know what ive written relies completely on someone having the webpage open but for the most part we have few few visitors at all times