Created
September 3, 2014 05:58
-
-
Save savonarola/e15b03371ba7c29fe1df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'nokogiri' | |
if ARGV.size < 2 | |
abort "Usage:\n\n #{$0} VK_MUSIC_PAGE.HTML TARGET_PATH" | |
end | |
doc = Nokogiri::HTML::Document.parse(File.read(ARGV[0])) | |
target = ARGV[1] | |
doc.css('.audio').each do |audio| | |
input = audio.css('input').first | |
next unless input | |
link = input['value'].sub(/\?.*/, '') | |
title_wrap = audio.css('.info .title_wrap') | |
author_a, title_tag = title_wrap.css('a') | |
if title_tag.nil? | |
title_tag = title_wrap.css('span').first | |
end | |
author = author_a ? author_a.inner_text : 'Unknown' | |
title = title_tag ? title_tag.inner_text : 'Unknown' | |
[author, title].each do |s| | |
s.gsub!(/[^A-Za-zА-ЯёЁа-я\d\-\s]/, '') | |
s.strip! | |
end | |
name = "#{author} - #{title}.mp3" | |
path = File.join(target, name) | |
puts "wget '#{link}' -O '#{path}'" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment