-
Notifications
You must be signed in to change notification settings - Fork 0
/
myapp.rb
52 lines (40 loc) · 1.66 KB
/
myapp.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
require 'sinatra'
require 'sinatra/reloader' if development?
require 'dotenv/load'
require 'rspotify'
require 'chartkick'
configure :development, :test do
require 'pry'
end
get '/' do
send_file File.join(settings.public_folder, 'index.html') if params[:playlist_ids].nil?
authenticated = RSpotify.authenticate(ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_CLIENT_SECRET'])
send_file File.join(settings.public_folder, 'index.html') unless authenticated
playlist_ids = params[:playlist_ids].split
tracks = []
artists = []
playlist_ids.each do |playlist_id|
playlist = RSpotify::Playlist.find(nil, playlist_id)
tracks += playlist.tracks
artists += playlist.tracks.map { |track| track.artists.first.name }.uniq
end
tracks.flatten
track_ids = tracks.map(&:id)
duplicated_track_ids = track_ids.select { |id| track_ids.count(id) > 1 }
common_artists = artists.select { |artist| artists.count(artist) > 1 }
track_id_counts = Hash.new(0)
duplicated_track_ids.each { |track| track_id_counts[track] += 1 }
sorted_track_id_counts = track_id_counts
.sort_by { |_key, value| value }
.reverse.to_h
artists_count = Hash.new(0)
common_artists.each { |artist| artists_count[artist] += 1 }
shared_tracks = tracks.select { |track| duplicated_track_ids.include?(track.id) }.uniq(&:id)
@display_tracks = {}
sorted_track_id_counts.each do |key, value|
track = shared_tracks.detect { |shared_track| shared_track.id == key }
@display_tracks["\"#{track.name}\" by #{track.artists.first.name}"] = value
end
@artists_count = artists_count.sort_by { |_key, value| value }.reverse.to_h
erb :charts
end