从www.yobo.com下载音乐的一个简单脚本

2 views
Skip to first unread message

Trent...@gmail.com

unread,
Dec 20, 2007, 10:05:35 AM12/20/07
to Trent Zhou
#!/usr/bin/env ruby

def song_list_for_keyword(keyword)
result = []
song_hash = {}
page_url = "http://www.yobo.com/search/song?q=#{keyword}"
current_page = 1
total_page_count = 1
get_song = 1

while get_song == 1 do
text = `wget "#{page_url}" -O-`
song_url = ""
text.each_line do
|line|
case
when line =~ /.*yobo_play_lite\('([^ ]*)'\,/
song_url = "#{$1}.mp3"
when !(song_url.empty?)
if line =~ /target=\"_blank\">([^<]*)(<span.*<\/span>)?
<\/a/ then
title = $1
title.gsub!(/[ ,|&%\[]+/, '_')
title.gsub!(/_+/, '_')
title.gsub!(/_$/, '')
title = keyword if title.empty?
# handle songs with same name
song_hash[title] ||= 0
title += "#{song_hash[title]}" if
(song_hash[title] += 1) > 1
result.push([title, song_url])
song_url = ""
end
when line =~ /page=(\d+)\"/
total_page_count = $1.to_i
end
end
if current_page < total_page_count
current_page = current_page + 1
page_url = "http://www.yobo.com/search/song?
q=#{keyword}&page=#{current_page}"
else
get_song = 0
end
end

return result
end

if ARGV.size == 0 then
print "Search keyword: "
keyword = gets.chomp
else
keyword = ARGV[0]
end
exit if keyword.empty?

result = song_list_for_keyword(keyword)

result.each do
|v|
song_name = v[0]
url = v[1]
`wget -c "#{url}" -O #{song_name}.mp3`
end
Reply all
Reply to author
Forward
0 new messages