kgmusic 0.1.0pre20160515 → 0.1.0pre20160517
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 +4 -4
 - data/README.md +4 -3
 - data/lib/kgmusic/api.rb +45 -0
 - data/lib/kgmusic/basemod.rb +19 -16
 - data/lib/kgmusic/version.rb +1 -1
 - data/lib/kgmusic.rb +14 -25
 - metadata +3 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 34f94579c2edfe6b9b62d490224f73a5a5e38653
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 354ca55887440e1b8c8a372ccaa521ddf94005b3
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 62a30ea5d3616881032f4aca06861b0ad2c493b7ebe9ddb1ab6bbfb84569ffe5f113ab3452470b54839df4b2f51e75f4b6ad4b27653f9ef54cd78c4bd5f79853
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: f07f37ce5c857156e9439a59571ca81de4d976a8b2f9d68d3bc0c3397589ed572c532ed948a04e18d70314956c1fdfc3c4332173b391f00bd3878aa1f3525a13
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -19,12 +19,13 @@ Or install it yourself as: 
     | 
|
| 
       19 
19 
     | 
    
         
             
                $ gem install kgmusic
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
21 
     | 
    
         
             
            ## Usage
         
     | 
| 
      
 22 
     | 
    
         
            +
                require 'kgmusic'
         
     | 
| 
       22 
23 
     | 
    
         | 
| 
       23 
     | 
    
         
            -
                 
     | 
| 
      
 24 
     | 
    
         
            +
                music = KgMusic::Client.new :artist => 'God Is An Astronaut', :album => 'God Is An Astronaut'
         
     | 
| 
       24 
25 
     | 
    
         | 
| 
       25 
     | 
    
         
            -
                 
     | 
| 
      
 26 
     | 
    
         
            +
                music.search
         
     | 
| 
       26 
27 
     | 
    
         | 
| 
       27 
     | 
    
         
            -
                 
     | 
| 
      
 28 
     | 
    
         
            +
                album = music.download_album
         
     | 
| 
       28 
29 
     | 
    
         | 
| 
       29 
30 
     | 
    
         
             
            ## Development
         
     | 
| 
       30 
31 
     | 
    
         | 
    
        data/lib/kgmusic/api.rb
    ADDED
    
    | 
         @@ -0,0 +1,45 @@ 
     | 
|
| 
      
 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
         
     | 
    
        data/lib/kgmusic/basemod.rb
    CHANGED
    
    | 
         @@ -14,6 +14,7 @@ module KgMusic 
     | 
|
| 
       14 
14 
     | 
    
         
             
                ]
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
       16 
16 
     | 
    
         
             
                protected
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
       17 
18 
     | 
    
         
             
                def go_to(url, params={})
         
     | 
| 
       18 
19 
     | 
    
         
             
                  __url = Curl.urlalize(url, params)
         
     | 
| 
       19 
20 
     | 
    
         
             
                  c = Curl::Easy.new(__url) { |c| c.follow_location = true }
         
     | 
| 
         @@ -45,18 +46,15 @@ module KgMusic 
     | 
|
| 
       45 
46 
     | 
    
         
             
                  #end
         
     | 
| 
       46 
47 
     | 
    
         
             
                end
         
     | 
| 
       47 
48 
     | 
    
         | 
| 
       48 
     | 
    
         
            -
                def  
     | 
| 
       49 
     | 
    
         
            -
                   
     | 
| 
      
 49 
     | 
    
         
            +
                def parse_http_header str
         
     | 
| 
      
 50 
     | 
    
         
            +
                  list = str.split(/[\r\n]+/).map(&:strip)
         
     | 
| 
      
 51 
     | 
    
         
            +
                  Hash[list.flat_map { |s| s.scan(/^(\S+): (.+)/) }]
         
     | 
| 
       50 
52 
     | 
    
         
             
                end
         
     | 
| 
       51 
53 
     | 
    
         | 
| 
       52 
54 
     | 
    
         
             
                def __head_request __url
         
     | 
| 
       53 
     | 
    
         
            -
                  c = Curl::Easy.new(__url)  
     | 
| 
       54 
     | 
    
         
            -
                    curl.set(:nobody, true)
         
     | 
| 
       55 
     | 
    
         
            -
                  end
         
     | 
