spotify_rec 1.1 → 1.6

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.
@@ -1,137 +1,162 @@
1
- class MyList
2
-
3
- def initialize(user)
4
- @user = user
5
- @mylist = user.mylist
6
- @menu = Menu.new(@user)
7
- end
8
-
9
- def separator
10
- puts "----------------------------------------"
11
- end
12
-
13
- def list
14
- unless @mylist.empty?
15
- rows = @mylist.map do |hash|
16
- if hash["type"] == "track" || hash["type"] == "album"
17
- ["#{hash["name"]} by #{hash["artist"]}", hash["type"].capitalize]
18
- else
19
- [hash["name"], hash["type"].capitalize]
20
- end
21
- end
22
- table = Terminal::Table.new :headings => ['Item', 'Type'], :rows => rows
23
- puts table
24
- $prompt.keypress("Press any key to return to the previous menu..")
25
- @menu.my_list
26
- else
27
- empty_list
28
- end
29
- end
30
-
31
- def empty_list
32
- puts "Oh no! Your list is currently empty!".colorize(:light_red)
33
- puts "Add up to 5 items to your list. An item can be a song, artist or genre.".colorize(:light_red)
34
- separator
35
- $prompt.keypress("Press any key to return to the previous menu..")
36
- @menu.my_list
37
- end
38
-
39
- def add_to_list
40
- if @mylist.length >= 5
41
- puts "Oh no! You've reached maximum capacity in your list! You won't be able to add another item until you remove an existing one.".colorize(:light_red)
42
- puts "You can do this by heading back to the previous menu, and selecting 'Remove'".colorize(:light_red)
43
- $prompt.keypress("Press any key to return to the previous menu..")
44
- @menu.my_list
45
- end
46
- selection = $prompt.select("Which type would you like to add?".colorize(:light_green), (["Song", "Artist", "Genre", "Back"]))
47
- case selection
48
- when "Song"
49
- search_song
50
- when "Artist"
51
- search_artist
52
- when "Genre"
53
- store_genre
54
- when "Back"
55
- @menu.my_list
56
- end
57
- end
58
-
59
- def remove_from_list
60
- empty_list if @mylist.length <= 0
61
- item_names = @mylist.map { |item| item["name"] }
62
- item_names << "Back"
63
- selection = $prompt.select("Which item would you like to remove?".colorize(:light_green), (item_names))
64
- @menu.my_list if selection == "Back"
65
- @mylist.each_with_index do |item, index|
66
- @mylist.delete_at(index) if item["name"] == selection
67
- end
68
- update_file
69
- end
70
-
71
- def search_song
72
- song_query = $prompt.ask("What is the name of the song?".colorize(:light_green))
73
- tracks = RSpotify::Track.search(song_query, limit: 5)
74
- cleaned_results = []
75
- tracks.each { |t| cleaned_results << "#{t.name} by #{t.artists[0].name}" }
76
- system("clear")
77
- cleaned_results << "Back"
78
- selection = $prompt.select("Please select one of the search results:", (cleaned_results)).split(" by ")
79
- add_to_list if selection[0] == "Back"
80
- store_song(selection)
81
- end
82
-
83
- def store_song(details)
84
- track = RSpotify::Track.search("#{details[0]} #{details[1]}", limit: 1).first
85
- song_details = {
86
- "name" => track.name,
87
- "artist" => track.artists[0].name,
88
- "id" => track.id,
89
- "type" => "track"
90
- }
91
- @mylist << song_details
92
- update_file
93
- end
94
-
95
- def search_artist
96
- artist_query = $prompt.ask("What is the name of the artist?".colorize(:light_green))
97
- artists = RSpotify::Artist.search(artist_query, limit: 5)
98
- cleaned_results = []
99
- artists.each { |a| cleaned_results << "#{a.name}" }
100
- system("clear")
101
- selection = $prompt.select("Please select one of the search results:", (cleaned_results))
102
- store_artist(selection)
103
- end
104
-
105
- def store_artist(details)
106
- artist = RSpotify::Artist.search("#{details}", limit: 1).first
107
- artist_details = {
108
- "name" => artist.name,
109
- "id" => artist.id,
110
- "type" => "artist"
111
- }
112
- @mylist << artist_details
113
- update_file
114
- end
115
-
116
- def store_genre
117
- genres = RSpotify::Recommendations.available_genre_seeds
118
- genre = $prompt.select("Which genre would you like to add to your list?".colorize(:light_green), genres, filter: true)
119
- genre_details = {
120
- "name" => genre.capitalize,
121
- "type" => "genre"
122
- }
123
- @mylist << genre_details
124
- update_file
125
- end
126
-
127
- def update_file
128
- updated_data = Login.load_data.each { |user| user["mylist"] = @mylist if user["id"] == @user.uid.to_s }
129
- File.open(userdata,"w") do |f|
130
- f.puts JSON.pretty_generate(updated_data)
131
- end
132
- puts "Sweet! Your list has been updated!".colorize(:light_green)
133
- $prompt.keypress("Press any key to return to the previous menu..")
134
- @menu.my_list
135
- end
136
-
137
- end
1
+ # frozen_string_literal: true
2
+
3
+ class MyList
4
+ def initialize(user)
5
+ @user = user
6
+ @mylist = user.mylist
7
+ @menu = Menu.new(@user)
8
+ @prompt = TTY::Prompt.new
9
+ end
10
+
11
+ # Display MyList
12
+ # Displays items in MyList. Raises error if no items in list
13
+ def list
14
+ raise MyListEmpty.new, 'List must not be empty' if @mylist.empty?
15
+
16
+ list_table
17
+ @prompt.keypress('Press any key to return to the previous menu..')
18
+ @menu.my_list
19
+ rescue MyListEmpty
20
+ empty_list
21
+ end
22
+
23
+ # Generates the list in table view and prints it to the screen
24
+ def list_table
25
+ rows = @mylist.map do |hash|
26
+ if hash['type'] == 'track' || hash['type'] == 'album'
27
+ ["#{hash['name']} by #{hash['artist']}", hash['type'].capitalize]
28
+ else
29
+ [hash['name'], hash['type'].capitalize]
30
+ end
31
+ end
32
+ table = Terminal::Table.new headings: %w[Item Type], rows: rows
33
+ puts table
34
+ end
35
+
36
+ # Tells the user they have no items in their list, and returns to menu
37
+ def empty_list
38
+ puts 'Oh no! Your list is currently empty!'.colorize(:light_red)
39
+ puts 'Add up to 5 items to your list. An item can be a song, artist or genre.'.colorize(:light_red)
40
+ puts
41
+ @prompt.keypress('Press any key to return to the previous menu..')
42
+ @menu.my_list
43
+ end
44
+
45
+ # Add to MyList
46
+ # Prompts the user to specify what type of item to add
47
+ def add_to_list
48
+ list_too_long if @mylist.length >= 5
49
+ selection = @prompt.select('Which type would you like to add?'.colorize(:light_green), %w[Song Artist Genre Back])
50
+ case_add_to_list(selection)
51
+ end
52
+
53
+ # Returns user to menu if the MyList has too many items
54
+ def list_too_long
55
+ puts "Oh no! You've reached maximum capacity in your list! You won't be able to add".colorize(:light_red)
56
+ puts 'another item until you remove an existing one.'.colorize(:light_red)
57
+ puts "You can do this by heading back to the previous menu, and selecting 'Remove'".colorize(:light_red)
58
+ @prompt.keypress('Press any key to return to the previous menu..')
59
+ @menu.my_list
60
+ end
61
+
62
+ # Cases the add to list selection and routes the user
63
+ def case_add_to_list(selection)
64
+ case selection
65
+ when 'Song'
66
+ search_song
67
+ when 'Artist'
68
+ search_artist
69
+ when 'Genre'
70
+ store_genre
71
+ when 'Back'
72
+ @menu.my_list
73
+ end
74
+ end
75
+
76
+ # Prompts user for song name and searches spotify. Prompts user to select a result
77
+ def search_song
78
+ song_query = @prompt.ask('What is the name of the song?'.colorize(:light_green))
79
+ tracks = RSpotify::Track.search(song_query, limit: 5)
80
+ cleaned_results = []
81
+ tracks.each { |t| cleaned_results << "#{t.name} by #{t.artists[0].name}" }
82
+ system('clear')
83
+ cleaned_results << 'Back'
84
+ selection = @prompt.select('Please select one of the search results:', cleaned_results).split(' by ')
85
+ add_to_list if selection[0] == 'Back'
86
+ store_song(selection)
87
+ end
88
+
89
+ # Stores the song in a hash ready to be written to the file
90
+ def store_song(details)
91
+ track = RSpotify::Track.search("#{details[0]} #{details[1]}", limit: 1).first
92
+ song_details = {
93
+ 'name' => track.name,
94
+ 'artist' => track.artists[0].name,
95
+ 'id' => track.id,
96
+ 'type' => 'track'
97
+ }
98
+ @mylist << song_details
99
+ update_file
100
+ end
101
+
102
+ # Prompts user for artist name, searches for the artist and prompts user to select a result
103
+ def search_artist
104
+ artist_query = @prompt.ask('What is the name of the artist?'.colorize(:light_green))
105
+ artists = RSpotify::Artist.search(artist_query, limit: 5)
106
+ cleaned_results = []
107
+ artists.each { |a| cleaned_results << a.name.to_s }
108
+ system('clear')
109
+ selection = @prompt.select('Please select one of the search results:', cleaned_results)
110
+ store_artist(selection)
111
+ end
112
+
113
+ # Stores the artist details in a hash, ready to be written to userfile
114
+ def store_artist(details)
115
+ artist = RSpotify::Artist.search(details.to_s, limit: 1).first
116
+ artist_details = {
117
+ 'name' => artist.name,
118
+ 'id' => artist.id,
119
+ 'type' => 'artist'
120
+ }
121
+ @mylist << artist_details
122
+ update_file
123
+ end
124
+
125
+ # Prompts user to select genre from the list. Stores details in hash to be written to file
126
+ def store_genre
127
+ genres = RSpotify::Recommendations.available_genre_seeds
128
+ genre = @prompt.select('Which genre would you like to add to your list?', genres, filter: true)
129
+ genre_details = {
130
+ 'name' => genre.capitalize,
131
+ 'type' => 'genre'
132
+ }
133
+ @mylist << genre_details
134
+ update_file
135
+ end
136
+
137
+ # Remove From MyList
138
+ # Prompts user to select item to remove from MyList. Removes item from mylist array
139
+ def remove_from_list
140
+ empty_list if @mylist.length <= 0
141
+ item_names = @mylist.map { |item| item['name'] }
142
+ item_names << 'Back'
143
+ selection = @prompt.select('Which item would you like to remove?'.colorize(:light_green), item_names)
144
+ @menu.my_list if selection == 'Back'
145
+ @mylist.each_with_index do |item, index|
146
+ @mylist.delete_at(index) if item['name'] == selection
147
+ end
148
+ update_file
149
+ end
150
+
151
+ # Update userfile
152
+ # Writes the user MyList to the userfile
153
+ def update_file
154
+ updated_data = Login.load_data.each { |user| user['mylist'] = @mylist if user['id'] == @user.uid.to_s }
155
+ File.open(userdata, 'w') do |f|
156
+ f.puts JSON.pretty_generate(updated_data)
157
+ end
158
+ puts 'Sweet! Your list has been updated!'.colorize(:light_green)
159
+ @prompt.keypress('Press any key to return to the previous menu..')
160
+ @menu.my_list
161
+ end
162
+ end
@@ -1,112 +1,148 @@
1
- class Playlist
1
+ # frozen_string_literal: true
2
2
 
