-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
73 lines (56 loc) · 1.7 KB
/
app.rb
1
2
3
4
5
6
7
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'rubygems'
require 'bundler/setup'
require 'sinatra/base'
Bundler.require(:default, :test)
require_relative 'lib/setup'
require_relative "lib/echonest_wrapper"
require_relative "lib/itunes_wrapper"
require_relative "lib/html_helpers"
module Echotunes
class App < Sinatra::Base
helpers Sinatra::OutputBuffer::Helpers
helpers Echotunes::HtmlHelpers
def en
@en ||= EchonestWrapper.new
end
def itunes
@itunes ||= ItunesWrapper.new
end
def lookup_itunes_tracks(tracks)
track_ids = tracks.map &:item_id
itunes.find_tracks(track_ids)
end
get '/' do
@catalogs = en.catalogs.select{ |c| c.type == 'song' }
erb :index
end
post "/catalog" do
catalog_id = params[:catalog]
# create catalog
if catalog_id.to_s == '0'
catalog_id = en.create_catalog('Echotunes ' + rand(1_000).to_s).id
end
# TEMP: Just a few songs now
itunes_songs = itunes.all_tracks
ticket = en.add_songs_to_catalog(catalog_id, itunes_songs)
redirect to("catalog/#{catalog_id}?ticket=#{ticket}")
end
get '/catalog/:catalog_id' do
@catalog = en.catalog_profile(params[:catalog_id])
@status = en.ticket_status(params[:ticket])
unless @status.failed.empty?
# Stick this in zeroMQ/Resque
# en.analyze_and_add(params[:catalog_id], lookup_itunes_tracks(@status.failed))
end
erb :catalog
end
post '/playlist/:catalog_id' do
name = params.delete('name')
@songs = en.fetch_playlist(params)
itunes_ids = en.itunes_ids_from_songs(@songs)
itunes.create_playlist(name, itunes_ids)
erb :playlist
end
run! if app_file == $0
end
end