-
Notifications
You must be signed in to change notification settings - Fork 1
/
ccmixter_download.rb
executable file
·214 lines (174 loc) · 5.7 KB
/
ccmixter_download.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/ruby
require 'fileutils'
require 'open-uri'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "\n ==ccmixter-download - a tool for batch downloading and streaming songs from ccMixter==\n Usage: ccmixter_download_artist.rb [OPTIONS]"
opts.on("-c", "--license LICENSE", "Filter tracks by license") { |v| options[:license] = v }
opts.on("-d", "--download", "Download all tracks") { options[:download] = true }
opts.on("-f", "--save-to-file", "Save urls to tracklist file") { options[:save] = true }
opts.on("-i", "--id ID", "Get results for track id (or url)") { |v| options[:id] = v }
opts.on("-l", "--limit NUMBER", "Specify results limit (default 200)") { |v| options[:limit] = v }
opts.on("-m", "--markdown", "Print out playlist in markdown format with links") { options[:markdown] = true }
opts.on("-p", "--print", "Print tracklist") { options[:print] = true }
opts.on("-q", "--query KEYWORD", "Search for a keyword") { |v| options[:search] = v }
opts.on("-R", "--remixes ID", "Get remixes of a given track by id number") { |v| options[:remixes] = v }
opts.on("-r", "--recommended", "Sort by highest recommended uploads") { options[:recommended] = true }
opts.on("-s", "--stream", "Stream entire playlist (requires mplayer)") { options[:stream] = true }
opts.on("-t", "--tag TAG", "Specify tag name") { |v| options[:tag] = v }
opts.on("-u", "--user USER", "Specify user name") { |v| options[:user] = v }
opts.on("-w", "--raw", "Output raw track array values (debugging)") { options[:raw] = true }
end.parse!
def get_url_content(params)
url = "http://ccmixter.org/api/query?f=html&t=links_by_dl_ul&chop=0" + params
URI.open(escape_url(url)).read
end
def get_track_list(params)
content = get_url_content(params)
content.scan(/<a href="(http:\/\/ccmixter.org\/(?:content|contests)\/.*?)">/)
end
def download_all_tracks(params, descriptor)
mp3 = get_track_list(params)
download_count = 1
puts " ** Fetching remote mp3s..."
mp3.uniq.each do |m|
filename = m[0].gsub(/.*\//, "")
FileUtils.mkdir_p descriptor
progress = download_count.to_f / mp3.uniq.length.to_f * 100
File.write(descriptor + "/" + filename, URI.open(m[0], "Referer" => "http://ccmixter.org/").read, {mode: 'wb'})
puts " ##{download_count.to_s} of #{mp3.uniq.length.to_s}: #{filename} saved to #{descriptor} directory! (#{progress.round(2).to_s}%)"
download_count += 1
end
puts " ** All files saved to download folder!"
end
def tracklist_to_file(params, filename)
mp3 = get_track_list(params)
mp3.uniq.each do |m|
File.open(filename, "w") { |f| f << mp3.join("\n") }
end
end
def save_tracklist(params, basename)
filename = basename + ".txt"
tracklist_to_file(params, filename)
puts " ** Tracklist saved to #{filename}!"
end
def stream_playlist(params, basename)
filename = "/tmp/" + basename + ".tmp"
tracklist_to_file(params, filename)
exec("mplayer -referrer http://ccmixter.org/ -playlist #{filename}")
end
def print_tracklist(params)
mp3 = get_track_list(params)
mp3.uniq.each do |m|
puts m
end
end
def raw_tracklist(params)
mp3 = get_track_list(params)
mp3.uniq.each do |m|
p m
end
end
def get_track_info(params)
content = get_url_content(params)
content.scan(/^\s+<li>\n\s+<a href="(http:\/\/ccmixter.org\/files\/.*?\/.*?)" class="cc_file_link">(.*?)<\/a>by <a href="(http:\/\/ccmixter.org\/people\/.*?)">(.*?)<\/a>\n\s+<a href="(http:\/\/ccmixter.org\/(?:content|contests)\/.*?)">(.*?)<\/a>\n\s+<\/li>/)
end
def print_markdown(params)
info = get_track_info(params)
info.each do |i|
ccmixter_link = i[0]
title = i[1]
artist_link = i[2]
artist_name = i[3]
file_download_link = i[4]
file_type = i[5]
puts "* _[#{title}](#{ccmixter_link})_: **[#{artist_name}](#{artist_link})** ([#{file_type}](#{file_download_link}))\n"
end
end
def get_descriptor(desc_array)
desc = "untitled"
join = desc_array.join("@").gsub(/(^@+)|(@+$)/, "").gsub(/@+/, "_").gsub(/ /, "-")
if join != ""
desc = join
end
desc
end
def get_id(url)
url.gsub(/.*\//, "")
end
params = ""
user = ""
tag = ""
search = ""
license = ""
limit = ""
sort = ""
id = ""
desc_arr = ["", "", "", ""]
if options[:limit]
limit = "&limit=" + options[:limit]
end
if options[:recommended]
sort = "&sort=num_scores"
end
if options[:user]
user = "&u=" + options[:user]
desc_arr[0] = options[:user]
end
if options[:tag]
tag = "&tags=" + options[:tag]
desc_arr[1] = options[:tag]
end
if options[:search]
kw = options[:search]
search = "&search=" + kw.gsub(/ /, "+") + "&search_type=match"
desc_arr[2] = kw
end
if options[:license]
license = "&lic=" + options[:license]
desc_arr[3] = options[:license]
end
if options[:id]
id = get_id(options[:id])
desc_arr[4] = id
id = "&ids=" + id
end
if options[:remixes]
id = get_id(options[:remixes])
desc_arr[4] = "remixes_of_" + id
id = "&remixes=" + id
end
def escape_url(url)
if url.match(/%/)
url = CGI.unescape(url)
end
url.gsub(/(.)/) { |u| u.match(URI::UNSAFE) ? CGI.escape(u) : u }
end
descriptor = get_descriptor(desc_arr)
basename = descriptor + "_ccmixter_tracklist"
params = user + tag + search + license + limit + sort + id
if params == ""
if ARGV[0]
params = "&tags=" + ARGV[0]
puts get_track_list(params)
else
puts " ** No arguments specified. Please use the -h option for help."
end
exit
end
if options[:download]
download_all_tracks(params, descriptor)
elsif options[:save]
save_tracklist(params, basename)
elsif options[:print]
print_tracklist(params)
elsif options[:markdown]
print_markdown(params)
elsif options[:stream]
stream_playlist(params, basename)
elsif options[:raw]
raw_tracklist(params)
else
puts get_track_list(params)
end