3
+ class Playlist
3
4
  attr_reader :tracks
4
5
 
5
6
  def initialize(user)
6
- @playlist = user.playlist
7
7
  @user = user
8
+ @playlist = user.playlist
9
+ @prompt = TTY::Prompt.new
8
10
  end
9
11
 
10
- def list
11
- rows = @playlist.map { |track| [track["name"], track["artist"]] }
12
- table = Terminal::Table.new :headings => ['Track', 'Artist'], :rows => rows
13
- puts table
12
+ # Playlist Menu Section
13
+ # Prompts user to select an item from the menu
14
+ def menu
15
+ system('clear')
16
+ selection = @prompt.select('》 PLAYLIST 《', ['Display', 'Add', 'Remove', 'Export To File', 'Back'])
17
+ case selection
18
+ when 'Display'
19
+ list
20
+ else
21
+ case_menu(selection)
22
+ end
14
23
  end
15
24
 
16
- def remove
17
- empty if @playlist.length <= 0
18
- item_names = @playlist.map { |item| "#{item["name"]} by #{item["artist"]}" }
19
- item_names << "Back"
20
- selection = $prompt.select("Which track would you like to remove?", (item_names)).split(" by ")
21
- menu if selection == "Back"
22
- @playlist.each_with_index do |item, index|
23
- @user.playlist.delete_at(index) if item["name"] == selection[0]
25
+ # Cases the user selection and routes the user
26
+ def case_menu(selection)
27
+ case selection
28
+ when 'Add'
29
+ add
30
+ when 'Remove'
31
+ remove
32
+ else
33
+ second_case_menu(selection)
34
+ end
35
+ end
36
+
37
+ # Continues to case the selection and route the user
38
+ def second_case_menu(selection)
39
+ case selection
40
+ when 'Export To File'
41
+ export_to_file
42
+ when 'Back'
43
+ menu = Menu.new(@user)
44
+ menu.menu_router
24
45
  end
