<commit_before>require "isrc/version"

require 'nokogiri'
require 'mechanize'
require 'logger'

module ISRC
  # NOTE the cont=A is crucial to getting the trick to work
  PPLUK_SESSION_GRAB_URL = 'http://repsearch.ppluk.com/ARSWeb/appmanager/ARS/main'
  PPLUK_AJAX_SEARCH_URL = 'http://repsearch.ppluk.com/ARSWeb/block/send-receive-updates'

  class PPLUK
    def retrieve(opts)
      # default options
      opts = { :title_size => 2 }.merge(opts)

      agent = Mechanize.new
      agent.log = Logger.new "mech.log"
      agent.user_agent_alias = 'Mac Safari'

      isrc_session_init = agent.get PPLUK_SESSION_GRAB_URL
      view_state = self.extract_view_state(isrc_session_init.body)
      ice_session, ice_session_count = self.extract_ice_session(isrc_session_init.body)

      # add the ice_sessions cookie
      ice_cookie = Mechanize::Cookie.new('ice.sessions', "#{ice_session}##{ice_session_count}")
      ice_cookie.path = "/"
      ice_cookie.domain = "repsearch.ppluk.com"
      agent.cookie_jar.add!(ice_cookie)

      # NOTE the online search is a bit funky: adding more to the search make the results worse
      # trying out a three word limit

      title_pieces = extract_song_peices(opts[:title])
      shortened_title = title_pieces.slice(0, [opts[:title_size], title_pieces.size].min).join(' ')

      puts "Title: #{shortened_title}\nArtist: #{opts[:artist]}"

      begin
        isrc_search = agent.post PPLUK_AJAX_SEARCH_URL, {
          'ice.submit.partial' => 'false',
          # 'ice.event.target' => 'T400335881332330323192:ars_form:search_button',
          # 'ice.event.captured' => 'T400335881332330323192:ars_form:search_button',
          # 'ice.event.type' => 'onclick',
          # 'ice.event.alt' => 'false',
          # 'ice.event.ctrl' => 'false',
          # 'ice.event.shift' => 'false',
          # 'ice.event.meta' => 'false',
          # 'ice.event.x' => '47',
          # 'ice.event.y' => '65',
          # 'ice.event.left' => 'false',
          # 'ice.event.right' => 'false',
          'T400335881332330323192:ars_form:search_button' => 'Search',
          'T400335881332330323192:ars_form:isrc_code' => '',
          'T400335881332330323192:ars_form:rec_title_idx' => '',
          'T400335881332330323192:ars_form:rec_title' => shortened_title,
          'T400335881332330323192:ars_form:rec_band_artist_idx' => '',
          'T400335881332330323192:ars_form:rec_band_artist' => opts[:artist],
          'javax.faces.RenderKitId' => 'ICEfacesRenderKit',
          'javax.faces.ViewState' => view_state,
          'icefacesCssUpdates' => '',
          'T400335881332330323192:ars_form' => '',
          'ice.session' => ice_session,
          'ice.view' => view_state,
          'ice.focus' => '',

          # the rand is 19 characters long in the browser's HTTP requests
          'rand' => sprintf('%1.17f', rand)
        }
      rescue Mechanize::ResponseCodeError => e
        agent.log.error "The Stuff #{e.page.body}"
      end

      # creates an array representation of the table:
      #   artist, title, isrc, rights holder, released, time
      isrc_html = Nokogiri::HTML(isrc_search.body)
      @matches = isrc_html.css("table[id='T400335881332330323192:ars_form:searchResultsTable'] tbody tr").map do |m|
        columns = m.css('td')

        # if there is no ISRC don't bother looking
        next if columns[2] == 'Not Supplied'

        # zero length music wont be used
        next if columns[-1] == '0:00sec'

        columns.map &:text
      end

      # if the shortened title did not work, try making it longer
      if @matches.empty? and opts[:title_size] < title_pieces.size
        self.retrieve(opts.merge({:title_size => opts[:title_size] + 1}))
      end
    end

    def match(opts = {})
      return {
        :isrc => 'No Match',
        :delta => -1
      } if @matches.count == 0

      return {
        :artist => @matches.first[0],
        :title => @matches.first[1],
        :isrc => @matches.first[2],
        :delta => 0
      } if @matches.count == 1

      seconds = opts[:time]

      if seconds
        # if string, convert to integer. Format '5:08'
        seconds = timestring_to_integer(seconds) if seconds.class == String
        match_quality = []

        @matches.each do |song_match|<commit_msg>Fixing ISRC query to work with the latest ppl uk website update<commit_after>require "isrc/version"

require 'nokogiri'
require 'mechanize'
require 'logger'

