spotify_rec 1.1 → 1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/spotify_rec +32 -25
- data/lib/login.rb +151 -117
- data/lib/menu.rb +90 -67
- data/lib/my_list.rb +153 -137
- data/lib/playlist.rb +98 -67
- data/lib/rec.rb +56 -44
- data/lib/user.rb +89 -69
- data/public/users.json +1 -13
- metadata +3 -4
- data/lib/run.rb +0 -47
data/lib/rec.rb
CHANGED
@@ -1,11 +1,42 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
class Rec
|
3
4
|
def initialize(user)
|
4
5
|
@user_list = user.mylist
|
5
6
|
@user = user
|
6
7
|
@tracks = []
|
7
8
|
@artists = []
|
8
9
|
@genres = []
|
10
|
+
@prompt = TTY::Prompt.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def amount_of_suggestions
|
14
|
+
system('clear')
|
15
|
+
too_many_items if @user_list.length > 5
|
16
|
+
puts '》 RECOMMENDATIONS 《'
|
17
|
+
amount = @prompt.ask('How many recommendations would you like to generate?'.colorize(:light_green)) do |q|
|
18
|
+
q.in '1-10'
|
19
|
+
q.messages[:range?] = 'Number must be between 1 and 10'
|
20
|
+
end
|
21
|
+
recommend(amount.to_i)
|
22
|
+
end
|
23
|
+
|
24
|
+
def too_many_items
|
25
|
+
puts "Uh oh! You don't have any items in your list yet, so we can't generate any".colorize(:light_red)
|
26
|
+
puts 'recommendations. Please add some before doing this!'.colorize(:light_red)
|
27
|
+
@prompt.keypress('Press any key to return to the previous menu..')
|
28
|
+
menu = Menu.new(@user)
|
29
|
+
menu.display_menu
|
30
|
+
end
|
31
|
+
|
32
|
+
def recommend(num)
|
33
|
+
system('clear')
|
34
|
+
recommendations = recommendations_generate(num)
|
35
|
+
cleaned_recs = recommendations.tracks.map { |t| "#{t.name} by #{t.artists[0].name}" }
|
36
|
+
puts '》 RECOMMENDATIONS 《'.colorize(:light_green)
|
37
|
+
puts 'Select the recommendations you want to add to your playlist!'
|
38
|
+
selections = @prompt.multi_select('Hit enter with none selected to skip.', cleaned_recs)
|
39
|
+
clean_recommendations(selections)
|
9
40
|
end
|
10
41
|
|
11
42
|
def sort_my_list
|
@@ -13,62 +44,43 @@ class Rec
|
|
13
44
|
@artists.clear
|
14
45
|
@genres.clear
|
15
46
|
@user_list.each do |item|
|
16
|
-
@tracks << item[
|
17
|
-
@artists << item[
|
18
|
-
@genres << item[
|
47
|
+
@tracks << item['id'] if item['type'] == 'track'
|
48
|
+
@artists << item['id'] if item['type'] == 'artist'
|
49
|
+
@genres << item['name'].downcase if item['type'] == 'genre'
|
19
50
|
end
|
20
51
|
end
|
21
52
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
f.puts JSON.pretty_generate(updated_data)
|
26
|
-
end
|
27
|
-
puts "Sweet! Your list has been updated!".colorize(:light_green)
|
28
|
-
$prompt.keypress("Press any key to return to the previous menu..")
|
29
|
-
menu = Menu.new(@user)
|
30
|
-
menu.menu_router
|
53
|
+
def recommendations_generate(num)
|
54
|
+
sort_my_list
|
55
|
+
RSpotify::Recommendations.generate(limit: num, seed_artists: @artists, seed_genres: @genres, seed_tracks: @tracks)
|
31
56
|
end
|
32
57
|
|
33
58
|
def clean_recommendations(selections)
|
34
59
|
selections.each do |track|
|
35
|
-
details = track.split(
|
60
|
+
details = track.split(' by ')
|
36
61
|
song = RSpotify::Track.search("#{details[0]} #{details[1]}", limit: 1).first
|
37
|
-
song_details
|
38
|
-
"name" => song.name,
|
39
|
-
"id" => song.id,
|
40
|
-
"artist" => song.artists[0].name }
|
41
|
-
@user.playlist << song_details
|
62
|
+
song_details(song)
|
42
63
|
end
|
43
64
|
update_file
|
44
65
|
end
|
45
66
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
selections = $prompt.multi_select("Hit enter with none selected to skip.", cleaned_recs)
|
54
|
-
clean_recommendations(selections)
|
67
|
+
def song_details(song)
|
68
|
+
song_details = {
|
69
|
+
'name' => song.name,
|
70
|
+
'id' => song.id,
|
71
|
+
'artist' => song.artists[0].name
|
72
|
+
}
|
73
|
+
@user.playlist << song_details
|
55
74
|
end
|
56
75
|
|
57
|
-
def
|
58
|
-
|
59
|
-
|
60
|
-
puts
|
61
|
-
$prompt.keypress("Press any key to return to the previous menu..")
|
62
|
-
menu = Menu.new(@user)
|
63
|
-
menu.display_menu
|
64
|
-
end
|
65
|
-
puts "》 RECOMMENDATIONS 《"
|
66
|
-
amount = $prompt.ask("How many recommendations would you like to generate?".colorize(:light_green)) do |q|
|
67
|
-
q.in '1-10'
|
68
|
-
q.messages[:range?] = "Number must be between 1 and 10"
|
76
|
+
def update_file
|
77
|
+
updated_data = Login.load_data.each { |user| user['playlist'] = @user.playlist if user['id'] == @user.uid.to_s }
|
78
|
+
File.open(userdata, 'w') do |f|
|
79
|
+
f.puts JSON.pretty_generate(updated_data)
|
69
80
|
end
|
70
|
-
|
81
|
+
puts 'Sweet! Your list has been updated!'.colorize(:light_green)
|
82
|
+
@prompt.keypress('Press any key to return to the previous menu..')
|
83
|
+
menu = Menu.new(@user)
|
84
|
+
menu.menu_router
|
71
85
|
end
|
72
|
-
|
73
|
-
|
74
|
-
end
|
86
|
+
end
|
data/lib/user.rb
CHANGED
@@ -1,69 +1,89 @@
|
|
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
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# An instance of User should be made for everyone who logs in or signs up
|
4
|
+
class User
|
5
|
+
attr_reader :username, :password, :uid
|
6
|
+
attr_accessor :playlist, :mylist
|
7
|
+
|
8
|
+
def initialize(username, password, uid, playlist = [], mylist = [])
|
9
|
+
@username = username
|
10
|
+
@password = password
|
11
|
+
@playlist = playlist
|
12
|
+
@uid = uid
|
13
|
+
@mylist = mylist
|
14
|
+
@prompt = TTY::Prompt.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def details
|
18
|
+
puts "Username: #{@username.colorize(:light_green)}"
|
19
|
+
puts "Password: #{@password.colorize(:light_green)}"
|
20
|
+
puts "User ID: #{@uid.to_s.colorize(:light_green)}"
|
21
|
+
@prompt.keypress('Press any key to continue..')
|
22
|
+
menu = Menu.new(Login.user)
|
23
|
+
menu.account_details
|
24
|
+
end
|
25
|
+
|
26
|
+
# Change Username section
|
27
|
+
|
28
|
+
def change_username(new_name)
|
29
|
+
update_username(new_name)
|
30
|
+
@username = new_name
|
31
|
+
puts "Success! Your new username is #{Login.user.username}".colorize(:light_green)
|
32
|
+
@prompt.keypress('Press any key to continue..')
|
33
|
+
menu = Menu.new(Login.user)
|
34
|
+
menu.account_details
|
35
|
+
end
|
36
|
+
|
37
|
+
def update_username(new_name)
|
38
|
+
updated_data = Login.load_data.each do |user|
|
39
|
+
user['username'] = new_name if user['id'] == Login.user.uid.to_s
|
40
|
+
end
|
41
|
+
File.open(Login.userdata, 'w') do |f|
|
42
|
+
f.puts JSON.pretty_generate(updated_data)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Change password section
|
47
|
+
|
48
|
+
def change_password(new_password)
|
49
|
+
update_password(new_password)
|
50
|
+
@password = new_password
|
51
|
+
puts 'Success! Your password has been changed.'.colorize(:light_green)
|
52
|
+
@prompt.keypress('Press any key to continue..')
|
53
|
+
menu = Menu.new(Login.user)
|
54
|
+
menu.account_details
|
55
|
+
end
|
56
|
+
|
57
|
+
def update_password(new_password)
|
58
|
+
updated_data = Login.load_data.each do |user|
|
59
|
+
user['password'] = new_password if user['id'] == Login.user.uid.to_s
|
60
|
+
end
|
61
|
+
File.open(Login.userdata, 'w') do |f|
|
62
|
+
f.puts JSON.pretty_generate(updated_data)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Delete account section
|
67
|
+
|
68
|
+
def delete_account
|
69
|
+
puts 'Woah there! Deleting your account is serious business, and cannot be undone.'.colorize(:light_red)
|
70
|
+
selection = @prompt.yes?('Are you sure you want to delete your account?'.colorize(:light_red))
|
71
|
+
if selection
|
72
|
+
delete_from_file
|
73
|
+
else
|
74
|
+
menu = Menu.new(Login.user)
|
75
|
+
menu.account_details
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def delete_from_file
|
80
|
+
updated_data = []
|
81
|
+
Login.load_data.each { |user| updated_data << user unless user['id'] == Login.user.uid.to_s }
|
82
|
+
File.open(Login.userdata, 'w') do |f|
|
83
|
+
f.puts JSON.pretty_generate(updated_data)
|
84
|
+
end
|
85
|
+
puts 'Your account has been deleted. The program will now exit.'.colorize(:light_red)
|
86
|
+
@prompt.keypress('Press any key to continue..')
|
87
|
+
exit
|
88
|
+
end
|
89
|
+
end
|
data/public/users.json
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spotify_rec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Ahale
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -23,7 +23,6 @@ files:
|
|
23
23
|
- lib/my_list.rb
|
24
24
|
- lib/playlist.rb
|
25
25
|
- lib/rec.rb
|
26
|
-
- lib/run.rb
|
27
26
|
- lib/user.rb
|
28
27
|
- public/users.json
|
29
28
|
homepage:
|
@@ -38,7 +37,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
37
|
requirements:
|
39
38
|
- - ">="
|
40
39
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
40
|
+
version: '2.4'
|
42
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
42
|
requirements:
|
44
43
|
- - ">="
|
data/lib/run.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'rspotify'
|
2
|
-
require 'optparse'
|
3
|
-
require 'json'
|
4
|
-
require 'tty-prompt'
|
5
|
-
require 'terminal-table'
|
6
|
-
require 'colorize'
|
7
|
-
require_relative 'user'
|
8
|
-
require_relative 'playlist'
|
9
|
-
require_relative 'login'
|
10
|
-
require_relative 'rec'
|
11
|
-
require_relative 'menu'
|
12
|
-
require_relative 'my_list'
|
13
|
-
include Login
|
14
|
-
|
15
|
-
RSpotify.authenticate("712ff89a218a4e6dbe1f169e06f949b9", "e9e0517f405b4a01a1be8823126459b7")
|
16
|
-
$prompt = TTY::Prompt.new
|
17
|
-
|
18
|
-
VERSION = 1.0
|
19
|
-
|
20
|
-
ARGV << '--run' if ARGV.empty?
|
21
|
-
|
22
|
-
options = {}
|
23
|
-
parser = OptionParser.new do |opts|
|
24
|
-
opts.banner = "Welcome to Spotify Recommendations! Usage: spotifyrec [options]".colorize(:light_green)
|
25
|
-
|
26
|
-
opts.on("-v", "--version", "Display the version") do
|
27
|
-
puts "Spotify Recommendations version #{VERSION}".colorize(:light_green)
|
28
|
-
end
|
29
|
-
|
30
|
-
opts.on("-h", "--help", "Display the help message") do
|
31
|
-
puts opts
|
32
|
-
exit
|
33
|
-
end
|
34
|
-
|
35
|
-
opts.on("-qGENRE", "--quick=GENRE", "Generate a quick recommendation with the chosen genre") do |genre|
|
36
|
-
recommendations = RSpotify::Recommendations.generate(limit: 5, seed_genres: [genre])
|
37
|
-
cleaned_recs = recommendations.tracks.map { |t| "#{t.name} by #{t.artists[0].name}" }
|
38
|
-
puts "》 RECOMMENDATIONS 《".colorize(:light_green)
|
39
|
-
cleaned_recs.each { |track| puts track }
|
40
|
-
end
|
41
|
-
|
42
|
-
opts.on("-r", "--run", "Run the application") do
|
43
|
-
Login::login
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
parser.parse!
|