25
- update_playlist
26
46
  end
27
47
 
48
+ # View Playlist
49
+ # Generates table from user playlist and prints it to the screen
50
+ def list
51
+ puts '》 PLAYLIST 《'
52
+ empty if @playlist.empty?
53
+ rows = @playlist.map { |track| [track['name'], track['artist']] }
54
+ table = Terminal::Table.new headings: %w[Track Artist], rows: rows
55
+ puts table
56
+ keypress_playlist
57
+ menu
58
+ end
59
+
60
+ # Tells user if their playlist is empty and sends them back to the menu
28
61
  def empty
29
- puts "Oh no! Your playlist is currently empty!"
30
- puts "You can add songs manually from the previous menu, or generate recommendations and add those!"
31
- separator
32
- $prompt.keypress("Press any key to return to the previous menu..")
33
- Menu::my_list
62
+ puts 'Oh no! Your playlist is currently empty!'
63
+ puts 'You can add songs manually from the previous menu, or generate recommendations and add those!'
64
+ puts
65
+ @prompt.keypress('Press any key to return to the previous menu..')
66
+ menu
34
67
  end
35
68
 
69
+ # Add to Playlist
70
+ # Prompts user to enter song name, searches for song and prompts user to select a result
36
71
  def add
37
- song_query = $prompt.ask("What is the name of the song?")
72
+ song_query = @prompt.ask('What is the name of the song?')
38
73
  tracks = RSpotify::Track.search(song_query, limit: 5)
