Skip to content

Commit

Permalink
Merge pull request #2 from nspu/master
Browse files Browse the repository at this point in the history
add delay(10min) and check stream_type is live
  • Loading branch information
JeremiePat authored May 4, 2018
2 parents e3eff7d + cd56d30 commit e768907
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
accropolis-extension.zip
79 changes: 53 additions & 26 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
(function (browser) {


(function(browser) {

// Useful constants
// --------------------------------------------------------------------------
const TWITCH_ID = "gjds1hg0hy0zanu764903orz2adzsy";
const TWITCH_ID = "gjds1hg0hy0zanu764903orz2adzsy";
const TWITCH_URL = "https://api.twitch.tv/kraken/streams/accropolis?client_id=" + TWITCH_ID;
const DELAY = 10; // minute
const REFRESH_TIME = 2 * 60 * 1000; //2min

// Global status
// --------------------------------------------------------------------------
Expand All @@ -18,23 +22,23 @@
async function openTab() {
console.log("Opening tab");
await browser.tabs.create({
url: isLive ? "https://www.twitch.tv/accropolis"
: "http://accropolis.fr"
url: isLive ? "https://www.twitch.tv/accropolis" : "http://accropolis.fr"
})
}

// Call the Twitch API to check is a liveis in progress
//
// @return { Promise => Boolean }
async function checkLiveStatus() {
var data = await fetch(TWITCH_URL).then((data) => { return data.json() });

return Boolean(
data.stream
&& data.stream.channel
&& data.stream.channel.status
&& data.stream.channel.status.indexOf("[Rediffusions]") === -1
);
var data = await fetch(TWITCH_URL).then((data) => {
return data.json()
});
var isOn = Boolean(data.stream //stream is online
&&
data.stream.stream_type === "live") //the stream is live https://dev.twitch.tv/docs/v5/reference/streams/#get-live-streams


return isOn ? data : null
}

// Update the browser action badge
Expand All @@ -47,38 +51,61 @@
// Display a notification indicating a live is in progress
function setNotification() {
// MS Edge doesn't support notifications yet
if (!browser.notifications) { return; }
if (!browser.notifications) {
return;
}

browser.notifications.create("AccropolisLive", {
type : "basic",
title : browser.i18n.getMessage("title"),
message : browser.i18n.getMessage("notification"),
iconUrl : "icons/accropolis.svg",
isClickable : true
type: "basic",
title: browser.i18n.getMessage("title"),
message: browser.i18n.getMessage("notification"),
iconUrl: "icons/accropolis.svg",
isClickable: true
})
}

// Update the extension status on a regular basis
//
// The extension check if a live is in progress or not every 2min
// Timeout the start of ([DELAY] min + 1 sec)
//
// @return { Promise }
async function onLiveChange() {
var isOn = await checkLiveStatus();
var data = await checkLiveStatus()
var isOn = data != null
var startWithDelay = 0
var now = 0
var delay = 0

if(isOn && !isLive){
startWithDelay = new Date(data.stream.created_at).getTime() + DELAY * 60 * 1000 // [DELAY] * 1 min
now = new Date().getTime()

if (startWithDelay + 1000 > now) {
isOn = false
delay = startWithDelay - now
}else{
delay = REFRESH_TIME
}
}else{
delay = REFRESH_TIME
}

if (isLive !== isOn) {
isLive = isOn;
setBadgeText(isLive);

if(isLive) { setNotification() }
if (isLive) {
setNotification()
}
}

setTimeout(onLiveChange, 120000)
setTimeout(onLiveChange, delay)
}


// Basic set up
// --------------------------------------------------------------------------
browser.browserAction.setBadgeBackgroundColor({ color: "#07D21F" });
browser.browserAction.setBadgeBackgroundColor({
color: "#07D21F"
});

// Set up events
// --------------------------------------------------------------------------
Expand All @@ -91,6 +118,6 @@

// Start checking the live status
// --------------------------------------------------------------------------
onLiveChange();
onLiveChange()

}(window.browser || window.chrome));
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "__MSG_title__",
"version": "1.1",
"version": "1.2",
"default_locale": "fr",

"description": "__MSG_description__",
Expand Down

0 comments on commit e768907

Please sign in to comment.