Class: XiamiCloner::Cloner

Inherits:
Object
  • Object
show all
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

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, options = {})
	cloneds = options[:cloned_file] && File.exists?(options[:cloned_file])?
		strip_invalid(File.read(options[:cloned_file]).lines) :
		[]
	songs = strip_invalid(File.read(playlist).lines)
	terse = options[: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, options)

		cloneds << song
		File.write(options[:cloned_file], cloneds.join("\n")) if options[: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, options = {})
	options[:import_to_itunes] ||= false
	terse = options[:terse]
	hq = options[:high_quality_song]
	cookie = options[: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, cookie)
	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 options[: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