Class: MonkeyMusic::Generate::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_music_generate/runner.rb

Instance Method Summary collapse

Constructor Details

#initializeRunner



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/monkey_music_generate/runner.rb', line 8

def initialize
  OptionParser.new do |opts|
    opts.banner = 'Usage: monkeymusic-generate [options]'

    opts.on('-g',
            '--generate TOPLIST_FILE',
            'Generate a user from a toplist file.') do |user|
      @toplist_file = File.join(Dir.getwd, user)
    end

    opts.on('-o',
            '--out OUT_FILE',
            'The file to dump a generated user to.') do |file|
      @out_file = File.join(Dir.getwd, file)
    end

    opts.on('-k', 
            '--app-key KEY', 
            'Path to libspotify application key.') do |key|
      @spotify_appkey_file = File.join(Dir.getwd, key)
    end

    opts.on('-a', 
            '--account ACCOUNT', 
            'Username for a Spotify premium account.') do ||
       = 
    end

    opts.on('-w', '--password PASSWORD', 
            'Password for a Spotify premium account.') do |password|
      @spotify_password = password
    end

    opts.on_tail('-h',
                 '--help', 
                 'Show this message.') do 
      puts opts
      exit
    end
  end.parse!
end

Instance Method Details

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/monkey_music_generate/runner.rb', line 50

def run
  if generate_user?
    # Create user
    user = User.new
    # Connect to libspotify
    #Hallon.load_timeout = 0
    session = Hallon::Session.initialize(IO.read(@spotify_appkey_file))
    session.login!(, @spotify_password)
    # Load toplists
    puts "Loading toplists from #{@toplist_file}..."
    toplist_loader = ToplistLoader.new(@toplist_file)
    toplist_loader.load_for_user!(user)
    # Generate recommendations
    puts "Loading recommendations..."
    loaded_toplists = toplist_loader.loaded_toplists
    recommendation_loader = RecommendationLoader.new(loaded_toplists)
    recommendation_loader.load_for_user!(user)
    ## Disconnect from libspotify
    session.logout!
    # Evaluate recommendations
    puts "Evaluating recommendations..."
    score_system = ScoreSystem.new
    score_system.evaluate_user_recommendations!(user)
    # Dump and print the user
    File.open(@out_file, 'w') do |f|
      # Clean out all commas before writing
      f.write(user.dump.gsub(',', ''))
    end
    puts "====="
    puts "DONE!"
    puts "====="
    puts "Loaded tracks:"
    user.recommendations.group_by(&:tier).sort.each do |k,v| 
      puts "#Tier #{k}:\t#{v.length}"
    end
  end
end