| 
      
 55 
     | 
    
         
            +
                  c = Curl::Easy.new(__url) { |curl| curl.set(:nobody, true) }
         
     | 
| 
       56 
56 
     | 
    
         
             
                  c.perform
         
     | 
| 
       57 
     | 
    
         
            -
                   
     | 
| 
       58 
     | 
    
         
            -
                  headers_list = response.split(/[\r\n]+/).map(&:strip)
         
     | 
| 
       59 
     | 
    
         
            -
                  __headers = Hash[headers_list.flat_map {|s| s.scan(/^(\S+): (.+)/)}]
         
     | 
| 
      
 57 
     | 
    
         
            +
                  parse_http_header c.header_str
         
     | 
| 
       60 
58 
     | 
    
         
             
                end
         
     | 
| 
       61 
59 
     | 
    
         | 
| 
       62 
60 
     | 
    
         
             
                def get_content_length __uri
         
     | 
| 
         @@ -67,16 +65,21 @@ module KgMusic 
     | 
|
| 
       67 
65 
     | 
    
         
             
                def obtain_final_url __uri
         
     | 
| 
       68 
66 
     | 
    
         
             
                  c = Curl::Easy.new __uri
         
     | 
| 
       69 
67 
     | 
    
         
             
                  c.perform
         
     | 
| 
       70 
     | 
    
         
            -
                   
     | 
| 
       71 
     | 
    
         
            -
                   
     | 
| 
       72 
     | 
    
         
            -
             
     | 
| 
       73 
     | 
    
         
            -
             
     | 
| 
      
 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')
         
     | 
| 
       74 
76 
     | 
    
         
             
                end
         
     | 
| 
       75 
77 
     | 
    
         | 
| 
       76 
     | 
    
         
            -
                def  
     | 
| 
       77 
     | 
    
         
            -
                   
     | 
| 
       78 
     | 
    
         
            -
                   
     | 
| 
       79 
     | 
    
         
            -
                  @direct_link =  
     | 
| 
      
 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
         
     | 
| 
       80 
82 
     | 
    
         
             
                end
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
       81 
84 
     | 
    
         
             
              end
         
     | 
| 
       82 
85 
     | 
    
         
             
            end
         
     | 
    
        data/lib/kgmusic/version.rb
    CHANGED
    
    
    
        data/lib/kgmusic.rb
    CHANGED
    
    | 
         @@ -3,12 +3,12 @@ require "kgmusic/basemod" 
     | 
|
| 
       3 
3 
     | 
    
         | 
| 
       4 
4 
     | 
    
         
             
            module KgMusic
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
              class  
     | 
| 
      
 6 
     | 
    
         
            +
              class Client
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
       8 
8 
     | 
    
         
             
                include BaseMod
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
                attr_reader :artist, :album
         
     | 
| 
       11 
     | 
    
         
            -
                attr_reader : 
     | 
| 
      
 11 
     | 
    
         
            +
                attr_reader :result, :download_page, :direct_link
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
13 
     | 
    
         
             
                def initialize args
         
     | 
| 
       14 
14 
     | 
    
         
             
                  keys = [:artist, :album]
         
     | 
| 
         @@ -24,26 +24,21 @@ module KgMusic 
     | 
|
| 
       24 
24 
     | 
    
         
             
                protected
         
     | 
| 
       25 
25 
     | 
    
         | 
| 
       26 
26 
     | 
    
         
             
                def run
         
     | 
| 
       27 
     | 
    
         
            -
                  url = 'kibergrad.fm/search'
         
     | 
| 
       28 
     | 
    
         
            -
                  params = {:q => "#{@artist} - #{@album}", :p => "albums"}
         
     | 
| 
       29 
27 
     | 
    
         | 
| 
       30 
     | 
    
         
            -
                   
     | 
| 
       31 
     | 
    
         
            -
                   
     | 
| 
      
 28 
     | 
    
         
            +
                  url = 'kibergrad.fm/search'
         
     | 
| 
      
 29 
     | 
    
         
            +
                  params = { :q => "#{@artist} - #{@album}", :p => "albums" }
         
     | 
| 
      
 30 
     | 
    
         
            +
                  album_info = get_album_info url, params
         
     | 
| 
       32 
31 
     | 
    
         | 
| 
       33 
     | 
    
         
            -
                   
     | 
