<commit_before>import { Controller } from 'stimulus';

export default class extends Controller {
  static targets = ['title', 'subTitle', 'artwork', 'audio', 'link'];

  connect() {
    fetch(`https://itunes.apple.com/search?term=${this.trackName}+${this.artistName}&country=JP&media=music&entity=song&lang=ja_jp`)
      .then(response => response.json())
      .then(json => {
        if (json.resultCount === 0) {
          return;
        }
        const result = this._findAppropriateResult(json.results);
        this._showResult(result);
      });
  }

  get trackName() {
    return this.data.get('trackName');
  }

  get artistName() {
    return this.data.get('artistName');
  }

  _findAppropriateResult(results) {
    let result = results.find(res => {
      return res.trackName.toLowerCase() === this.trackName.toLowerCase() && res.artistName.toLowerCase() === this.artistName.toLowerCase();
    });

    if (result === undefined) {
      result = results.find(res => res.artistName.toLowerCase() === this.artistName.toLowerCase());
    }

    if (result === undefined) {
      result = results[0];
    }

    return result;
  }

  _showResult(result) {
    this.subTitleTarget.textContent = `${result.collectionName} ${result.trackNumber}`;
    this.titleTarget.textContent = result.trackName;

    this.artworkTarget.setAttribute('src', result.artworkUrl100);
    this.audioTarget.setAttribute('src', result.previewUrl);

    this.linkTargets.forEach(element => element.setAttribute('href', `${result.trackViewUrl}&at=1001lKQU&app=itunes`));

    this.element.classList.remove('d-none');<commit_msg>Add artistName to itunes preview<commit_after>import { Controller } from 'stimulus';

export default class extends Controller {
  static targets = ['title', 'subTitle', 'artwork', 'audio', 'link'];

  connect() {
    fetch(`https://itunes.apple.com/search?term=${this.trackName}+${this.artistName}&country=JP&media=music&entity=song&lang=ja_jp`)
      .then(response => response.json())
      .then(json => {
        if (json.resultCount === 0) {
          return;
        }
        const result = this._findAppropriateResult(json.results);
        this._showResult(result);
      });
  }

  get trackName() {
    return this.data.get('trackName');
  }

  get artistName() {
    return this.data.get('artistName');
  }

  _findAppropriateResult(results) {
    let result = results.find(res => {
      return res.trackName.toLowerCase() === this.trackName.toLowerCase() && res.artistName.toLowerCase() === this.artistName.toLowerCase();
    });

    if (result === undefined) {
      result = results.find(res => res.artistName.toLowerCase() === this.artistName.toLowerCase());
    }

    if (result === undefined) {
      result = results[0];
    }

    return result;
  }

  _showResult(result) {
    this.subTitleTarget.textContent = `${result.collectionName} ${result.trackNumber}`;
    this.titleTarget.textContent = `${result.trackName} / ${result.artistName}`;

    this.artworkTarget.setAttribute('src', result.artworkUrl100);
    this.audioTarget.setAttribute('src', result.previewUrl);

    this.linkTargets.forEach(element => element.setAttribute('href', `${result.trackViewUrl}&at=1001lKQU&app=itunes`));

    this.element.classList.remove('d-none');
{'fraction_non_alphanumeric': 0.16427480916030535, 'fraction_numerical': 0.00549618320610687, 'mean_word_length': 3.7409551374819103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '4618805', 'n_tokens_mistral': 1019, 'n_tokens_neox': 1014, 'n_words': 152}