39
74
  cleaned_results = []
40
75
  tracks.each { |t| cleaned_results << "#{t.name} by #{t.artists[0].name}" }
41
- system("clear")
42
- cleaned_results << "Back"
43
- selection = $prompt.select("Please select one of the search results:", (cleaned_results)).split(" by ")
44
- menu if selection[0] == "Back"
76
+ system('clear')
77
+ cleaned_results << 'Back'
78
+ selection = @prompt.select('Please select one of the search results:', cleaned_results).split(' by ')
79
+ menu if selection[0] == 'Back'
45
80
  store(selection)
46
81
  end
47
82
 
83
+ # Turns song details into a hash and adds to playlist array
48
84
  def store(details)
49
85
  track = RSpotify::Track.search("#{details[0]} #{details[1]}", limit: 1).first
50
86
  song_details = {
51
- "name" => track.name,
52
- "id" => track.id,
53
- "artist" => track.artists[0].name
87
+ 'name' => track.name,
88
+ 'id' => track.id,
89
+ 'artist' => track.artists[0].name
54
90
  }
55
91
  @playlist << song_details
56
92
  update_playlist
57
93
  end
58
94
 
95
+ # Remove from Playlist
96
+ # Prompts user to select item in playlist to remove and deleted from playlist array
97
+ def remove
98
+ empty if @playlist.length <= 0
99
+ item_names = @playlist.map { |item| "#{item['name']} by #{item['artist']}" }
100
+ item_names << 'Back'
101
+ selection = @prompt.select('Which track would you like to remove?', item_names).split(' by ')
102
+ menu if selection == 'Back'
103
+ @playlist.each_with_index do |item, index|
104
+ @user.playlist.delete_at(index) if item['name'] == selection[0]
105
+ end
106
+ update_playlist
107
+ end
108
+
109
+ # Update Playlist in file
110
+ # Loads userdata and updates playlist in file. Returns the user to the menu
59
111
  def update_playlist
