bandcamp 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'band_camp'
4
+
5
+ ARGV.each do |url|
6
+ band = BandCamp::Band.new(url)
7
+ band.download
8
+ end
@@ -0,0 +1,7 @@
1
+ require "band_camp/band"
2
+
3
+ module BandCamp
4
+ def self.file_safe_string(string)
5
+ string.tr("^a-zA-Z0-9-_", "_").gsub(/_+/, "_")
6
+ end
7
+ end
@@ -0,0 +1,49 @@
1
+ require "band_camp/song"
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'harmony'
5
+
6
+ module BandCamp
7
+ class Band
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def page_html
13
+ unless @page_html
14
+ puts "Getting content of \"#{@url}\""
15
+ @page_html = Net::HTTP.get(URI.parse(@url))
16
+ end
17
+ @page_html
18
+ end
19
+
20
+ def harmony_page
21
+ unless @harmony_page
22
+ html = page_html
23
+ puts "Initializing headless browser"
24
+ @harmony_page = Harmony::Page.new(html)
25
+ end
26
+ @harmony_page
27
+ end
28
+
29
+ def songs
30
+ @songs ||= harmony_page.execute_js("TralbumData.trackinfo").map do |song_object|
31
+ Song.new(song_object.title.to_s, song_object.title_link.to_s, song_object.file.to_s)
32
+ end
33
+ end
34
+
35
+ def name
36
+ harmony_page.execute_js("BandData.name")
37
+ end
38
+
39
+ def download
40
+ dir = path_for_download
41
+ `mkdir -p #{dir}`
42
+ songs.each_with_index { |song, index| song.download(dir, index) }
43
+ end
44
+
45
+ def path_for_download
46
+ File.join("download", BandCamp::file_safe_string(name))
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module BandCamp
5
+ class Song
6
+ attr_reader :title, :title_link, :url
7
+
8
+ def initialize(title, title_link, url)
9
+ @title = title
10
+ @title_link = title_link
11
+ @url = url
12
+ end
13
+
14
+ def to_s
15
+ "title: #{title}"
16
+ end
17
+
18
+ def download(band_path, index = nil)
19
+ song_url = URI.parse(url)
20
+ res = Net::HTTP.start(song_url.host, song_url.port) {|http|
21
+ http.get("#{song_url.path}?#{song_url.query}")
22
+ }
23
+
24
+ song_name = BandCamp::file_safe_string(title)
25
+ if index
26
+ song_name = "%02d-%s" % [index + 1, song_name]
27
+ end
28
+ file_name = File.join(band_path, song_name + ".mp3")
29
+ puts "Saving #{file_name}"
30
+ File.open(file_name, "w") do |file|
31
+ file.puts res.body
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,4 @@
1
+ module BandCamp
2
+ # We're doing this because bundler did it this way
3
+ VERSION = "0.0.2" unless defined?(::BandCamp::VERSION)
4
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bandcamp
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Tom ten Thij
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-09 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: harmony
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: A utility to download the low quality mp3 files for a band on bandcamp.com
36
+ email:
37
+ - ruby@tomtenthij.nl
38
+ executables:
39
+ - band_camp_download
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - bin/band_camp_download
46
+ - lib/band_camp/band.rb
47
+ - lib/band_camp/song.rb
48
+ - lib/band_camp/version.rb
49
+ - lib/band_camp.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/tomtt/bandcamp
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.7
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: A utility to download the low quality mp3 files for a band on bandcamp.com
84
+ test_files: []
85
+