kgmusic 0.1.0pre20160517 → 0.1.0pre20160524

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: 34f94579c2edfe6b9b62d490224f73a5a5e38653
4
- data.tar.gz: 354ca55887440e1b8c8a372ccaa521ddf94005b3
3
+ metadata.gz: 7fd045bd3f769580e8326eb6fe3a67a2b4c6f572
4
+ data.tar.gz: 2b248d9cd21dbb25b7a27401b3fa7ca73022aec6
5
5
  SHA512:
6
- metadata.gz: 62a30ea5d3616881032f4aca06861b0ad2c493b7ebe9ddb1ab6bbfb84569ffe5f113ab3452470b54839df4b2f51e75f4b6ad4b27653f9ef54cd78c4bd5f79853
7
- data.tar.gz: f07f37ce5c857156e9439a59571ca81de4d976a8b2f9d68d3bc0c3397589ed572c532ed948a04e18d70314956c1fdfc3c4332173b391f00bd3878aa1f3525a13
6
+ metadata.gz: a33f6172cedf98f79424caf865a1dac55ef4e3c06498e37ce3d09ce06f03b83f8810eef217823c742b6a86f45dbb5611e9b5859afee2211ee05df89836b73a12
7
+ data.tar.gz: 4766d4f001cc045d985d8f65b5c0820803e1e324226f2fb727f8b031050d8eca3ded8611beedec174bcbc5282a551b39da389ca42e6d83099aa6ac98605956f5
data/README.md CHANGED
@@ -21,19 +21,20 @@ Or install it yourself as:
21
21
  ## Usage
22
22
  require 'kgmusic'
23
23
 
24
- music = KgMusic::Client.new :artist => 'God Is An Astronaut', :album => 'God Is An Astronaut'
24
+ Using API:
25
25
 
26
- music.search
26
+ cli = KgMusic::Client.new
27
27
 
28
- album = music.download_album
28
+ popular_artists = cli.popular_artists
29
29
 
30
- ## Development
30
+ popular_genres = cli.popular_genres
31
31
 
32
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
32
+ all_genres = cli.all_genres
33
33
 
34
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+ Using Downloader:
35
35
 
36
- ## Contributing
36
+ music = KgMusic::Downloader.new :artist => 'God Is An Astronaut', :album => 'God Is An Astronaut'
37
37
 
38
- Bug reports and pull requests are welcome on GitHub at https://github.com/vitvegl/kgmusic.
38
+ result = music.search # find album
39
39
 
40
+ album = music.download_album # download album
@@ -0,0 +1,40 @@
1
+ require "curl"
2
+ require "nokogiri"
3
+
4
+ module KgMusic
5
+
6
+ module API
7
+
8
+ class Client
9
+
10
+ def initialize(url: 'kibergrad.fm')
11
+ c = Curl::Easy.new(url) do |curl|
12
+ curl.follow_location = true
13
+ curl.perform
14
+ end
15
+ @@root = Nokogiri::HTML.parse(c.body_str)
16
+ end
17
+
18
+ def popular_artists
19
+ list = @@root.xpath("//div[@class='popular-artists']//li/a")
20
+ out = []; list.collect { |element| out << "#{element.children}" }
21
+ out
22
+ end
23
+
24
+ def popular_genres
25
+ list = @@root.xpath("//div/ul[@class='aim-menu css']/li/a")
26
+ out = []; list.collect { |element| out << "#{element.text}" }
27
+ out
28
+ end
29
+
30
+ def all_genres
31
+ list = @@root.xpath("//div//ul[@class='aim-menu css']//li/a")
32
+ out = []; list.collect { |element| out << "#{element.text}" }
33
+ out
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ end
data/lib/kgmusic/api.rb CHANGED
@@ -1,45 +1 @@
1
- require "curl"
2
- require "nokogiri"
3
- require "singleton"
4
-
5
- module KgMusic
6
-
7
- class API
8
-
9
- include Singleton
10
-
11
- class << self
12
-
13
- private
14
- def root
15
- root = Curl::Easy.new('kibergrad.fm') do |curl|
16
- curl.follow_location = true
17
- curl.perform
18
- end
19
-
20
- doc = Nokogiri::HTML.parse root.body_str
21
- end
22
-
23
- public
24
- def popular_artists
25
- list = root.xpath("//div[@class='popular-artists']//li/a")
26
- out = []; list.collect { |element| out << "#{element.children}" }
27
- out
28
- end
29
-
30
- def popular_genres
31
- list = root.xpath("//div/ul[@class='aim-menu css']/li/a")
32
- out = []; list.collect { |element| out << "#{element.text}" }
33
- out
34
- end
35
-
36
- def all_genres
37
- list = root.xpath("//div//ul[@class='aim-menu css']//li/a")
38
- out = []; list.collect { |element| out << "#{element.text}" }
39
- out
40
- end
41
-
42
- end
43
-
44
- end
45
- end
1
+ require 'kgmusic/api/client'
@@ -0,0 +1,84 @@
1
+ require 'kgmusic/version'
2
+ require 'kgmusic/api'
3
+ #require 'unicode_utils/titlecase'
4
+ #require 'fileutils'
5
+ require 'curl'
6
+ require 'nokogiri'
7
+
8
+ module KgMusic
9
+ module BaseMod
10
+ #include UnicodeUtils
11
+
12
+ UNALLOWED_SYMBOLS = [
13
+ "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", ",", "_", "=",
14
+ "+", "[", "{", "]", "}", "\\", "|", ";", ":", "\"", "<", ".", ">", "/", "?"
15
+ ]
16
+
17
+ protected
18
+ def go_to(url, params = {})
19
+ __url = Curl.urlalize(url, params)
20
+ c = Curl::Easy.new(__url) { |c| c.follow_location = true }
21
+ c.perform
22
+ c.body_str
23
+ end
24
+
25
+ def has_unallowed_symbols?(key)
26
+ counter = UNALLOWED_SYMBOLS.select {|s| key.include?(s)}
27
+ counter.size > 0 ? true : false
28
+ end
29
+
30
+ def strip_unallowed_symbols!(key)
31
+ UNALLOWED_SYMBOLS.map! {|s| key.delete(s)}
32
+ end
33
+
34
+ def parse_html doc
35
+ Nokogiri::HTML.parse doc
36
+ end
37
+
38
+ def validate(key)
39
+ strip_unallowed_symbols!(key) if has_unallowed_symbols?(key)
40
+ key
41
+ #words = key.split
42
+ #if words.size == 1
43
+ # key[0] =~ /^[[:lower:]]$/ ? titlecase(key) : key
44
+ #elsif words.size > 1
45
+ # words.map {|w| titlecase(w) unless (w[0].ord === w[0])}.join(" ")
46
+ #end
47
+ end
48
+
49
+ def parse_http_header str
50
+ list = str.split(/[\r\n]+/).map(&:strip)
51
+ Hash[list.flat_map { |s| s.scan(/^(\S+): (.+)/) }]
52
+ end
53
+
54
+ def __head_request __url
55
+ c = Curl::Easy.new(__url) { |curl| curl.set(:nobody, true) }
56
+ c.perform
57
+ parse_http_header c.header_str
58
+ end
59
+
60
+ def get_content_length __uri
61
+ response = __head_request __uri
62
+ response['Content-Length'].to_i
63
+ end
64
+
65
+ def obtain_final_url __uri
66
+ c = Curl::Easy.new __uri
67
+ c.perform
68
+ __headers = parse_http_header c.header_str
69
+ __headers['Location'] || __uri
70
+ end
71
+
72
+ def get_album_info(url, params={})
73
+ __response = go_to url, params
74
+ doc = parse_html __response
75
+ doc.css('div.album-info')
76
+ end
77
+
78
+ def get_direct_link __url
79
+ __response = go_to __url
80
+ doc = parse_html __response
81
+ @direct_link = doc.css('a.download-block').attr('href').to_s
82
+ end
83
+ end
84
+ end
@@ -1,85 +1 @@
1
- require 'kgmusic/version'
2
- #require 'unicode_utils/titlecase'
3
- require 'fileutils'
4
- require 'curl'
5
- require 'nokogiri'
6
-
7
- module KgMusic
8
- module BaseMod
9
- #include UnicodeUtils
10
-
11
- UNALLOWED_SYMBOLS = [
12
- "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", ",", "_", "=",
13
- "+", "[", "{", "]", "}", "\\", "|", ";", ":", "\"", "<", ".", ">", "/", "?"
14
- ]
15
-
16
- protected
17
-
18
- def go_to(url, params={})
19
- __url = Curl.urlalize(url, params)
20
- c = Curl::Easy.new(__url) { |c| c.follow_location = true }
21
- c.perform
22
- c.body_str
23
- end
24
-
25
- def has_unallowed_symbols?(key)
26
- counter = UNALLOWED_SYMBOLS.select {|s| key.include?(s)}
27
- counter.size > 0 ? true : false
28
- end
29
-
30
- def strip_unallowed_symbols!(key)
31
- UNALLOWED_SYMBOLS.map! {|s| key.delete(s)}
32
- end
33
-
34
- def parse_html doc
35
- Nokogiri::HTML.parse doc
36
- end
37
-
38
- def validate(key)
39
- strip_unallowed_symbols!(key) if has_unallowed_symbols?(key)
40
- key
41
- #words = key.split
42
- #if words.size == 1
43
- # key[0] =~ /^[[:lower:]]$/ ? titlecase(key) : key
44
- #elsif words.size > 1
45
- # words.map {|w| titlecase(w) unless (w[0].ord === w[0])}.join(" ")
46
- #end
47
- end
48
-
49
- def parse_http_header str
50
- list = str.split(/[\r\n]+/).map(&:strip)
51
- Hash[list.flat_map { |s| s.scan(/^(\S+): (.+)/) }]
52
- end
53
-
54
- def __head_request __url
55
- c = Curl::Easy.new(__url) { |curl| curl.set(:nobody, true) }
56
- c.perform
57
- parse_http_header c.header_str
58
- end
59
-
60
- def get_content_length __uri
61
- response = __head_request __uri
62
- response['Content-Length'].to_i
63
- end
64
-
65
- def obtain_final_url __uri
66
- c = Curl::Easy.new __uri
67
- c.perform
68
- __headers = parse_http_header c.header_str
69
- __headers['Location'] || __uri
70
- end
71
-
72
- def get_album_info(url, params={})
73
- __response = go_to url, params
74
- doc = parse_html __response
75
- doc.css('div.album-info')
76
- end
77
-
78
- def get_direct_link __url
79
- __response = go_to __url
80
- doc = parse_html __response
81
- @direct_link = doc.css('a.download-block').attr('href').to_s
82
- end
83
-
84
- end
85
- end
1
+ require 'kgmusic/base/basemod'
@@ -1,3 +1,3 @@
1
1
  module KgMusic
