pzmp3_downloader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmZmNWE4ZTA5NjE0MDFjODBlNGQ5NzM5OThmYTlmNzFkOWQ0OGNjNg==
5
+ data.tar.gz: !binary |-
6
+ MzIyZWJlMzZmZDQ3N2U2MDZjMzU4MWY2NmIzYTY2NDVjZThjODA1OQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Mzc0ZmZlNWMyMTlhZWNmMDIwNjY4ZjkyMWQ0OTk1Y2JmZDZmNzIzMGU0ODVm
10
+ NGE0OGVmMjgyNTEyYmRlZmU3YjM4NGIyMDliYTYzZGE1NWIxNGUxMDUwNTRl
11
+ ZGZjZTJjMDZkM2IwNzE5ZDFlNTRhMWFiZDBjMmIyZTEyZmE2YjY=
12
+ data.tar.gz: !binary |-
13
+ NTUzZWQxYTljNWI5NWM2NWUzZTk0ZDUwYmU4MDkxM2Y2Mjk2ODhiOTBjNTUz
14
+ YWFmMGYwMGEzNDg4MWRmYTRlM2RiMjU2ZTdlODNkZGNlYzFiYTM5ZjI3Mzg3
15
+ NGI4MTkyMjEwNmYxZjQ5NzVjYzA4NjY1NjU1ZTNjOGRjNWMxMmY=
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # pzmp3_downloader
2
+ [![Code Climate](https://codeclimate.com/github/briandiaz/pzmp3_downloader/badges/gpa.svg)](https://codeclimate.com/github/briandiaz/pzmp3_downloader)
3
+
4
+ ## Information
5
+
6
+ pzmp3_downloader is a song downloader for http://pzmp3.com/
7
+
8
+ ## Getting Started
9
+ Just require the gem:
10
+
11
+ ```ruby
12
+ require 'pzmp3_downloader'
13
+ ```
14
+
15
+ Then call the downloader with:
16
+ ```ruby
17
+ downloader = Downloader::Pzmp3.new
18
+ ```
19
+
20
+ ### Album
21
+
22
+ Set the url of the album you want to download by:
23
+ ```ruby
24
+ downloader.download_album 'http://pzmp3.com/music/album/3727/Good-Girl-Gone-Bad.html'
25
+ ```
26
+ This will automatically create a folder named 'Good Girl Gone Bad' and will download every song (listed at the website) of the Rihanna's 'Good Girl Gone Bad'.
27
+
28
+ ### Single Song
29
+ If you want to download a single song:
30
+ ```ruby
31
+ downloader.download 'http://pzmp3.com/music/song/28579/The-Real-Slim-Shady.html'
32
+ ```
33
+ This will download the Eminem's 'The Real Slim Shady' song as a file called 'Eminem - The Real Slim Shady.mp3'
34
+
35
+ ## Contributors
36
+
37
+ Brian Díaz https://github.com/briandiaz
@@ -0,0 +1 @@
1
+ require 'pzmp3_downloader'
@@ -0,0 +1,69 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require_relative 'song'
4
+
5
+ module Downloader
6
+ class Pzmp3
7
+
8
+ # Get html document by making a request to the url
9
+ # with nokogiri
10
+ def get_html_doc(url)
11
+ Nokogiri::HTML(open(url))
12
+ end
13
+
14
+ # Return a list of links of songs that an album url contains
15
+ def get_album_songs_url(album_url)
16
+ html_doc = get_html_doc(album_url)
17
+ links = html_doc.css('a')
18
+ links.map {|link| (link.attribute('href').to_s =~ /mp3-song.html/) ? "http://pzmp3.com/"+link.attribute('href').to_s : ""}.sort.delete_if{|href| href.empty?}
19
+ end
20
+
21
+ # Parse a Song instance from a url
22
+ def parse_song(song_url)
23
+ html_doc = get_html_doc(song_url)
24
+ song_info = html_doc.css('table table').css('tr td').map{ |data| data.text.split(':')[1].sub!(" ", "") }
25
+ song_url = get_128kbps_mp3_url(html_doc)
26
+ Song.parse(song_info, song_url)
27
+ end
28
+
29
+ # Return the 128kbps Mp3 file from an specific html doc from a song url
30
+ def get_128kbps_mp3_url(html_doc)
31
+ links = html_doc.css('a')
32
+ mp3_urls = links.map {|link| (link.attribute('href').to_s.include? ".mp3") ? link.attribute('href').to_s : ""}.delete_if{|href| href.empty?}
33
+ mp3_urls.last
34
+ end
35
+
36
+ # Download the song
37
+ def download(song_url)
38
+ song = parse_song(song_url)
39
+ song_path = (@album_path) ? @album_path + "/" + song.file_name : song.file_name
40
+ web_file = open(song.url)
41
+ if web_file.status[0] == "200"
42
+ puts "Downloading #{song} ..."
43
+ File.open(song_path,'wb') do |data|
44
+ data.write web_file.read
45
+ end
46
+ puts "#{song} successfully downloaded!"
47
+ else
48
+ "Problems downloading file"
49
+ end
50
+ end
51
+
52
+ # Download all songs of a album specified by an url.
53
+ def download_album(url)
54
+ create_album_path(url)
55
+ songs = get_album_songs_url(url)
56
+ songs.each do |song|
57
+ download song
58
+ end
59
+ end
60
+
61
+ private
62
+ # Creates the album path
63
+ def create_album_path(url)
64
+ @album_path = url.split('/').last.gsub!(/-|mp3|songs|.html/,"").strip
65
+ Dir.mkdir @album_path unless File.exists?(@album_path)
66
+ end
67
+
68
+ end
69
+ end
data/lib/song.rb ADDED
@@ -0,0 +1,27 @@
1
+
2
+ class Song
3
+ attr_accessor :artist, :name, :url, :genre, :album
4
+
5
+ def initialize(name, album, artist, genre, url)
6
+ @name, @album, @artist, @genre, @url = name, album, artist, genre, url
7
+ end
8
+
9
+ def self.parse(args, song_url)
10
+ name, album, artist, genre = self.remove_prohibited_chars(args)
11
+ new(name, album, artist, genre, song_url)
12
+ end
13
+
14
+ def file_name
15
+ "#{@artist} - #{@name}.mp3"
16
+ end
17
+
18
+ def to_s
19
+ "#{@artist} - #{@name} [#{file_name}]"
20
+ end
21
+
22
+ private
23
+
24
+ def self.remove_prohibited_chars(args)
25
+ args.map{ |arg| arg.gsub(/\//," ") }
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ spec = Gem::Specification.new do |s|
2
+ s.name = "pzmp3_downloader"
3
+ s.version = "0.1.0"
4
+ s.author = "Brian Díaz"
5
+ s.email = "brian@briandiaz.me"
6
+ s.homepage = "https://github.com/briandiaz/pzmp3_downloader"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.description = "Downloads songs from pzmp3.com"
9
+ s.summary = "Ruby wrapper that downloads songs from pzmp3.com"
10
+ s.files = [
11
+ "README.md",
12
+ "lib/pzmp3_downloader.rb",
13
+ "lib/song.rb",
14
+ "pzmp3_downloader.gemspec",
15
+ "spec/spec_helper.rb",
16
+ "spec/song_spec.rb",
17
+ "bin/pzmp3_downloader"
18
+ ]
19
+ s.add_dependency('nokogiri', '>= 1.4.0')
20
+ end
data/spec/song_spec.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'song'
3
+
4
+ describe Song do
5
+
6
+ let(:song_name){ "Like A Pimp" }
7
+ let(:album_name){ "Welcome 2 Tha Chuuch" }
8
+ let(:artist_name){ "Snoop Dogg" }
9
+ let(:genre_name){ "Hip Hop Music" }
10
+ let(:link){ "http://pzmp3.com/music/song/32362/Like-A-Pimp-Snoop-Dogg-mp3-song.html" }
11
+
12
+ let(:song){ Song.new song_name, album_name, artist_name, genre_name, link }
13
+
14
+ subject{ song }
15
+
16
+ context "creation" do
17
+
18
+ it { should respond_to(:name) }
19
+ it { should respond_to(:artist) }
20
+ it { should respond_to(:album) }
21
+ it { should respond_to(:genre) }
22
+ it { should respond_to(:url) }
23
+
24
+ it "is named Like A Pimp" do
25
+ subject.name.should == song_name
26
+ end
27
+ it "its album name is Welcome 2 Tha Chuuch" do
28
+ subject.album.should == album_name
29
+ end
30
+ it "its artist name is Snoop Dogg" do
31
+ subject.artist.should == artist_name
32
+ end
33
+ it "its artist name is Hip Hop Music" do
34
+ subject.genre.should == genre_name
35
+ end
36
+
37
+ end
38
+
39
+ context "#file_name" do
40
+ it { subject.file_name.should eq("Snoop Dogg - Like A Pimp.mp3")}
41
+ end
42
+
43
+ context "#to_s" do
44
+ it { subject.to_s.should eq("#{artist_name} - #{song_name} [#{subject.file_name}]")}
45
+ end
46
+
47
+ context "#parse" do
48
+ let(:args_to_parse) {["Like A Pimp","Welcome 2 Tha Chuuch","Snoop Dogg","Hip Hop Music"] }
49
+ let(:url_to_parse) { "http://pzmp3.com/music/song/32362/Like-A-Pimp-Snoop-Dogg-mp3-song.html" }
50
+ it { Song.parse(args_to_parse, url_to_parse).should be_an_instance_of(Song) }
51
+ it {Song.parse(args_to_parse, url_to_parse).should equal?(Song.new(song_name, album_name, artist_name, genre_name, link))}
52
+ end
53
+
54
+ end
@@ -0,0 +1,92 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ expectations.syntax = :should
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ =begin
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is
55
+ # recommended. For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ =end
92
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pzmp3_downloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Díaz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.0
27
+ description: Downloads songs from pzmp3.com
28
+ email: brian@briandiaz.me
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - bin/pzmp3_downloader
35
+ - lib/pzmp3_downloader.rb
36
+ - lib/song.rb
37
+ - pzmp3_downloader.gemspec
38
+ - spec/song_spec.rb
39
+ - spec/spec_helper.rb
40
+ homepage: https://github.com/briandiaz/pzmp3_downloader
41
+ licenses: []
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.7
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Ruby wrapper that downloads songs from pzmp3.com
63
+ test_files: []