Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TwitchLive {
  2.  
  3.   constructor() {
  4.  
  5.     /**
  6.      * Le CLIENT-ID de l'application pour utiliser l'API de Twitch.
  7.      * @type {String}
  8.      */
  9.     this.CLIENT_IDS = [
  10.       "yn51bimnrjgd9mkqerx99ns1efn0wh",
  11.     ];
  12.  
  13.     /**
  14.      * L'URL a appeler pour avoir les infos sur un stream.
  15.      * @type {string}
  16.      */
  17.     this.API_URL_STREAM = 'https://api.twitch.tv/kraken/streams/?channel=27115917';
  18.  
  19.     /**
  20.      * L'URL du stream Twitch.
  21.      * @type {string}
  22.      */
  23.     this.URL_STREAM = 'https://www.twitch.tv/teyz_';
  24.  
  25.     /**
  26.      * @type {Boolean|null}
  27.      */
  28.     this.isOnline = null;
  29.  
  30.     this.setupBadgeBehavior();
  31.     this.setUpNotificationsBehavior();
  32.     this.updateStreamState();
  33.   }
  34.  
  35.   /**
  36.    * Configure le comportement du badge.
  37.    */
  38.   setupBadgeBehavior() {
  39.     chrome.browserAction.onClicked.addListener(_ => this._openStream())
  40.   }
  41.  
  42.   /**
  43.    * Configure le comportement des notifications.
  44.    */
  45.   setUpNotificationsBehavior() {
  46.     chrome.notifications.onClicked.addListener(_ => this._openStream());
  47.   }
  48.  
  49.   /**
  50.    * Ouvre la page du stream Twitch dans un nouvel onglet.
  51.    * @private
  52.    */
  53.   _openStream() {
  54.     chrome.tabs.create({active: true, url: this.URL_STREAM})
  55.   }
  56.  
  57.   /**
  58.    * Met à  jour l'état en-ligne/hors-ligne d'un stream en particulier, en appelant l'API Twitch.
  59.    */
  60.   updateStreamState() {
  61.     const clientId = this.CLIENT_IDS[Math.floor(Math.random() * this.CLIENT_IDS.length)];
  62.     const headers = {
  63.       'Accept': 'application/vnd.twitchtv.v5+json',
  64.       'Client-ID': clientId
  65.     };
  66.  
  67.     // Connexion à  l'API de Twitch
  68.     fetch(this.API_URL_STREAM, {headers})
  69.       .then(response => {
  70.         if(response.ok) {
  71.           response.json().then(json => this.handleResponse(json))
  72.         } else {
  73.           console.error(new Date(), "Mauvaise réponse du réseau");
  74.         }
  75.       })
  76.       .catch(error => {
  77.         console.error(new Date(), "Erreur avec la fonction fetch()", error)
  78.       });
  79.  
  80.     this.prepareNextUpdate();
  81.   }
  82.  
  83.   /**
  84.    * Traite les données issues d'une requête à  l'API Twitch.
  85.    * @param {Object} json Les données en JSON retournées par l'API Twitch
  86.    */
  87.   handleResponse(json) {
  88.     let isOnline = json['streams'].length > 0;
  89.  
  90.     if (this.isOnline === false && isOnline === true) {
  91.       chrome.notifications.create('Teyz_', {
  92.         type: 'basic',
  93.         iconUrl: '',
  94.         title: 'Teyz_ est actuellement en live !',
  95.         message: json['streams'][0]['channel']['status'].trim()
  96.       });
  97.     }
  98.  
  99.     isOnline ? this.showStreamerOnline() : this.showStreamerOffline();
  100.     this.isOnline = isOnline;
  101.   }
  102.  
  103.   /**
  104.    * Prépare la prochaine mise à  jour de l'état du stream.
  105.    */
  106.   prepareNextUpdate() {
  107.     // Entre 1 et 3 minutes
  108.     const timeToWaitBeforeNextUpdate = (Math.random() * 2 + 1) * 60 * 1000;
  109.     setTimeout(_ => this.updateStreamState(), timeToWaitBeforeNextUpdate)
  110.   }
  111.  
  112.   /**
  113.    * Affiche le streamer en-ligne.
  114.    */
  115.   showStreamerOnline() {
  116.     chrome.browserAction.setBadgeText({text: 'ON'});
  117.     chrome.browserAction.setBadgeBackgroundColor({color: 'green'})
  118.   }
  119.  
  120.   /**
  121.    * Affiche le streamer hors-ligne.
  122.    */
  123.   showStreamerOffline() {
  124.     chrome.browserAction.setBadgeText({text: 'OFF'});
  125.     chrome.browserAction.setBadgeBackgroundColor({color: 'gray'})
  126.   }
  127. }
  128.  
  129. window.TwitchLive = new TwitchLive();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement