bandcamp 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,13 +10,13 @@ module BandCamp
10
10
  OptionParser.new do |opts|
11
11
  opts.banner = "Usage: example.rb [options]"
12
12
 
13
- opts.on("-t", "--true", "Do not download any mp3 files") do |v|
14
- options[:try] = v
15
- end
16
-
17
13
  opts.on("-d", "--[no-]debug", "Output debug information") do |v|
18
14
  options[:debug] = v
19
15
  end
16
+
17
+ opts.on("-a", "--all", "If the url does not contain songs but links to tracks/albums: download all those") do |v|
18
+ options[:all] = v
19
+ end
20
20
  end.parse!
21
21
  options
22
22
  end
@@ -13,11 +13,15 @@ module BandCamp
13
13
  def page_html
14
14
  unless @page_html
15
15
  puts "Getting content of \"#{@url}\"" if @options[:debug]
16
- @page_html = Net::HTTP.get(URI.parse(@url))
16
+ @page_html = Net::HTTP.get(parsed_url)
17
17
  end
18
18
  @page_html
19
19
  end
20
20
 
21
+ def parsed_url
22
+ @parsed_url ||= URI.parse(@url)
23
+ end
24
+
21
25
  def harmony_page
22
26
  unless @harmony_page
23
27
  # Assigning to a variable first to make the puts statements make sense
@@ -28,6 +32,13 @@ module BandCamp
28
32
  @harmony_page
29
33
  end
30
34
 
35
+ def is_album?
36
+ harmony_page.execute_js("TralbumData.current")
37
+ return true
38
+ rescue Johnson::Error
39
+ return false
40
+ end
41
+
31
42
  def songs
32
43
  @songs ||= harmony_page.execute_js("TralbumData.trackinfo").map do |song_object|
33
44
  Song.new(song_object.title.to_s, song_object.title_link.to_s, song_object.file.to_s, @options)
@@ -42,7 +53,38 @@ module BandCamp
42
53
  @album_name ||= harmony_page.execute_js("TralbumData.current.title")
43
54
  end
44
55
 
56
+ def urls_of_albums_on_this_page
57
+ hpricot.search(".ipCell h1 a").map { |a| extend_relative_url(a.attributes["href"]) }
58
+ end
59
+
45
60
  def download
61
+ if is_album?
62
+ download_songs_for_this_album
63
+ else
64
+ download_albums_on_this_page
65
+ end
66
+ end
67
+
68
+ def extend_relative_url(relative_url)
69
+ url = URI.parse(relative_url)
70
+ url.scheme = "http"
71
+ url.host = parsed_url.host
72
+ url.to_s
73
+ end
74
+
75
+ def path_for_download
76
+ File.join("download",
77
+ BandCamp::file_safe_string(band_name),
78
+ BandCamp::file_safe_string(album_name))
79
+ end
80
+
81
+ def hpricot
82
+ @hpricot ||= Hpricot.parse(page_html)
83
+ end
84
+
85
+ private
86
+
87
+ def download_songs_for_this_album
46
88
  dir = path_for_download
47
89
  unless @options[:try]
48
90
  `mkdir -p #{dir}`
@@ -61,18 +103,20 @@ module BandCamp
61
103
  download_album_art
62
104
  end
63
105
 
64
- def path_for_download
65
- File.join("download",
66
- BandCamp::file_safe_string(band_name),
67
- BandCamp::file_safe_string(album_name))
68
- end
69
-
70
- def hpricot
71
- @hpricot ||= Hpricot.parse(page_html)
106
+ def download_albums_on_this_page
107
+ if @options[:all]
108
+ puts "Downloading all albums" if @options[:debug]
109
+ urls_of_albums_on_this_page.sort.each do |album_url|
110
+ puts "Now downloading: #{album_url}" if @options[:debug]
111
+ Page.new(album_url, @options).download
112
+ end
113
+ else
114
+ puts "This url does not contain songs, but only a list of albums/tracks:"
115
+ urls_of_albums_on_this_page.sort.each { |u| puts "- #{u}" }
116
+ puts "You can either download those urls individually or pass the -a flag to download them all"
117
+ end
72
118
  end
73
119
 
74
- private
75
-
76
120
  def download_album_art
77
121
  # Assumes the directory where the album is downloaded already exists
78
122
  url = hpricot.search("#tralbumArt img").first.attributes["src"]
@@ -1,4 +1,4 @@
1
1
  module BandCamp
2
2
  # We're doing this because bundler did it this way
3
- VERSION = "0.0.4" unless defined?(::BandCamp::VERSION)
3
+ VERSION = "0.0.5" unless defined?(::BandCamp::VERSION)
4
4
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bandcamp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom ten Thij
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-10 00:00:00 +01:00
18
+ date: 2010-09-11 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency