Class: XiamiCloner::Cloner
- Inherits:
-
Object
- Object
- XiamiCloner::Cloner
- Defined in:
- lib/xiami_cloner.rb
Constant Summary collapse
- INFO_URL =
'http://www.xiami.com/song/playlist/id/%d/object_name/default/object_id/0'
- ALBUM_PAGE_URL =
'http://www.xiami.com/album/%d'
- GET_HQ_URL =
'http://www.xiami.com/song/gethqsong/sid/%d'
- CACHE_DIR =
'~/Library/Caches/xiami_cloner'
Class Method Summary collapse
- .check_integrity(playlist) ⇒ Object
- .clone(playlist, outdir, options = {}) ⇒ Object
- .clone_song(song, outdir, options = {}) ⇒ Object
- .retrieve_album_list(id) ⇒ Object
Class Method Details
.check_integrity(playlist) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/xiami_cloner.rb', line 91 def self.check_integrity(playlist) songs = strip_invalid(File.read(playlist).lines) counter = 0 songs.each do |song| counter += 1 puts "正在检查第 #{counter} / #{songs.size} 首歌曲的完整性" puts " 歌曲 #{song} 没有下载完全" unless check_song_integrity(song) end end |
.clone(playlist, outdir, options = {}) ⇒ Object
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 |
# File 'lib/xiami_cloner.rb', line 22 def self.clone(playlist, outdir, = {}) cloneds = [:cloned_file] && File.exists?([:cloned_file])? strip_invalid(File.read([:cloned_file]).lines) : [] songs = strip_invalid(File.read(playlist).lines) terse = [:terse] counter = 0 songs.each do |song| counter += 1 print "正在下载第 #{counter} / #{songs.size} 首歌曲" unless terse if cloneds.include?(song) puts " ... 跳过" unless terse next end puts self.clone_song(song, outdir, ) cloneds << song File.write([:cloned_file], cloneds.join("\n")) if [:cloned_file] end end |
.clone_song(song, outdir, options = {}) ⇒ Object
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 |
# File 'lib/xiami_cloner.rb', line 50 def self.clone_song(song, outdir, = {}) [:import_to_itunes] ||= false terse = [:terse] hq = [:high_quality_song] = [:cookie] FileUtils.mkdir_p outdir print "正在下载 " unless terse info = retrieve_info(song) artist = info.search('artist').text title = info.search('title').text print "--- " if terse print "#{artist} - #{title} " puts url = retrieve_url(song, hq, ) song_path = hq ? "#{song}.hq.mp3" : "#{song}.mp3" while true break if check_song_integrity(song, hq) FileUtils.rm(self.cache_path(song_path)) self.download_to_cache(url, song_path, false) end out_path = File.join(outdir, filename(song)) out_path = uniquefy(out_path, ".mp3") FileUtils.cp(self.cache_path(song_path), out_path) write_id3(song, out_path) if [:import_to_itunes] import_to_itunes(out_path) puts "已将 #{artist} - #{title} 导入 iTunes" unless terse end end |
.retrieve_album_list(id) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/xiami_cloner.rb', line 105 def self.retrieve_album_list(id) require 'open-uri' url = ALBUM_PAGE_URL % id doc = Nokogiri::HTML(open(url).read) doc.css('.song_name').map do |song| /\/song\/([0-9]*)/.match(song.css('a')[0]['href'])[1] end end |