| 
      
 32 
     | 
    
         
            +
                  unless album_info.nil?
         
     | 
| 
       34 
33 
     | 
    
         
             
                    begin
         
     | 
| 
       35 
     | 
    
         
            -
                       
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
             
     | 
| 
       38 
     | 
    
         
            -
                         
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
       40 
     | 
    
         
            -
                        n = @info.css('h3').find_index do |e|
         
     | 
| 
       41 
     | 
    
         
            -
                          e.at('a').children.to_s === @album
         
     | 
| 
       42 
     | 
    
         
            -
                        end
         
     | 
| 
       43 
     | 
    
         
            -
                        @result = @info.css('h3')[n]
         
     | 
| 
      
 34 
     | 
    
         
            +
                      if album_info.size == 1
         
     | 
| 
      
 35 
     | 
    
         
            +
                        @download_page = album_info.css('h3').at('a').attr('href')
         
     | 
| 
      
 36 
     | 
    
         
            +
                      elsif album_info.size > 1
         
     | 
| 
      
 37 
     | 
    
         
            +
                        full_match_index = album_info.css('h3').find_index { |e| e.at('a').children.to_s === @album }
         
     | 
| 
      
 38 
     | 
    
         
            +
                        @result = album_info.css('h3')[full_match_index]
         
     | 
| 
       44 
39 
     | 
    
         
             
                        @download_page = @result.at('a').attr('href')
         
     | 
| 
       45 
40 
     | 
    
         
             
                      end
         
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
      
 41 
     | 
    
         
            +
                        get_direct_link @download_page
         
     | 
| 
       47 
42 
     | 
    
         
             
                    rescue
         
     | 
| 
       48 
43 
     | 
    
         
             
                    end
         
     | 
| 
       49 
44 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -55,15 +50,9 @@ module KgMusic 
     | 
|
| 
       55 
50 
     | 
    
         
             
                  outfile = File.join(ENV['HOME'], "#{@artist} - #{@album}.zip")
         
     | 
| 
       56 
51 
     | 
    
         
             
                  content_length = get_content_length url
         
     | 
| 
       57 
52 
     | 
    
         | 
| 
       58 
     | 
    
         
            -
                  if File.exist? outfile
         
     | 
| 
       59 
     | 
    
         
            -
                    fsize = File.size outfile
         
     | 
| 
       60 
     | 
    
         
            -
                  else
         
     | 
| 
       61 
     | 
    
         
            -
                    FileUtils.touch outfile
         
     | 
| 
       62 
     | 
    
         
            -
                    fsize = 0
         
     | 
| 
       63 
     | 
    
         
            -
                  end
         
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
53 
     | 
    
         
             
                  begin
         
     | 
| 
       66 
     | 
    
         
            -
                    f = File.open(outfile, 'ab')
         
     | 
| 
      
 54 
     | 
    
         
            +
                    f = File.open(outfile, 'ab+')
         
     | 
| 
      
 55 
     | 
    
         
            +
                    fsize = File.size outfile
         
     | 
| 
       67 
56 
     | 
    
         
             
                    c = Curl::Easy.new url do |curl|
         
     | 
| 
       68 
57 
     | 
    
         
             
                      curl.set(:resume_from, fsize) if File.exists? outfile
         
     | 
| 
       69 
58 
     | 
    
         
             
                      curl.on_body { |body| f.write body }
         
     | 
    
        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. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.0pre20160517
         
     | 
| 
       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- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2016-05-17 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: curb
         
     | 
| 
         @@ -109,6 +109,7 @@ files: 
     | 
|
| 
       109 
109 
     | 
    
         
             
            - bin/setup
         
     | 
| 
       110 
110 
     | 
    
         
             
            - kgmusic.gemspec
         
     | 
| 
       111 
111 
     | 
    
         
             
            - lib/kgmusic.rb
         
     | 
| 
      
 112 
     | 
    
         
            +
            - lib/kgmusic/api.rb
         
     | 
| 
       112 
113 
     | 
    
         
             
            - lib/kgmusic/basemod.rb
         
     | 
| 
       113 
114 
     | 
    
         
             
            - lib/kgmusic/version.rb
         
     | 
| 
       114 
115 
     | 
    
         
             
            homepage: https://github.com/vitvegl/kgmusic.git
         
     |