-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_playlist.rb
71 lines (55 loc) · 1.97 KB
/
build_playlist.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
# Preston Copeland
# July 6, 2013
# Tealeaf course 1
# Pine's 'How to Program,' ch. 11
=begin
Build your own playlist. For this to work, you need to have some music ripped
to your computer in some format. We’ve ripped a 100 or so CDs, and we
keep them in directories something like music/genre/artist_and_cd_name/
track_number.ogg. (I’m partial to the .ogg format, though this would work
just as well with .mp3s or whatever you use.)
Building a playlist is easy. It’s just a regular text file (no YAML
required, even). Each line is a filename, like this:
music/world/Stereolab--Margarine_Eclipse/track05.ogg
What makes it a playlist? Well, you have to give the file the .m3u
extension, like playlist.m3u or something. And that’s all a playlist
is: a text file with an .m3u extension.
So, have your program search for various music files and build you a
playlist. Use your shuffle method on page 75 to mix up your playlist.
Then check it out in your favorite music player (Winamp, MPlayer, and so on)!
=end
require 'yaml'
# First we define these fancy methods...
def yaml_save object, filename
File.open filename, 'w' do |f| f.write(object.to_yaml)
end end
def yaml_load filename
yaml_string = File.read filename
YAML::load yaml_string
end
def shuffle array
shuffled = Array.new(array.length)
originalSize = array.length
while(!array.empty?)
entry = array.pop
newPosition = rand(originalSize)
while(shuffled[newPosition] !=nil)
newPosition = rand(originalSize)
end
shuffled[newPosition] = entry
end
shuffled
end
def build_playlist
puts "What would you like the playlist title to be?"
playlist_title = gets.chomp
playlist_title = playlist_title + ".txt"
Dir.chdir '~/Users/prestoncopeland'
possible_songs = Dir['**/*.{M3U,m3u}']
possible_songs = shuffle possible_songs
new_playlist = []
50.times do
new_playlist << possible_songs.pop
end
yaml_save(new_playlist, playlist_title)
end