60
- updated_data = Login.load_data.each { |user| user["playlist"] = @playlist if user["id"] == @user.uid.to_s }
61
- File.open((Login::userdata),"w") do |f|
112
+ updated_data = Login.load_data.each { |user| user['playlist'] = @playlist if user['id'] == @user.uid.to_s }
113
+ File.open(Login.userdata, 'w') do |f|
62
114
  f.puts JSON.pretty_generate(updated_data)
63
115
  end
64
- puts "Sweet! Your playlist has been updated!"
65
- $prompt.keypress("Press any key to return to the previous menu..")
116
+ puts 'Sweet! Your playlist has been updated!'
117
+ @prompt.keypress('Press any key to return to the previous menu..')
66
118
  menu
67
119
  end
68
120
 
121
+ # Export playlist to file
122
+ # Writes user playlist information to a markdown file
69
123
  def export_to_file
70
124
  path = File.join(File.dirname(File.dirname(File.absolute_path(__FILE__))))
71
- playlist_file = "#{path}/playlist.md"
72
- File.open(playlist_file,"w") do |f|
125
+ File.open("#{path}/playlist.md", 'w') do |f|
73
126
  f.puts("# #{@user.username}'s Playlist")
74
- @playlist.each { |track| f.puts("1. #{track["name"]} by #{track["artist"]} || [Listen on Spotify](https://open.spotify.com/track/#{track["id"]})") }
127
+ @playlist.each do |track|
128
+ link = "[Listen on Spotify](https://open.spotify.com/track/#{track['id']})"
129
+ f.puts("1. #{track['name']} by #{track['artist']} #{link}")
130
+ end
75
131
  end
76
- system("cp #{playlist_file} ~/Desktop/playlist.md")
77
- puts "Exported playlist to your Desktop!"
78
- sleep(2)
79
- system("clear")
132
+ copy_to_desktop(path)
80
133
  end
81
134
 
82
- def keypress_playlist
83
- $prompt.keypress("Press any key to return to the previous menu..")
135
+ # Copies markdown file to desktop for easy access
136
+ def copy_to_desktop(path)
137
+ system("cp #{path}/playlist.md ~/Desktop/playlist.md")
138
+ puts 'Exported playlist to your Desktop!'
139
+ @prompt.keypress('Press any key to return to the previous menu..')
140
+ menu
84
141
  end
85
142
 
86
- def menu
87
- system("clear")
88
- selection = $prompt.select("》 PLAYLIST 《", (["Display", "Add", "Remove", "Export To File", "Back"]))
89
- case selection
90
- when "Display"
91
- puts "》 PLAYLIST 《"
92
- list
93
- keypress_playlist
94
- menu
95
- when "Add"
96
- add
97
- keypress_playlist
98
- menu
99
- when "Remove"
100
- remove
101
- menu
102
- when "Export To File"
103
- export_to_file
104
- keypress_playlist
105
- menu
106
- when "Back"
107
- @menu = Menu.new(@user)
108
- @menu.menu_router
109
- end
110
- end
143
+ # Helper method
111
144
 
112
- end
145
+ def keypress_playlist
146
+ @prompt.keypress('Press any key to return to the previous menu..')
147
+ end
148
+ end