<commit_before>class Track < Sequel::Model
  plugin :timestamps

  def get_rdio
    r = JSON.parse(Track.rdio_client.post('http://api.rdio.com/1/',
      method: 'get',
      keys:   rdio_key,
      extras: "isrcs"  # TODO: better extras
    ).body)['result'][rdio_key]

    {
      isrc:     r['isrcs'][0],
      artist:   r['artist'],
      album:    r['album'],
      name:     r['name'],
      duration: r['duration']
    }
  end

  def get_spotify
    r = JSON.parse(Track.spotify_client.get("tracks/#{spotify_id}").body)

    {
      isrc:       r['external_ids']['isrc'],
      artist:     r['artists'][0]['name'],
      album:      r['album']['name'],
      name:       r['name'],
      duration:   r['duration_ms'] / 1000
    }
  end

  def search_spotify
    qs = [
      "isrc:#{isrc}",
      "track:#{name} artist:#{artist} album:#{album}",
      "track:#{name} artist:#{artist}",
    ]

    @spotify_search_results ||= JSON.parse(ISRC.spotify_client.get("search", params: {
      type: "track",
      q:    q[0]
    }).body)['tracks']['items']
  end

  def match_by_diff_attribute_count
  end

  def self.rdio_client
    # Unauthorized Rdio client
    # http://www.rdio.com/developers/docs/web-service/oauth/ref-signing-requests
    consumer = OAuth::Consumer.new(ENV['RDIO_APP_KEY'], ENV['RDIO_APP_SECRET'], { site: 'http://api.rdio.com' })
    OAuth::AccessToken.new(consumer)
  end

  def self.spotify_client
    # Unauthorized Spotify client
    # https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow
    # Access token generated with `foreman run bin/keys`
    consumer = OAuth2::Client.new(ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_CLIENT_SECRET'], site: 'https://api.spotify.com/v1')
    client = OAuth2::AccessToken.new(consumer, ENV['SPOTIFY_ACCESS_TOKEN'])
  end
end<commit_msg>factor out result to hash method<commit_after>class Track < Sequel::Model
  plugin :timestamps

  def rdio_metadata(r)
    {
      isrc:     r['isrcs'][0],
      artist:   r['artist'],
      album:    r['album'],
      name:     r['name'],
      duration: r['duration']
    }
  end

  def spotify_metadata(r)
    {
      isrc:       r['external_ids']['isrc'],
      artist:     r['artists'][0]['name'],
      album:      r['album']['name'],
      name:       r['name'],
      duration:   r['duration_ms'] / 1000
    }
  end

  def get_rdio
    r = JSON.parse(Track.rdio_client.post('http://api.rdio.com/1/',
      method: 'get',
      keys:   rdio_key,
      extras: "isrcs"  # TODO: better extras
    ).body)['result'][rdio_key]

    rdio_metadata(r)
  end

  def spotify_metadata(r)
    {
      isrc:       r['external_ids']['isrc'],
      artist:     r['artists'][0]['name'],
      album:      r['album']['name'],
      name:       r['name'],
      duration:   r['duration_ms'] / 1000
    }
  end

  def get_spotify
    r = JSON.parse(Track.spotify_client.get("tracks/#{spotify_id}").body)

    spotify_metadata(r)
  end

  def search_spotify
    qs = [
      "isrc:#{isrc}",
      "track:#{name} artist:#{artist} album:#{album}",
      "track:#{name} artist:#{artist}",
    ]

    @spotify_search_results ||= JSON.parse(ISRC.spotify_client.get("search", params: {
      type: "track",
      q:    q[0]
    }).body)['tracks']['items']
  end

  def match_by_total_edit_distance
    search_spotify.each do |i|
    end
  end

  def self.rdio_client
    # Unauthorized Rdio client
    # http://www.rdio.com/developers/docs/web-service/oauth/ref-signing-requests
    consumer = OAuth::Consumer.new(ENV['RDIO_APP_KEY'], ENV['RDIO_APP_SECRET'], { site: 'http://api.rdio.com' })
    OAuth::AccessToken.new(consumer)
  end

  def self.spotify_client
    # Unauthorized Spotify client
    # https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow
    # Access token generated with `foreman run bin/keys`
    consumer = OAuth2::Client.new(ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_CLIENT_SECRET'], site: 'https://api.spotify.com/v1')
    client = OAuth2::AccessToken.new(consumer, ENV['SPOTIFY_ACCESS_TOKEN'])
  end
end
{'fraction_non_alphanumeric': 0.16564115185823283, 'fraction_numerical': 0.006645335958651243, 'mean_word_length': 2.9648780487804878, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '2838507', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1461, 'n_words': 236}