Browse Source

tts: cache stickers for 60 seconds

master
vampirefrog 12 months ago
parent
commit
1e5cee115b
  1. 2
      soundcloud.js
  2. 32
      static/tts.js

2
soundcloud.js

@ -21,7 +21,7 @@ class SoundCloud extends SongProvider {
getInfo(url, cb) {
var canonicalUrl = this.getCanonicalUrl(url);
fetch(canonicalUrl)
.then((response) => { return response.text(); } )
.then((response) => { return response.text(); })
.then((html) => {
let match;
if(match = html.match(/catch\([a-z]\)\{\}\}\)\},(\[(.*?)\])\);/)) {

32
static/tts.js

@ -220,6 +220,32 @@ function randomItem(items) {
return items;
}
var stickerCache = {};
var cacheTime = 60 * 1000;
function getStickerInfo(stickerId) {
return new Promise((resolve, reject) => {
let d = new Date();
if(stickerId in stickerCache) {
let dateDiff = d - stickerCache[stickerId].date;
if(dateDiff < cacheTime) {
return resolve(stickerCache[stickerId].result);
}
}
util.loadJSON(config.stickerInfoUrl+'/'+stickerId, function(response) {
let result = response.results && response.results[0] || null;
stickerCache[stickerId] = {
date: d,
result: result
};
resolve(result);
});
});
}
function handleMessage(data) {
switch(data.type) {
case 'chat':
@ -229,7 +255,7 @@ function handleMessage(data) {
var m = text.match(/:emote\/([^/]+)\/([^/]+)\/([^:]+):/);
if(m && m[3] && queue.queue.length < minSpeedupQueueLength) {
util.loadJSON(config.stickerInfoUrl+'/'+m[3], function(response) {
getStickerInfo(m[3]).then((result) => {
var chat = {
id: data.id,
avatar: avatar,
@ -245,10 +271,10 @@ function handleMessage(data) {
]
};
if(response && response.results && response.results[0] && response.results[0].name) {
if(result && result.name) {
chat.platform = data.platform;
chat.username = data.username;
chat.speak = chat.content[1].alt = response.results[0].name;
chat.speak = chat.content[1].alt = result.name;
}
appendChat(chat);

Loading…
Cancel
Save