<commit_before>import fs from 'fs';
import createJSON from './_jsonCreator';
import _ from 'lodash';

class PlaybackAPI {
  constructor() {
    this.PATH = createJSON('playback');
    this.reset();
    this._save();
    if (Settings.get('enableJSONApi', true)) {
      // DEV: Handle windows users running as admin...
      fs.chmodSync(this.PATH, '777');
    }

    this._ev = {};

    this._hook();
  }

  _hook() {
    Emitter.on('change:song', (event, details) => {
      this._setPlaybackSong(details.title, details.artist, details.album, details.art);
    });

    Emitter.on('playback:isPlaying', this._setPlaying.bind(this, true));
    Emitter.on('playback:isPaused', this._setPlaying.bind(this, false));
    Emitter.on('playback:isStopped', this._setPlaying.bind(this, false));

    Emitter.on('change:playback-time', (event, timeObj) => {
      this._setTime(timeObj.current, timeObj.total);
    });
    // we throttle this function because of a bug in gmusic.js
    // ratings are received multiple times here in a couple of ms
    // to avoid writing the file 5+ times we throttle it to 500ms max
    Emitter.on('change:rating', _.throttle((event, details) => {
      this._setRating(details);
    }, 500));

    Emitter.on('change:shuffle', _.throttle((event, mode) => {
      this._setShuffle(mode);
    }), 20);
    Emitter.on('change:repeat', _.throttle((event, mode) => {
      this._setRepeat(mode);
    }), 20);
  }

  reset() {
    this.data = {
      playing: false,
      song: {
        title: null,
        artist: null,
        album: null,
        albumArt: null,
      },
      rating: {
        liked: false,
        disliked: false,
      },
      time: {
        current: 0,
        total: 0,
      },
      songLyrics: null,
      shuffle: 'NO_SHUFFLE',
      repeat: 'NO_REPEAT',
    };
    this._save();
  }

  _save() {
    if (Settings.get('enableJSONApi', true)) {
      fs.writeFileSync(this.PATH, JSON.stringify(this.data, null, 4));
    }
  }

  _setPlaying(isPlaying) {
    this.data.playing = isPlaying;
    this._fire('change:state', isPlaying);
    this._save();
  }

  _setPlaybackSong(title, artist, album, albumArt) {
    this.data.song = {
      title,
      artist,
      album,
      albumArt,
    };
    this._resetRating();
    this.data.songLyrics = null;
    this._fire('change:song', this.data.song);
    this._fire('change:lyrics', this.data.songLyrics);
    this._save();
  }

  _setRating(rating) {<commit_msg>Store the full sized albumArt image instead of 90x90 thumbnail.  Fixes #1012<commit_after>import fs from 'fs';
import createJSON from './_jsonCreator';
import _ from 'lodash';

class PlaybackAPI {
  constructor() {
    this.PATH = createJSON('playback');
    this.reset();
    this._save();
    if (Settings.get('enableJSONApi', true)) {
      // DEV: Handle windows users running as admin...
      fs.chmodSync(this.PATH, '777');
    }

    this._ev = {};

    this._hook();
  }

  _hook() {
    Emitter.on('change:song', (event, details) => {
      this._setPlaybackSong(details.title, details.artist, details.album, details.art);
    });

    Emitter.on('playback:isPlaying', this._setPlaying.bind(this, true));
    Emitter.on('playback:isPaused', this._setPlaying.bind(this, false));
    Emitter.on('playback:isStopped', this._setPlaying.bind(this, false));

    Emitter.on('change:playback-time', (event, timeObj) => {
      this._setTime(timeObj.current, timeObj.total);
    });
    // we throttle this function because of a bug in gmusic.js
    // ratings are received multiple times here in a couple of ms
    // to avoid writing the file 5+ times we throttle it to 500ms max
    Emitter.on('change:rating', _.throttle((event, details) => {
      this._setRating(details);
    }, 500));

    Emitter.on('change:shuffle', _.throttle((event, mode) => {
      this._setShuffle(mode);
    }), 20);
    Emitter.on('change:repeat', _.throttle((event, mode) => {
      this._setRepeat(mode);
    }), 20);
  }

  reset() {
    this.data = {
      playing: false,
      song: {
        title: null,
        artist: null,
        album: null,
        albumArt: null,
      },
      rating: {
        liked: false,
        disliked: false,
      },
      time: {
        current: 0,
        total: 0,
      },
      songLyrics: null,
      shuffle: 'NO_SHUFFLE',
      repeat: 'NO_REPEAT',
    };
    this._save();
  }

  _save() {
    if (Settings.get('enableJSONApi', true)) {
      fs.writeFileSync(this.PATH, JSON.stringify(this.data, null, 4));
    }
  }

  _setPlaying(isPlaying) {
    this.data.playing = isPlaying;
    this._fire('change:state', isPlaying);
    this._save();
  }

  _setPlaybackSong(title, artist, album, albumArt) {
    const fullSizeAlbumArt = albumArt.replace(/=s90-c-e100$/g, '');
    this.data.song = {
      title,
      artist,
      album,
      albumArt: fullSizeAlbumArt,
    };
    this._resetRating();
    this.data.songLyrics = null;
    this._fire('change:song', this.data.song);
    this._fire('change:lyrics', this.data.songLyrics);
    this._save();
  }

  _setRating(rating) {
{'fraction_non_alphanumeric': 0.15561676175486916, 'fraction_numerical': 0.009246507967735589, 'mean_word_length': 2.665465032444124, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5976166', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1675, 'n_words': 345}