-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
96 lines (82 loc) · 3.02 KB
/
app.rb
File metadata and controls
96 lines (82 loc) · 3.02 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require "bundler"
require "sinatra/reloader"
require "csv"
SongDB = []
CSV.foreach("db/songs.csv") do |id, artist, song, genre, decade|
SongDB << { :id => id, :title => song, :artist => artist, :tags => [artist, genre, decade] }
end
Bundler.require
enable :sessions
get "/" do
redirect "/#{SongDB.sample[:id]}"
end
get "/:id" do
session[:tags] ||= {}
@current_song = SongDB.find { |e| e[:id] == params[:id] }
@current_song[:tags].each { |t|
session[:tags][t] ||= 0
session[:tags][t] += 1
}
@recommendations = SongDB.sort_by { |e|
[@current_song[:tags] & e[:tags]].reduce { |s,t| s + session[:tags].fetch(t) }
}.last(20).sample(3)
slim :index
end
get "/explore/:term" do
song = SongDB.select { |e| e[:tags].include?(params[:term]) }.sample
redirect "/#{song[:id]}"
end
__END__
@@index
.row
.col-md-8
.well style="padding-top: 20px"
== %{<iframe width="705" height="396" src="https://www.youtube.com/embed/#{@current_song[:id]}" frameborder="0" allowfullscreen></iframe>}
.well style="padding-top: 20px"
.row
.col-md-4
a href="/#{@recommendations[0][:id]}" style="text-decoration: none; color: inherit;"
img src="http://img.youtube.com/vi/#{@recommendations[0][:id]}/0.jpg" width="100%"
p.text-center
small
strong #{@recommendations[0][:title]}
br
| #{@recommendations[0][:tags].join(" / ")}
.col-md-4
a href="/#{@recommendations[1][:id]}" style="text-decoration: none; color: inherit;"
img src="http://img.youtube.com/vi/#{@recommendations[1][:id]}/0.jpg" width="100%"
p.text-center
small
strong #{@recommendations[1][:title]}
br
| #{@recommendations[1][:tags].join(" / ")}
.col-md-4
a href="/#{@recommendations[2][:id]}" style="text-decoration: none; color: inherit;"
img src="http://img.youtube.com/vi/#{@recommendations[2][:id]}/0.jpg" width="100%"
p.text-center
small
strong #{@recommendations[2][:title]}
br
| #{@recommendations[2][:tags].join(" / ")}
hr
p.text-center
big
== 'Feeling adventurous? <a href="/">Load a random song</a>'
.col-md-4
br
br
.panel.panel-info
.panel-heading Your top interests (according to our creepy robots)
ul.list-group
- session[:tags].sort_by { |k,v| v }.last(15).reverse_each do |k,v|
li.list-group-item
a href="/explore/#{k}" #{k} - #{v}
@@layout
doctype html
html
head
link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"
link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"
body
.container style="padding-top: 20px"
== yield