Class: MetalArchives::Parsers::Band
- Defined in:
- lib/metal_archives/parsers/band.rb
Overview
Band parser
Class Method Summary collapse
-
.map_params(query) ⇒ Object
Map attributes to MA attributes.
-
.parse_html(response) ⇒ Object
Parse main HTML page.
-
.parse_related_links_html(response) ⇒ Object
Parse related links HTML page.
-
.parse_releases_html(response) ⇒ Object
Parse releases HTML page.
-
.parse_similar_bands_html(response) ⇒ Object
Parse similar bands HTML page.
Methods inherited from Parser
Class Method Details
.map_params(query) ⇒ Object
Map attributes to MA attributes
Returns Hash
params
-
Hash
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/metal_archives/parsers/band.rb', line 22 def map_params(query) params = { bandName: query[:name] || "", exactBandMatch: (query[:exact] ? 1 : 0), genre: query[:genre] || "", yearCreationFrom: query[:year]&.begin || "", yearCreationTo: query[:year]&.end || "", bandNotes: query[:comment] || "", status: map_status(query[:status]), themes: query[:lyrical_themes] || "", location: query[:location] || "", bandLabelName: query[:label] || "", indieLabelBand: (query[:independent] ? 1 : 0), } params[:country] = [] Array(query[:country]).each do |country| params[:country] << (country.is_a?(ISO3166::Country) ? country.alpha2 : (country || "")) end params[:country] = params[:country].first if params[:country].size == 1 params end |
.parse_html(response) ⇒ Object
Parse main HTML page
Returns Hash
- Raises
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/metal_archives/parsers/band.rb', line 54 def parse_html(response) # Set default props props = { name: nil, aliases: [], logo: nil, photo: nil, country: nil, location: nil, status: nil, date_formed: nil, years_active: [], independent: nil, genres: [], lyrical_themes: [], members: [], } doc = Nokogiri::HTML response props[:name] = sanitize doc.css("#band_info .band_name a").first.content # Logo unless doc.css(".band_name_img").empty? logo_uri = URI doc.css(".band_name_img img").first.attr("src") props[:logo] = rewrite(logo_uri) end # Photo unless doc.css(".band_img").empty? photo_uri = URI doc.css(".band_img img").first.attr("src") props[:photo] = rewrite(photo_uri) end doc.css("#band_stats dl").each do |dl| dl.search("dt").each do |dt| content = sanitize(dt.next_element.content) next if content == "N/A" case dt.content when "Country of origin:" props[:country] = Country.parse(sanitize(dt.next_element.css("a").first.content)) when "Location:" props[:location] = content when "Status:" props[:status] = content.downcase.tr(" -", "_").to_sym when "Formed in:" props[:date_formed] = Parsers::Date.parse(content) when "Genre:" props[:genres] = Parsers::Genre.parse(content) when "Lyrical themes:" content.split(",").each do |theme| t = theme.split.map(&:capitalize) t.delete "(early)" t.delete "(later)" props[:lyrical_themes] << t.join(" ") end when /(Current|Last) label:/ props[:independent] = (content == "Unsigned/independent") # TODO: label when "Years active:" content.split(",").each do |range| # Aliases range.scan(/\(as ([^)]*)\)/).each { |name| props[:aliases] << name.first } # Ranges props[:years_active] << Parsers::Year.parse(range.gsub(/ *\(as ([^)]*)\) */, "")) end else raise Errors::ParserError, "Unknown token: #{dt.content}" end end end # Members proc = proc do |row| link = row.css("a") if link.any? # Artist name contains a link id = Integer(link.attr("href").text.split("/").last) name = sanitize link.text else # Artist name does not contain a link name = sanitize row.css("h3").text end r = row.css("td").last.text role, range = r.match(/(.*)\(([^(]*)\)/)&.captures range = Parsers::Year.parse(range) { id: id, name: name, years_active: range, role: sanitize(role), }.compact end doc.css("#band_tab_members_current .lineupRow").each do |row| props[:members] << proc.call(row).merge(current: true) end doc.css("#band_tab_members_past .lineupRow").each do |row| props[:members] << proc.call(row).merge(current: false) end props rescue StandardError => e e.backtrace.each { |b| MetalArchives.config.logger.error b } raise Errors::ParserError, e end |
.parse_related_links_html(response) ⇒ Object
Parse related links HTML page
Returns Hash
- Raises
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/metal_archives/parsers/band.rb', line 207 def (response) links = [] doc = Nokogiri::HTML response doc.css("#linksTableOfficial td a").each do |a| links << { url: a["href"], type: :official, title: a.content, } end doc.css("#linksTableOfficial_merchandise td a").each do |a| links << { url: a["href"], type: :merchandise, title: a.content, } end links rescue StandardError => e e.backtrace.each { |b| MetalArchives.config.logger.error b } raise Errors::ParserError, e end |
.parse_releases_html(response) ⇒ Object
Parse releases HTML page
Returns Array
- Raises
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/metal_archives/parsers/band.rb', line 239 def parse_releases_html(response) releases = [] doc = Nokogiri::HTML response doc.css("tbody tr td:first a").each do |a| id = a["href"].split("/").last.to_i releases << MetalArchives::Release.find(id) end releases rescue StandardError => e e.backtrace.each { |b| MetalArchives.config.logger.error b } raise Errors::ParserError, e end |
.parse_similar_bands_html(response) ⇒ Object
Parse similar bands HTML page
Returns Hash
- Raises
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/metal_archives/parsers/band.rb', line 181 def parse_similar_bands_html(response) similar = [] doc = Nokogiri::HTML response doc.css("#artist_list tbody tr").each do |row| similar << { id: row.css("td a").first["href"].split("/").last.to_i, score: row.css("td").last.content.strip, } end similar rescue StandardError => e MetalArchives.config.logger e. e.backtrace.each { |b| MetalArchives.config.logger.error b } raise Errors::ParserError, e end |