kgmusic 0.1.0pre20160614 → 0.1.0pre20160714

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5ca7c38be623e5e6c561330d317ea53d87e9104
4
- data.tar.gz: e4db947176a1c349b99ae3600178027d8f7e6a12
3
+ metadata.gz: d73f513669536de0261aefbe5a944f6443a69b04
4
+ data.tar.gz: 8c2e529a169cc96c4c1254550d5f425ae0a23667
5
5
  SHA512:
6
- metadata.gz: bf1836cd6d83458cbf7385131ac3ce79e7dd8fa89127bf511bcaeaf828e20f1cc0e9e8802f92acddbf85f2e21975f5bce242e421395f8f55958068cf598d22e8
7
- data.tar.gz: a6efea97a95056f55930cea606d6495d59118c2ebe38f02836a398626b8f8133a575c76c85322c4bda5c71f030ed65dc2de707b6fefaa4193f464e8bda326b68
6
+ metadata.gz: 4eac3e426cae4a299ab6a3d01843eeca9d122ab4eba04f086dbdf2fecb5b6a9c7f333ea9f5654027eb7dfff738593fd9db52684f95807580e16dc49c3138bff6
7
+ data.tar.gz: 3d6b463942511755767dfc8ad08530a3d21d88757718ab6a8d062146483ecb7185c9989af1741e5ea0701f058365dc0966f1b9272ac142574503ea1fa039fc17
@@ -16,15 +16,25 @@ module KgMusic
16
16
 
17
17
  protected
18
18
  def create_work_dir
19
- @work_dir = File.join(ENV['HOME'], 'kgmusic')
20
- Dir.mkdir(@work_dir, 0700) unless Dir.exist?(@work_dir)
19
+ @@work_dir = File.join(ENV['HOME'], 'kgmusic')
20
+ Dir.mkdir(@@work_dir, 0700) unless Dir.exist?(@@work_dir)
21
21
  end
22
22
 
23
- def go_to(url, params = {})
23
+ def go_to(url, params = {}, &block)
24
+ request = __perform_request(url, params, &block)
25
+ request.body_str
26
+ end
27
+
28
+ def __build_request(url, params = {}, &block)
24
29
  __url = Curl.urlalize(url, params)
25
- c = Curl::Easy.new(__url) { |c| c.follow_location = true }
26
- c.perform
27
- c.body_str
30
+ Curl::Easy.new(__url, &block)
31
+ end
32
+
33
+ def __perform_request(url, params = {}, &block)
34
+ request = __build_request(url, params, &block)
35
+ request.perform
36
+
37
+ request
28
38
  end
29
39
 
30
40
  def has_unallowed_symbols?(key)
@@ -36,10 +46,6 @@ module KgMusic
36
46
  UNALLOWED_SYMBOLS.map {|s| key.delete!(s)}
37
47
  end
38
48
 
39
- def parse_html doc
40
- Nokogiri::HTML.parse doc
41
- end
42
-
43
49
  def validate(key)
44
50
  if key.is_a? String
45
51
  strip_unallowed_symbols!(key) if has_unallowed_symbols?(key)
@@ -55,39 +61,33 @@ module KgMusic
55
61
  end
56
62
  end
57
63
 
58
- def parse_http_header str
64
+ def parse_http_header(str)
59
65
  list = str.split(/[\r\n]+/).map(&:strip)
60
66
  Hash[list.flat_map { |s| s.scan(/^(\S+): (.+)/) }]
61
67
  end
62
68
 
63
- def __head_request __url
64
- c = Curl::Easy.new(__url) { |curl| curl.set(:nobody, true) }
65
- c.perform
66
- parse_http_header c.header_str
69
+ def __head_request(url)
70
+ request = __perform_request(url) { |r| r.set(:nobody, true) }
71
+
72
+ parse_http_header(request.header_str)
67
73
  end
68
74
 
69
- def get_content_length __uri
70
- response = __head_request __uri
75
+ def get_content_length(url)
76
+ response = __head_request(url)
71
77
  response['Content-Length'].to_i
72
78
  end
73
79
 
74
- def obtain_final_url __uri
75
- c = Curl::Easy.new __uri
76
- c.perform
77
- __headers = parse_http_header c.header_str
78
- __headers['Location'] || __uri
79
- end
80
+ def obtain_final_url(url)
81
+ response = __perform_request(url)
82
+ headers = parse_http_header(response.header_str)
80
83
 
81
- def get_album_info(url, params={})
82
- __response = go_to url, params
83
- doc = parse_html __response
84
- doc.css('div.album-info')
84
+ headers['Location'] || url
85
85
  end
