rybufc icon

background.js

rybufc | PRO | 05/31/16 06:17:31 PM UTC | 0 ⭐ | 267 👁️ | Never ⏰ | []
JavaScript |

4.54 KB

|

None

|

0 👍

/

0 👎

$(document).ready(function(){
 
    var keys = ["AIzaSyAobtIM9MXaMsxwYIl3UWppbDU9lN9i0SY", "AIzaSyB8Ye39ZELzbEtOesRAOWPMS3Ya9jRSyh4", "AIzaSyDC1Gs3Y6XLo4TSH2I2SYFimPctgtJNLYw"];
        ytID = '';
        twitch_data = '';
        titleForNtf = '';
        twitchID = '';
        nowOnline = '';
 
    ChannelType = {
        YOUTUBE : 0,
        TWITCH : 1
    };
 
    var liveStatus = new Array();
        liveStatus[ChannelType.TWITCH] = 'offline';
        liveStatus[ChannelType.YOUTUBE] = 'offline';
 
    var NilfgornChannelId = "UCEdCg-dup5LY-v5Y5F7IeTQ";
 
    updateData();
 
 
 
    function getTwitch(){
        $.ajax({
            url: 'https://api.twitch.tv/kraken/streams/nilfgorn',
            type: 'GET',
            async: false,
            complete: function(senddata){
                var data=JSON.stringify(senddata);
                    data=JSON.parse(data);
                    twitch_data=JSON.parse(data.responseText);
                if (twitch_data.stream == null){
                    liveStatus['tw'] = 'offline';
                } else {
                    if (twitchID == twitch_data.stream._id) {
                        liveStatus[ChannelType.TWITCH] = 'offline';
                    } else {
                        liveStatus[ChannelType.TWITCH] = 'online';
                        twitchID = twitch_data.stream._id;
                        titleForNtf = twitch_data.stream.channel.status + ' (' + twitch_data.stream.channel.game + ')';
                    }
                };
            }
        });
    };
 
 
    function sendData(dataToSend)
    {
        var data=JSON.stringify(dataToSend);
            data=JSON.parse(data);
            check_yt_result = JSON.parse(data.responseText);
 
        if (check_yt_result.items[0].id.videoId == ytID) 
        {
            if (cid == NilfgornChannelId) 
            {
                liveStatus[ChannelType.YOUTUBE] = 'offline';
            } 
            else 
            {
                liveStatus[ChannelType.TWITCH] = 'offline';
            }
        } 
        else 
        {
            if (cid == NilfgornChannelId) 
            {
                liveStatus[ChannelType.YOUTUBE] = 'online';
            } 
            else 
            {
                liveStatus[ChannelType.TWITCH] = 'online';
            }
        }
    }
 
    function getYoutubeLive(cid)
    {
        urlLink = 'https://www.googleapis.com/youtube/v3/search?key='+keys[Math.floor((Math.random() * 3))]+'&part=snippet&type=video&channelId='+cid+'&order=date&eventType=' ;
        $.ajax({
            url: urlLink + 'live',
            type: 'GET',
            async: false,
            complete: function(dataToSend){
                var data=JSON.stringify(dataToSend);
                    data=JSON.parse(data);
                    yt_result=JSON.parse(data.responseText);
 
                if (yt_result.pageInfo.totalResults == 0 || nowOnline == yt_result.items[0].id.videoId)
                {
                    if (cid == NilfgornChannelId) 
                    {
                        liveStatus[ChannelType.YOUTUBE] = 'offline';
                    } 
                    else 
                    {
                        liveStatus[ChannelType.TWITCH] = 'offline';
                    }
                }
                else
                {
                    if (cid == NilfgornChannelId) 
                    {
                            liveStatus[ChannelType.YOUTUBE] = 'online';
                            nowOnline =  yt_result.items[0].id.videoId;
                            titleForNtf = yt_result.items[0].snippet.title;
                    } 
                    else 
                    {
                        liveStatus[ChannelType.TWITCH] = 'online';
                        nowOnline =  yt_result.items[0].id.videoId;
                        titleForNtf = yt_result.items[0].snippet.title;
                    };
 
                    ytID = yt_result.items[0].id.videoId;
 
                    $.ajax({
                        url: urlLink + 'completed',
                        type: 'GET',
                        async: false,
                        complete: function(dataToSend){
                            sendData(dataToSend);
                        };
                    });
                }
            }
        });
    };
 
 
 
 
 
    function updateData() {
        
        getTwitch();
 
        if (liveStatus[ChannelType.TWITCH]=='online') {
 
            var mailNotification = new Notification(titleForNtf, {
                tag: "tw",
                body: "Прямо сейчас на Twitch'e",
                icon: "src/img/livetwitch.png"
            });
            mailNotification.onclick = function(){
                window.open("http://twitch.tv/nilfgorn");
                mailNotification.close();
            };
 
        } else {
 
            getYoutubeLive(NilfgornChannelId); // UCEdCg-dup5LY-v5Y5F7IeTQ  UCCCPCZNChQdGa9EkATeye4g
 
            if (liveStatus['yt']=='online') {
 
                var mailNotification = new Notification(titleForNtf, {
                    tag: "yt",
                    body: "Прямо сейчас на YouTube'e",
                    icon: "src/img/liveyoutube.png"
                });
                mailNotification.onclick = function(){
                    window.open("https://youtu.be/" + ytID);
                    mailNotification.close();
                };
 
            } else {
 
                getYoutubeLive(NilfgornChannelId);
 
                if (liveStatus[ChannelType.TWITCH] == 'online') {
 
                    var mailNotification = new Notification(titleForNtf, {
                        tag: "aw",
                        body: "Прямо сейчас на YouTube'e",
                        icon: "src/img/liveyoutube.png"
                    });
                    mailNotification.onclick = function(){
                        window.open("https://youtu.be/" + ytID);
                        mailNotification.close();
                    };
 
                }
            }
        };
        setTimeout(updateData, 1000*60*2); // 2 minutes
    }
});

Comments