<commit_before>require 'ejaydj/services/playlist_service'
require 'ejaydj/spotify/client'

module Ejaydj
  class Dj
    attr_accessor :music_client_id,
                  :music_client_secret,
                  :music_user_id,
                  :music_client

    def initialize(attributes={})
      instantiate_variables_from attributes
      yield self if block_given?
    end

    def playlists
      @playlists ||= all_playlists
    end

    def reload!
      @playlists = all_playlists
      @playlists.each {|playlist| playlist.reload! }
    end

    private

    def all_playlists
      playlist_service.all_from(@music_user_id)
    end

    def playlist_service
      @playlist_service ||= Services::PlaylistService.new(music_client: music_client)
    end

    def music_client
      @music_client ||= Spotify::Client.new(
                          client_id: music_client_id,
                          client_secret: music_client_secret)
    end

    def instantiate_variables_from(attributes)
      attributes.each {|key, val| instance_variable_set(:"@#{key}", val) }
    end
  end
end
<commit_msg>Implement playing a song based on a scheduled playlists<commit_after>require 'ejaydj/services/playlist_service'
require 'ejaydj/spotify/client'

module Ejaydj
  class Dj
    PLAYLIST_SCHEDULE = {
      6..11   => :morning_playlists,      # 6AM  - 12PM
      12..17  => :noon_playlists,         # 12PM - 6PM
      18..22  => :night_playlists,        # 6PM  - 11PM
      23..5   => :latenight_playlists     # 11PM - 6AM
    }

    attr_accessor :music_client_id,
                  :music_client_secret,
                  :music_user_id,
                  :music_client,
                  :morning_playlists,
                  :noon_playlists,
                  :night_playlists,
                  :latenight_playlists


    def initialize(attributes={})
      instantiate_variables_from attributes
      yield self if block_given?
    end

    def play_me_a_song(time: Time.now)
      current_playlist(time).next_track
    end

    def current_playlist(time)
      playlist_name = scheduled_playlist(time.strftime('%k').to_i)
      playlist = playlists.select do |playlist|
        playlist.name == playlist_name
      end.first
    end

    def playlists
      @playlists ||= all_playlists
    end

    def reload!
      @playlists = all_playlists
      @playlists.each {|playlist| playlist.reload!}
    end

    private

    def scheduled_playlist(time)
      scheduled_playlists = PLAYLIST_SCHEDULE.select do |schedule, value|
        schedule.cover? time
      end.values.first

      send(scheduled_playlists).sample
    end

    def all_playlists
      playlist_service.all
    end

    def playlist_service
      @playlist_service ||= Services::PlaylistService.new(
                                music_client: music_client,
                                user_id: music_user_id)
    end

    def music_client
      @music_client ||= Spotify::Client.new(
                          client_id: music_client_id,
                          client_secret: music_client_secret)
    end

    def instantiate_variables_from(attributes)
      attributes.each {|key, val| instance_variable_set(:"@#{key}", val) }
    end
  end
end

{'fraction_non_alphanumeric': 0.0766852195423624, 'fraction_numerical': 0.008039579468150896, 'mean_word_length': 2.146887159533074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3660536', 'n_tokens_mistral': 1043, 'n_tokens_neox': 959, 'n_words': 191}