Class: Spfy

Inherits:
Object
  • Object
show all
Defined in:
lib/spfy.rb

Class Method Summary collapse

Class Method Details

.generateObject



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/spfy.rb', line 32

def self.generate
	#
	# Read command-line arguments
	#
	begin
	
		# short usage banner
		simple_usage = "Use `#{File.basename($0)} --help` for available options."
	
		# test for zero arguments
		if ARGV.empty? then
			puts simple_usage
			exit
		end
		
		# parse command-line arguments
		options = OptionReader.parse(ARGV)
		
		# test for zero source paths
		if $dirs.empty?
			puts "No source path specified."
			puts simple_usage
			exit
		end
		
	rescue OptionParser::InvalidOption => t
		puts t
		puts simple_usage
		exit
	rescue OptionParser::MissingArgument => m
		puts m
		puts simple_usage
		exit
	end
	
	#
	# Process valid paths
	#
	begin
		if options.output.any?
			
			# test whether there is an option for data to output
			if options.hide_title and options.hide_artist and options.hide_album
				
				# all data has been suppressed, report to user
				puts "All tags suppressed, no XML file created."
				exit
			end
		
			xmlFile = File.open(options.output[0], "w")
			
			print "Generating XML.."
			
			xmlFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
			xmlFile.write("<playlist version=\"1\" xmlns=\"http:#xspf.org/ns/0/\">\n")
			xmlFile.write("\t<trackList>\n")
			
			Dir.foreach($dirs[0]).sort.each do |file|
				next if file == '.' or file == '..' or
				next if file.start_with? '.'
				
				begin
					TagLib::FileRef.open($dirs[0] + "/" + file) do |fileref|
	
						tag = fileref.tag
						
						# skip files with no tags
						next if tag.title.empty? and tag.artist.empty? and tag.album.empty?
						
						xmlFile.write("\t\t<track>\n")
						#xmlFile.write("\t\t\t<location>http:##{host}#{musicDir}/#{file}</location>\n")
						xmlFile.write("\t\t\t<title>#{tag.title}</title>\n") if !options.hide_title and !tag.title.empty?
						xmlFile.write("\t\t\t<creator>#{tag.artist}</creator>\n") if !options.hide_artist and !tag.artist.empty?
						xmlFile.write("\t\t\t<album>#{tag.album}</album>\n") if !options.hide_album and !tag.album.empty?
						xmlFile.write("\t\t</track>\n")
						
					end
				rescue Exception => e
					next
				end
			end
			
			xmlFile.write("\t</trackList>\n")
			xmlFile.write("</playlist>\n")
			xmlFile.close
			
			print " success\n"
		else		
			puts "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
			puts "<playlist version=\"1\" xmlns=\"http:#xspf.org/ns/0/\">\n"
			puts "\t<trackList>\n"
			
			Dir.foreach($dirs[0]).sort.each do |file|
				next if file == '.' or file == '..' or
				next if file.start_with? '.'
				
				begin
					TagLib::FileRef.open($dirs[0] + "/" + file) do |fileref|
					
						tag = fileref.tag
	
						# skip files with no tags
						next if tag.title.empty? and tag.artist.empty? and tag.album.empty?
	
						puts "\t\t<track>\n"
						#xmlFile.write("\t\t\t<location>http:##{host}#{musicDir}/#{file}</location>\n")
						puts "\t\t\t<title>#{tag.title}</title>\n"
						puts "\t\t\t<creator>#{tag.artist}</creator>\n"
						puts "\t\t\t<album>#{tag.album}</album>\n"
						puts "\t\t</track>\n"
						
					end
				rescue Exception => e
					next
				end
			end
			
			puts "\t</trackList>\n"
			puts "</playlist>\n"
		end
		
	rescue SystemExit, Interrupt
		abort("Cancelled, exiting..")
	rescue Exception => e	
		abort("Exiting.. (#{e})")
	end

end