-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
61 lines (56 loc) · 1.89 KB
/
main.js
File metadata and controls
61 lines (56 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var PLAYBACK_RATE = 1.2;
var MAX_ATTEMPTS = 3;
// This is a utility I had to write in order to get the redirect location
// of a page. I had trouble using traditional (client-side) methods
// because their https url redirects to http
var HEADER_UTIL_URL = "https://scripts.eg42.net/utils/get_http_header?url="
function getRedirectorURL(now)
{
let timeTag = strftime("%y%m%d-%H", now);
let url = `https://api.bynetcdn.com/Redirector/glz/${timeTag}_News/PD`;
return url;
}
function updateAudioElement(el, url)
{
el.src = url;
el.playbackRate = PLAYBACK_RATE;
el.play();
el.style.display = "block";
}
function findValidRedirect(now, failedAttempts, el, callback)
{
let url = getRedirectorURL(now);
let fullurl = HEADER_UTIL_URL + url;
console.log(fullurl);
fetch(fullurl)
.then(response => response.json())
.then(function(data) {
console.log(data);
if(data.status == "OK" && data.location != null && !data.location.includes("not_found"))
{
console.log(data.location);
let redirect_url = data.location;
//redirect_url = redirect_url.replace("http:","https:");
console.log(redirect_url);
updateAudioElement(el, redirect_url);
callback(true, now);
}
else
{
console.log("did not get valid redirect");
failedAttempts++;
if(failedAttempts >= MAX_ATTEMPTS) {
console.log("giving up");
callback(false, null);
} else {
now.setHours(now.getHours() - 1);
findValidRedirect(now, failedAttempts, el, callback);
}
}
});
}
function setAudioSource(el, callback)
{
let now = new Date();
findValidRedirect(now, 0, el, callback);
}