rjl-allmusic 0.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/allmusic.rb +29 -44
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b155dfb8ba731eb2b83141639618bc488cf4bf69
4
- data.tar.gz: a150392aa792c92b30be443bb676a0edaab3be52
3
+ metadata.gz: 787431b076c2ee822666ea04063ce6b8cda6c27f
4
+ data.tar.gz: c739491f05b3407a9ee54a7b0fa76dd0a6dbd501
5
5
  SHA512:
6
- metadata.gz: 4b3ce309787ceda0bcc62d004e26b5e5e4de19b681d3954c90a5734263f9eb8716d6547255c4852322efd60158efff8b447e6b37bc2a9e40923ed03ee44bff86
7
- data.tar.gz: 065de01a7543af3c01a1f1dba95f3793d6281f3186c80d8bd82eba4cde83255c9f39127d46f9b49854a7c66cbff2d1ab003ea063c82d1afd7dbb462e3483e4f9
6
+ metadata.gz: ea7d62c96dae2e47f7a88a6ec58458decea7aa7565cfa039bd6448a50d9739f0c1323a9f97201444dd3512385cf1bac5a65f7ca190faf43721667a5c89765d38
7
+ data.tar.gz: 557d55e82be8b31d40194b9b7cb6517a30f60ccfd188cd790e6156b4cf6355775893a5c4d4bb1b631e957eafd26267b1bd9edb4919f5ed021610ac6516ee3033
data/lib/allmusic.rb CHANGED
@@ -7,26 +7,24 @@ require 'fuzzystringmatch' #https://github.com/kiyoka/fuzzy-string-match
7
7
  # @param [String]
8
8
  class Allmusic
9
9
 
10
- # @!attribute album
11
- # @return [String] Album name
12
- attr_accessor :album
13
- # @!attribute artist
14
- # @return [String] Artist name
15
- attr_accessor :artist
16
- attr_reader :genres
17
- attr_reader :styles
18
- attr_reader :genre
19
- attr_reader :style
20
-
21
10
  ARTIST_SEARCH_URL = "http://www.allmusic.com/search/artists/"
22
11
 
23
- def initialize( artist = nil, album = nil)
12
+ def initialize( artist = nil, album = nil )
24
13
  @artist = artist
25
14
  @album = album
26
- @genre = nil
27
- @style = nil
15
+ @metadata = get_metadata( @artist, @album )
16
+ end
17
+
18
+ def genres
19
+ return @metadata[:genres]
28
20
  end
29
21
 
22
+ def styles
23
+ return @metadata[:styles]
24
+ end
25
+
26
+ private
27
+
30
28
  # return a list of the albums genre/styles
31
29
  # @param album_page [Nokogiri] the page node to parse
32
30
  # @param type [String] 'genre' | 'styles'
@@ -41,20 +39,25 @@ class Allmusic
41
39
  end
42
40
 
43
41
  # Sets @genre and @style for @album, @artist
44
- def get_meta
45
-
42
+ def get_metadata( artist, album )
43
+ metadata = {:genres => [], :styles => []}
46
44
  # search for artist page e.g. http://www.allmusic.com/search/artists/abba
47
- artist_search_url = make_url(ARTIST_SEARCH_URL, @artist)
48
- artist_search_page = Nokogiri::HTML(open(artist_search_url))
45
+ artist_search_url = make_url(ARTIST_SEARCH_URL, artist)
46
+ artist_search_page = nil
47
+ begin
48
+ artist_search_page = Nokogiri::HTML(open(artist_search_url))
49
+ rescue
50
+ return
51
+ end
49
52
 
50
53
  if no_search_result?(artist_search_page)
51
- raise "Couldn't find artist '#{@artist}'"
52
- exit
54
+ # raise "Couldn't find artist '#{@artist}'"
55
+ return
53
56
  end
54
57
 
55
58
  # get the url of the artist page
56
59
  artist_urls = artist_search_page.xpath("//ul[@class='search-results']//div[@class='name']/a")
57
- artist_url = best_match(@artist, artist_urls)
60
+ artist_url = best_match(artist, artist_urls)
58
61
 
59
62
  # get the artist discography page
60
63
  album_search_page = make_url(artist_url, '/discography/all')
@@ -62,31 +65,19 @@ class Allmusic
62
65
 
63
66
  # get album link
64
67
  album_urls = artist_discography_page.xpath("//td[@class='title']/a[1]")
65
- album_url = best_match(@album, album_urls)
68
+ album_url = best_match(album, album_urls)
66
69
 
67
70
  unless album_url.nil?
68
71
  # get album page
69
72
  begin
70
73
  album_page = Nokogiri::HTML(open(album_url))
71
- @genres = parse( album_page, 'genre' )
72
- @styles = parse( album_page, 'styles')
73
- # # get genre
74
- # # TODO: Improve this is there are more than one
75
- # @genre = album_page.xpath("//div[@class='genre']//a[1]").text
76
- # # get style
77
- # @style = album_page.xpath("//div[@class='styles']//a[1]").text
74
+ metadata[:genres] = parse( album_page, 'genre' )
75
+ metadata[:styles] = parse( album_page, 'styles')
78
76
  rescue
79
- puts ">> ERROR: Couldn't open #{album_url} for #{@artist} / #{@album}"
77
+ puts ">> ERROR: Couldn't open #{album_url} for #{artist} / #{album}"
80
78
  end
81
79
  end
82
- end
83
-
84
- def genre
85
- return @genres[0]
86
- end
87
-
88
- def style
89
- return @styles[0]
80
+ return metadata
90
81
  end
91
82
 
92
83
  # @return [URL] Joins URL parts
@@ -120,10 +111,4 @@ class Allmusic
120
111
  end
121
112
  return best_url
122
113
  end
123
-
124
- def debug( prefix = "debug", message )
125
- puts "-"*50
126
- puts "#{prefix}: #{message}"
127
- puts "="*50
128
- end
129
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rjl-allmusic
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lyon