<commit_before>require 'eventmachine'
require 'now_playing_client'
require 'bbc_service_map'

class Avoider
  include Radiodan::Logging

  def initialize(*config)
    @options  = config.shift
    @avoiding_track = @options[:avoiding_track]
    @avoiding = false
  end

  def call(player)
    @player = player

    EM.run do
      @now_playing_client = NowPlayingClient.new

      # When avoid event fired, stash the current
      # station's Track object and wait until the
      # current playing song is finished
      player.register_event :avoid do

        if @avoiding
          logger.info "Already avoiding"
        else
          logger.info "AVOID!"

          @avoided_track = player.playlist.tracks
          logger.info "Current playlist.tracks #{@avoided_track}"
          logger.info "replacement_tracks #{replacement_tracks}"

          station_id        = current_station_id
          now_playing_track = @now_playing_client.now_playing(current_station_id)

          logger.info "current_station_id #{current_station_id}"
          logger.info "now_playing_track #{now_playing_track}"

          track_start_time    = now_playing_track[:start_time].to_i
          track_duration      = now_playing_track['duration']
          time_left_for_track = (track_start_time + track_duration) - Time.now.to_i

          logger.info "track_start_time #{track_start_time}, track_duration #{track_duration}, time_left_for_track #{time_left_for_track}"

          player.playlist.tracks = replacement_tracks
          @avoiding = true

          EventMachine.add_periodic_timer(time_left_for_track) do
            logger.info "Track should be finished, reinstating previous station"
            @player.playlist.tracks = @avoided_track
            @avoiding = false
          end
        end
      end
    end
  end

  # This does a look-up from the BBC-provided stream name
  # through the BBC Service Map to get an ID key that
  # matches those provided by the Now Playing service
  def current_station_id
    BBCRD::ServiceMap.lookup(@player.state.tracks.first.attributes[:Name]).programmes_id
  end

  def replacement_tracks
    @avoiding_track
  end
end<commit_msg>Use one-shot timer for avoiding<commit_after>require 'eventmachine'
require 'now_playing_client'
require 'bbc_service_map'

class Avoider
  include Radiodan::Logging

  def initialize(*config)
    @options  = config.shift
    @avoiding_track = @options[:avoiding_track]
    @avoiding = false
  end

  def call(player)
    @player = player

    EM.run do
      @now_playing_client = NowPlayingClient.new

      # When avoid event fired, stash the current
      # station's Track object and wait until the
      # current playing song is finished
      player.register_event :avoid do

        if @avoiding
          logger.info "Already avoiding"
        else
          logger.info "AVOID!"

          @avoided_track = player.playlist.tracks
          logger.info "Current playlist.tracks #{@avoided_track}"
          logger.info "replacement_tracks #{replacement_tracks}"

          station_id        = current_station_id
          now_playing_track = @now_playing_client.now_playing(current_station_id)

          logger.info "current_station_id #{current_station_id}"
          logger.info "now_playing_track #{now_playing_track}"

          track_start_time    = now_playing_track[:start_time].to_i
          track_duration      = now_playing_track['duration']
          time_left_for_track = (track_start_time + track_duration) - Time.now.to_i

          logger.info "track_start_time #{track_start_time}, track_duration #{track_duration}, time_left_for_track #{time_left_for_track}"

          player.playlist.tracks = replacement_tracks
          @avoiding = true

          EventMachine.add_timer(time_left_for_track) do
            logger.info "Track should be finished, reinstating previous station"
            @player.playlist.tracks = @avoided_track
            @avoiding = false
          end
        end
      end
    end
  end

  # This does a look-up from the BBC-provided stream name
  # through the BBC Service Map to get an ID key that
  # matches those provided by the Now Playing service
  def current_station_id
    BBCRD::ServiceMap.lookup(@player.state.tracks.first.attributes[:Name]).programmes_id
  end

  def replacement_tracks
    @avoiding_track
  end
end
{'fraction_non_alphanumeric': 0.07088781830695114, 'fraction_numerical': 0.0, 'mean_word_length': 2.8790035587188614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6927491', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1218, 'n_words': 305}