86
86
 
87
- def get_direct_link __url
88
- __response = go_to __url
89
- doc = parse_html __response
90
- @direct_link = doc.css('a.download-block').attr('href').to_s
87
+ def get_direct_link(url)
88
+ response = go_to(url)
89
+ doc = Nokogiri::HTML.parse(response)
90
+ doc.css('a.download-block').attr('href').to_s
91
91
  end
92
92
  end
93
93
  end
@@ -1,3 +1,3 @@
1
1
  module KgMusic
2
- VERSION = "0.1.0pre20160614"
2
+ VERSION = "0.1.0pre20160714"
3
3
  end
data/lib/kgmusic.rb CHANGED
@@ -15,70 +15,70 @@ module KgMusic
15
15
 
16
16
  include BaseMod
17
17
 
18
- attr_reader :artist, :album, :work_dir
19
- attr_reader :direct_link
18
+ attr_reader :artist, :album, :direct_link
20
19
 
21
20
  def initialize(artist:, album:)
22
21
  @artist = validate(artist)
23
22
  @album = validate(album)
24
- create_work_dir
25
23
  end
26
24
 
27
- protected
25
+ def search
28
26
 
29
- def run
30
27
  url = 'kibergrad.fm/search'
31
- params = { :q => "#{self.artist} - #{self.album}", :p => "albums" }
32
- album_info = get_album_info url, params
28
+
29
+ params = { q: "#{self.artist} - #{self.album}", p: "albums" }
30
+
31
+ response = go_to(url, params) { |r| r.follow_location = true }
32
+ doc = Nokogiri::HTML.parse(response)
33
+
34
+ album_info = doc.css('div.album-info')
33
35
 
34
36
  unless album_info.nil?
35
37
  begin
36
38
  if album_info.size == 1
37
39
  download_page = album_info.css('h3').at('a').attr('href')
38
40
  elsif album_info.size > 1
39
- full_match_index = album_info.css('h3').find_index { |e| e.at('a').children.to_s === @album }
41
+ full_match_index = album_info.css('h3').find_index { |element| element.at('a').children.to_s === @album }
40
42
  result = album_info.css('h3')[full_match_index]
41
43
  download_page = result.at('a').attr('href')
42
44
  end
43
- get_direct_link download_page
45
+ @direct_link = get_direct_link(download_page)
44
46
  rescue
45
47
  end
46
48
  end
49
+
47
50
  end
48
51
 
49
- def get_album
50
- url = obtain_final_url @direct_link
51
- create_work_dir
52
- outfile = File.join(@work_dir, "#{self.artist} - #{self.album}.zip")
53
- content_length = get_content_length url
52
+ def download_album
53
+
54
+ create_work_dir()
55
+ url = obtain_final_url(@direct_link)
56
+
57
+ outfile = File.join(@@work_dir, "#{@artist} - #{@album}.zip")
58
+ content_length = get_content_length url
59
+
60
+ File.open(outfile, 'ab') do |f|
54
61
 
55
- begin
56
- File.open(outfile, 'ab+') do |f|
57
62
  fsize = File.size(outfile)
58
- c = Curl::Easy.new url do |curl|
59
- curl.set(:resume_from, fsize) if fsize > 0
60
- curl.on_body { |body| f.write body }
61
- end
62
- c.on_progress do |dlt, dln, ult, uln|
63
- printf("\r %i of %i MBytes received ...", (dln / 1_000_000), (dlt / 1_000_000))
64
- true
65
- end
66
- c.perform if content_length > fsize
67
- end
68
- rescue => ex
69
- puts ex.message
70
- end
71
- nil
72
- end
73
63
 
74
- public
64
+ req = __build_request(url) do |r|
65
+
66
+ r.set(:resume_from, fsize) if fsize > 0
67
+
68
+ r.on_body { |body| f.write(body) }
69
+
70
+ r.on_progress do |dlt, dln, _, _|
71
+ printf("\r %i of %i MBytes received ...", (dln / 1_000_000), (dlt / 1_000_000))
72
+ true
73
+ end
74
+
75
+ end
76
+
77
+ req.perform if content_length > fsize
75
78
 
76
- def search
77
- run
78
- end
79
+ end
79
80
 
80
- def download_album
81
- get_album
81
+ nil
82
82
  end
83
83
  end
84
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kgmusic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0pre20160614
4
+ version: 0.1.0pre20160714
5
5
  platform: ruby
6
6
  authors:
7
7
  - vitvegl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-14 00:00:00.000000000 Z
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curb