module ISRC
  # NOTE the cont=A is crucial to getting the trick to work
  PPLUK_SESSION_GRAB_URL = 'http://repsearch.ppluk.com/ARSWeb/appmanager/ARS/main'
  PPLUK_AJAX_SEARCH_URL = 'http://repsearch.ppluk.com/ARSWeb/block/send-receive-updates'

  class PPLUK
    def retrieve(opts)
      # default options
      opts = { :title_size => 2 }.merge(opts)

      agent = Mechanize.new
      agent.log = Logger.new "mech.log"
      agent.user_agent_alias = 'Mac Safari'

      isrc_session_init = agent.get PPLUK_SESSION_GRAB_URL
      view_state = self.extract_view_state(isrc_session_init.body)
      ice_session, ice_session_count = self.extract_ice_session(isrc_session_init.body)

      # add the ice_sessions cookie
      ice_cookie = Mechanize::Cookie.new('ice.sessions', "#{ice_session}##{ice_session_count}")
      ice_cookie.path = "/"
      ice_cookie.domain = "repsearch.ppluk.com"
      agent.cookie_jar.add!(ice_cookie)

      # NOTE the online search is a bit funky: adding more to the search make the results worse
      # trying out a three word limit

      title_pieces = extract_song_peices(opts[:title])
      shortened_title = title_pieces.slice(0, [opts[:title_size], title_pieces.size].min).join(' ')

      # puts "Title: #{shortened_title}\nArtist: #{opts[:artist]}"

      begin
        isrc_search = agent.post PPLUK_AJAX_SEARCH_URL, {
          'ice.submit.partial' => 'false',
          # 'ice.event.target' => 'T400335881332330323192:ars_form:search_button',
          # 'ice.event.captured' => 'T400335881332330323192:ars_form:search_button',
          # 'ice.event.type' => 'onclick',
          # 'ice.event.alt' => 'false',
          # 'ice.event.ctrl' => 'false',
          # 'ice.event.shift' => 'false',
          # 'ice.event.meta' => 'false',
          # 'ice.event.x' => '47',
          # 'ice.event.y' => '65',
          # 'ice.event.left' => 'false',
          # 'ice.event.right' => 'false',
          'T5000782701386267377497:ars_form:search_button' => 'Search',
          'T5000782701386267377497:ars_form:isrc_code' => '',
          'T5000782701386267377497:ars_form:rec_title_idx' => '',
          'T5000782701386267377497:ars_form:rec_title' => shortened_title,
          'T5000782701386267377497:ars_form:rec_band_artist_idx' => '',
          'T5000782701386267377497:ars_form:rec_band_artist' => opts[:artist],
          'javax.faces.RenderKitId' => 'ICEfacesRenderKit',
          'javax.faces.ViewState' => view_state,
          'icefacesCssUpdates' => '',
          'T5000782701386267377497:ars_form' => '',
          'ice.session' => ice_session,
          'ice.view' => view_state,
          'ice.focus' => '',

          # the rand is 19 characters long in the browser's HTTP requests
          'rand' => sprintf('%1.17f', rand)
        }
      rescue Mechanize::ResponseCodeError => e
        agent.log.error "Error submitting AJAX request: #{e.page.body}"
      end

      # creates an array representation of the table:
      #   artist, title, isrc, rights holder, released, time
      isrc_html = Nokogiri::HTML(isrc_search.body)
      @matches = isrc_html.css("table[id='T5000782701386267377497:ars_form:searchResultsTable'] tbody tr").map do |m|
        columns = m.css('td')

        # if there is no ISRC don't bother looking
        next if columns[2] == 'Not Supplied'

        # zero length music wont be used
        next if columns[-1] == '0:00sec'

        columns.map &:text
      end

      # if the shortened title did not work, try making it longer
      if @matches.empty? && opts[:title_size] < title_pieces.size
        self.retrieve(opts.merge({:title_size => opts[:title_size] + 1}))
      end
    end

    def match(opts = {})
      return {
        :isrc => 'No Match',
        :delta => -1
      } if @matches.count == 0

      return {
        :artist => @matches.first[0],
        :title => @matches.first[1],
        :isrc => @matches.first[2],
        :delta => 0
      } if @matches.count == 1

      seconds = opts[:time]

      if seconds
        # if string, convert to integer. Format '5:08'
        seconds = timestring_to_integer(seconds) if seconds.class == String
        match_quality = []

        @matches.each do |song_match|
{'fraction_non_alphanumeric': 0.131888402121282, 'fraction_numerical': 0.05556836522942126, 'mean_word_length': 2.7025181391378577, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2788958', 'n_tokens_mistral': 3203, 'n_tokens_neox': 2846, 'n_words': 569}