2
- VERSION = "0.1.0pre20160517"
2
+ VERSION = "0.1.0pre20160524"
3
3
  end
data/lib/kgmusic.rb CHANGED
@@ -3,14 +3,16 @@ require "kgmusic/basemod"
3
3
 
4
4
  module KgMusic
5
5
 
6
- class Client
6
+ class Client < API::Client; end
7
+
8
+ class Downloader
7
9
 
8
10
  include BaseMod
9
11
 
10
12
  attr_reader :artist, :album
11
13
  attr_reader :result, :download_page, :direct_link
12
14
 
13
- def initialize args
15
+ def initialize(args)
14
16
  keys = [:artist, :album]
15
17
  keys.each do |k|
16
18
  if args.keys === keys and args[k].is_a? String
@@ -54,7 +56,7 @@ module KgMusic
54
56
  f = File.open(outfile, 'ab+')
55
57
  fsize = File.size outfile
56
58
  c = Curl::Easy.new url do |curl|
57
- curl.set(:resume_from, fsize) if File.exists? outfile
59
+ curl.set(:resume_from, fsize) if fsize > 0
58
60
  curl.on_body { |body| f.write body }
59
61
  end
60
62
  puts "Please, wait ..."
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.0pre20160517
4
+ version: 0.1.0pre20160524
5
5
  platform: ruby
6
6
  authors:
7
7
  - vitvegl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-17 00:00:00.000000000 Z
11
+ date: 2016-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curb
@@ -110,6 +110,8 @@ files:
110
110
  - kgmusic.gemspec
111
111
  - lib/kgmusic.rb
112
112
  - lib/kgmusic/api.rb
113
+ - lib/kgmusic/api/client.rb
114
+ - lib/kgmusic/base/basemod.rb
113
115
  - lib/kgmusic/basemod.rb
114
116
  - lib/kgmusic/version.rb
115
117
  homepage: https://github.com/vitvegl/kgmusic.git
@@ -132,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
134
  version: 1.3.1
133
135
  requirements: []
134
136
  rubyforge_project:
135
- rubygems_version: 2.6.4
137
+ rubygems_version: 2.5.1
136
138
  signing_key:
137
139
  specification_version: 4
138
140
  summary: Find music albums on kibergrad.fm