<commit_before>'use strict';

const YT         = require('./client');
const LocalModel = require('../local/models/playlists');

module.exports = class YouTubeModel {
  static history() {
    if (Cache.youtube.history)
      return Cache.fetch('youtube', 'history');

    return LocalModel.loadPlaylist(Paths.youtube_history).then((history) => {
      return Cache.add('youtube', 'history', history);
    });
  }

  static addToHistory(record) {
    return this.history().then((history) => {
      var last_ten = history.items.slice(0, 9);

      if (last_ten.map(i => i.id).includes(record.id))
        return;

      history.items.unshift(record);

      return LocalModel.savePlaylist(history);
    });
  }

  static playlists(token) {
    return this.fetch('playlists', token);
  }

  static likes(token) {
    return this.fetch('likes', token);
  }

  static playlistItems(id) {
    return this.fetch('items', id, true);
  }

  static fetch(action, token_or_id, full) {
    // Early return if we already have fetched the playlist's
    // items or the given action and we are not trying to
    // load a different page.
    if (action == 'items' && Cache.youtube.items.includes(token_or_id))
      return this.findPlaylist(token_or_id);
    else if (Cache.youtube[action] && !token_or_id)
      return Cache.fetch('youtube', 'action');

    return YT[action](token_or_id, full).then((set) => {
      return {
        id:         set.id,
        next_token: set.nextPageToken,
        collection: set.items.map((record) => {
          if (action == 'playlists')<commit_msg>Fix reading existing data from cache for the YouTube section<commit_after>'use strict';

const YT         = require('./client');
const LocalModel = require('../local/models/playlists');

module.exports = class YouTubeModel {
  static history() {
    if (Cache.youtube.history)
      return Cache.fetch('youtube', 'history');

    return LocalModel.loadPlaylist(Paths.youtube_history).then((history) => {
      return Cache.add('youtube', 'history', history);
    });
  }

  static addToHistory(record) {
    return this.history().then((history) => {
      var last_ten = history.items.slice(0, 9);

      if (last_ten.map(i => i.id).includes(record.id))
        return;

      history.items.unshift(record);

      return LocalModel.savePlaylist(history);
    });
  }

  static playlists(token) {
    return this.fetch('playlists', token);
  }

  static likes(token) {
    return this.fetch('likes', token);
  }

  static playlistItems(id) {
    return this.fetch('items', id, true);
  }

  static fetch(action, token_or_id, full) {
    // Early return if we already have fetched the playlist's
    // items or the given action and we are not trying to
    // load a different page.
    if (action == 'items' && Cache.youtube.items.includes(token_or_id))
      return this.findPlaylist(token_or_id);
    else if (Cache.youtube[action] && !token_or_id)
      return Cache.fetch('youtube', action)

    return YT[action](token_or_id, full).then((set) => {
      return {
        id:         set.id,
        next_token: set.nextPageToken,
        collection: set.items.map((record) => {
          if (action == 'playlists')
{'fraction_non_alphanumeric': 0.142991239048811, 'fraction_numerical': 0.0012515644555694619, 'mean_word_length': 3.026448362720403, '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': '3465381', 'n_tokens_mistral': 991, 'n_tokens_neox': 966, 'n_words': 226}