<commit_before>const { BrowserWindow } = require('electron');
const urlLib = require('url');
const path = require('path');
const fetch = require('electron-fetch');
const storage = require('electron-json-storage');
const notifier = require('node-notifier');

const playerUrl = 'http://www.xiami.com/play';
const playlistUrl = 'http://www.xiami.com/song/playlist';
const getSongUrl = 'http://www.xiami.com/song/gethqsong';

class XiamiPlayer {
    constructor() {
        this.init();
    }

    init() {
        this.playerWindow = new BrowserWindow({
            height: 768,
            width: 1024,
            resizable: true,
            frame: true,
            autoHideMenuBar: true,
            webPreferences: {
                javascript: true,
                plugins: true,
                webSecurity: false,
                nodeIntegration: false
            }
        });

        // load xiami player page.
        this.playerWindow.loadURL(playerUrl);

        // triggering when user try to close the play window.
        this.playerWindow.on('close', (e) => {
            if (this.playerWindow.isVisible()) {
                e.preventDefault();
                this.playerWindow.hide();
            }
        });

        // triggering after the play window closed.
        this.playerWindow.on('closed', () => {
            this.playerWindow = null;
        });

        // intercept the ajax call response
        this.playerWindow.webContents.on('did-get-response-details', ((event, status, newURL, originalURL) => this.registerResponseFilters(originalURL)));
    }

    // display and focus the player window.
    show() {
        this.playerWindow.show();
        this.playerWindow.focus();
    }

    // hide the play window.
    hide() {
        this.playerWindow.hide();
    }

    // return a boolean to indicate if the window is visible or not
    isVisible() {
        return this.playerWindow.isVisible();
    }

    pause() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.pause-btn').dispatchEvent(new MouseEvent('click'));");
    }

    play() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.play-btn').dispatchEvent(new MouseEvent('click'));");
    }

    next() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.next-btn').dispatchEvent(new MouseEvent('click'));");
    }

    previous() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.prev-btn').dispatchEvent(new MouseEvent('click'));");
    }

    getWebContents() {
        return this.playerWindow.webContents;
    }

    registerResponseFilters(requestUrl) {
        this.updatePlaylistListener(requestUrl);
        this.changeTrackListener(requestUrl);
    }

    updatePlaylistListener(requestUrl) {
        if (requestUrl.startsWith(playlistUrl)) {
            let urlWithPath = urlLib.parse(requestUrl, false);
            delete urlWithPath.search;
            // console.log('Retrieve the playlist from url ' + urlLib.format(urlWithPath));

            // get the cookie, make call with the cookie
            let session = this.playerWindow.webContents.session;
            session.cookies.get({ url : 'http://www.xiami.com' }, (error, cookies) => {
                let cookieString =cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join(';');
                fetch(urlLib.format(urlWithPath), {headers: {'Cookie': cookieString}}).then(res => res.json()).then(json => {

                    // refresh the local storage.
                    json.data.trackList.forEach(track => {
                        // console.log(track.songName);
                        storage.set(track.songId, track, (error) => {
                            if (error) console.log(error);
                        });
                    });
                }).catch((error) => {
                    console.log(error);
                });
            });
        }
    }

    changeTrackListener(requestUrl) {
        if (requestUrl.startsWith(getSongUrl)) {
            let pathname = urlLib.parse(requestUrl).pathname;
            let songId = path.parse(pathname).base;

            storage.get(songId, (error, trackInfo) => {
                if (error) throw error;
                // console.log(trackInfo);
                notifier.emit
                notifier.notify({
                    'title': `Track: ${trackInfo.songName}`,
                    'message': `Artist: ${trackInfo.artist_name}
Album${trackInfo.album_name}`<commit_msg>remove the  testing line in the previous commit<commit_after>const { BrowserWindow } = require('electron');
const urlLib = require('url');
const path = require('path');
const fetch = require('electron-fetch');
const storage = require('electron-json-storage');
const notifier = require('node-notifier');

const playerUrl = 'http://www.xiami.com/play';
const playlistUrl = 'http://www.xiami.com/song/playlist';
const getSongUrl = 'http://www.xiami.com/song/gethqsong';

class XiamiPlayer {
    constructor() {
        this.init();
    }

    init() {
        this.playerWindow = new BrowserWindow({
            height: 768,
            width: 1024,
            resizable: true,
            frame: true,
            autoHideMenuBar: true,
            webPreferences: {
                javascript: true,
                plugins: true,
                webSecurity: false,
                nodeIntegration: false
            }
        });

        // load xiami player page.
        this.playerWindow.loadURL(playerUrl);

        // triggering when user try to close the play window.
        this.playerWindow.on('close', (e) => {
            if (this.playerWindow.isVisible()) {
                e.preventDefault();
                this.playerWindow.hide();
            }
        });

        // triggering after the play window closed.
        this.playerWindow.on('closed', () => {
            this.playerWindow = null;
        });

        // intercept the ajax call response
        this.playerWindow.webContents.on('did-get-response-details', ((event, status, newURL, originalURL) => this.registerResponseFilters(originalURL)));
    }

    // display and focus the player window.
    show() {
        this.playerWindow.show();
        this.playerWindow.focus();
    }

    // hide the play window.
    hide() {
        this.playerWindow.hide();
    }

    // return a boolean to indicate if the window is visible or not
    isVisible() {
        return this.playerWindow.isVisible();
    }

    pause() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.pause-btn').dispatchEvent(new MouseEvent('click'));");
    }

    play() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.play-btn').dispatchEvent(new MouseEvent('click'));");
    }

    next() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.next-btn').dispatchEvent(new MouseEvent('click'));");
    }

    previous() {
        this.playerWindow.webContents.executeJavaScript("document.querySelector('.prev-btn').dispatchEvent(new MouseEvent('click'));");
    }

    getWebContents() {
        return this.playerWindow.webContents;
    }

    registerResponseFilters(requestUrl) {
        this.updatePlaylistListener(requestUrl);
        this.changeTrackListener(requestUrl);
    }

    updatePlaylistListener(requestUrl) {
        if (requestUrl.startsWith(playlistUrl)) {
            let urlWithPath = urlLib.parse(requestUrl, false);
            delete urlWithPath.search;
            // console.log('Retrieve the playlist from url ' + urlLib.format(urlWithPath));

            // get the cookie, make call with the cookie
            let session = this.playerWindow.webContents.session;
            session.cookies.get({ url : 'http://www.xiami.com' }, (error, cookies) => {
                let cookieString =cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join(';');
                fetch(urlLib.format(urlWithPath), {headers: {'Cookie': cookieString}}).then(res => res.json()).then(json => {

                    // refresh the local storage.
                    json.data.trackList.forEach(track => {
                        // console.log(track.songName);
                        storage.set(track.songId, track, (error) => {
                            if (error) console.log(error);
                        });
                    });
                }).catch((error) => {
                    console.log(error);
                });
            });
        }
    }

    changeTrackListener(requestUrl) {
        if (requestUrl.startsWith(getSongUrl)) {
            let pathname = urlLib.parse(requestUrl).pathname;
            let songId = path.parse(pathname).base;

            storage.get(songId, (error, trackInfo) => {
                if (error) throw error;
                // console.log(trackInfo);
                notifier.notify({
                    'title': `Track: ${trackInfo.songName}`,
                    'message': `Artist: ${trackInfo.artist_name}
Album${trackInfo.album_name}`
{'fraction_non_alphanumeric': 0.13336980306345733, 'fraction_numerical': 0.0015317286652078775, 'mean_word_length': 2.05005005005005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1187500', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2368, 'n_words': 449}