ehbrs-tools 0.25.1 → 0.26.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +30 -14
- data/lib/ehbrs/{core_ext.rb → tools/core_ext.rb} +0 -0
- data/lib/ehbrs/tools/runner/finances/bb_browser.rb +1 -1
- data/lib/ehbrs/tools/runner/fs/selected.rb +1 -1
- data/lib/ehbrs/tools/runner/music/lyrics.rb +69 -0
- data/lib/ehbrs/tools/runner/web_utils/videos/upload.rb +1 -1
- data/lib/ehbrs/tools/version.rb +1 -1
- data/vendor/aranha-parsers/aranha-parsers.gemspec +1 -0
- data/vendor/aranha-parsers/lib/aranha/parsers/rspec/setup.rb +1 -1
- data/vendor/aranha-parsers/lib/aranha/parsers/rspec/{source_target_fixtures_example.rb → shared_examples/source_target_fixtures.rb} +0 -0
- data/vendor/aranha-parsers/lib/aranha/parsers/source_address/fetch_content_error.rb +10 -0
- data/vendor/aranha-parsers/lib/aranha/parsers/source_address/http_get.rb +15 -2
- data/vendor/aranha-parsers/lib/aranha/parsers/version.rb +1 -1
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/compact.rb +22 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/fs_cache.rb +1 -1
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/compact.rb +20 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/string/delimited.rb +16 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/string_delimited.rb +70 -0
- data/vendor/eac_ruby_utils/lib/eac_ruby_utils/version.rb +1 -1
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/compact_spec.rb +20 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/compact_spec.rb +20 -0
- data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/string/delimited_spec.rb +34 -0
- data/vendor/ehbrs_ruby_utils/ehbrs_ruby_utils.gemspec +3 -0
- data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/music.rb +9 -0
- data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/version.rb +1 -1
- data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/videos/container.rb +31 -0
- data/vendor/ehbrs_ruby_utils/lib/ehbrs_ruby_utils/videos/stream.rb +1 -1
- data/vendor/ultimate_lyrics/Gemfile +5 -0
- data/vendor/ultimate_lyrics/bin/download_parser_spec_files.rb +94 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/field.rb +83 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/lyrics.rb +13 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/parser.rb +28 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider/exclude_item.rb +17 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider/extract_item.rb +21 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider/item.rb +48 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider/replace_fields.rb +13 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider/rule.rb +52 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider/url_format.rb +21 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider.rb +92 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/provider_search.rb +43 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/providers_data.xml +355 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/song_metadata/field.rb +24 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/song_metadata.rb +28 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics/version.rb +5 -0
- data/vendor/ultimate_lyrics/lib/ultimate_lyrics.rb +7 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec.rb +20 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/encyclopaedia_metallum__michael_jackson_thriller.source.html +8 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/encyclopaedia_metallum__michael_jackson_thriller.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/encyclopaedia_metallum__michael_jackson_thriller.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/encyclopaedia_metallum__milton_nascimento_rouxinol.source.html +8 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/encyclopaedia_metallum__milton_nascimento_rouxinol.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/encyclopaedia_metallum__milton_nascimento_rouxinol.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/hindilyrics_net_bollywood_songs__michael_jackson_thriller.source.html +193 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/hindilyrics_net_bollywood_songs__michael_jackson_thriller.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/hindilyrics_net_bollywood_songs__michael_jackson_thriller.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/hindilyrics_net_bollywood_songs__milton_nascimento_rouxinol.source.html +193 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/hindilyrics_net_bollywood_songs__milton_nascimento_rouxinol.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/hindilyrics_net_bollywood_songs__milton_nascimento_rouxinol.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyrics_com__michael_jackson_thriller.source.html +87 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyrics_com__michael_jackson_thriller.target.yaml +36 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyrics_com__michael_jackson_thriller.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyrics_com__milton_nascimento_rouxinol.source.html +4 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyrics_com__milton_nascimento_rouxinol.target.yaml +5 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyrics_com__milton_nascimento_rouxinol.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsplugin_com__michael_jackson_thriller.source.html +2 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsplugin_com__michael_jackson_thriller.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsplugin_com__michael_jackson_thriller.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsplugin_com__milton_nascimento_rouxinol.source.html +2 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsplugin_com__milton_nascimento_rouxinol.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsplugin_com__milton_nascimento_rouxinol.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsreg_com__milton_nascimento_rouxinol.source.html +101 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsreg_com__milton_nascimento_rouxinol.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/lyricsreg_com__milton_nascimento_rouxinol.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/mp3lyrics_org__michael_jackson_thriller.source.html +29 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/mp3lyrics_org__michael_jackson_thriller.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/mp3lyrics_org__michael_jackson_thriller.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/mp3lyrics_org__milton_nascimento_rouxinol.source.html +29 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/mp3lyrics_org__milton_nascimento_rouxinol.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/mp3lyrics_org__milton_nascimento_rouxinol.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/songlyrics_com__michael_jackson_thriller.source.html +1298 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/songlyrics_com__michael_jackson_thriller.target.yaml +3 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/parser_spec_files/songlyrics_com__michael_jackson_thriller.url +1 -0
- data/vendor/ultimate_lyrics/spec/lib/ultimate_lyrics/provider_search_spec.rb +72 -0
- data/vendor/ultimate_lyrics/spec/rubocop_spec.rb +3 -0
- data/vendor/ultimate_lyrics/spec/spec_helper.rb +4 -0
- data/vendor/ultimate_lyrics/ultimate_lyrics.gemspec +19 -0
- metadata +75 -6
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
ENV['BUNDLE_GEMFILE'] = ::File.expand_path('../Gemfile', __dir__)
|
5
|
+
require 'rubygems'
|
6
|
+
require 'bundler/setup'
|
7
|
+
$LOAD_PATH.unshift("#{__dir__}/../lib")
|
8
|
+
|
9
|
+
require 'ultimate_lyrics/song_metadata'
|
10
|
+
require 'ultimate_lyrics/provider'
|
11
|
+
require 'ultimate_lyrics/provider_search'
|
12
|
+
require 'aranha/parsers/source_address'
|
13
|
+
require 'aranha/parsers/source_address/fetch_content_error'
|
14
|
+
require 'eac_ruby_utils/core_ext'
|
15
|
+
|
16
|
+
class OriginalContentDownload
|
17
|
+
enable_simple_cache
|
18
|
+
common_constructor :song_metadata, :provider
|
19
|
+
|
20
|
+
def run
|
21
|
+
puts "Fetch content for #{provider_search}..."
|
22
|
+
source_content.if_present do |v|
|
23
|
+
source_file.write(v)
|
24
|
+
url_file.write(provider_search.url.strip + "\n")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def basename_file_uncached
|
31
|
+
provider.name.variableize + '__' + "#{song_metadata.artist}_#{song_metadata.title}".variableize
|
32
|
+
end
|
33
|
+
|
34
|
+
def fixtures_dir_uncached
|
35
|
+
__dir__.to_pathname.parent.join('spec', 'lib', 'ultimate_lyrics', 'parser_spec_files')
|
36
|
+
end
|
37
|
+
|
38
|
+
def provider_search_uncached
|
39
|
+
::UltimateLyrics::ProviderSearch.new(provider, song_metadata)
|
40
|
+
end
|
41
|
+
|
42
|
+
def source_content
|
43
|
+
r = ::Aranha::Parsers::SourceAddress.detect_sub(provider_search.url).content
|
44
|
+
puts 'SUCCESS: content fetched'
|
45
|
+
r
|
46
|
+
rescue ::Aranha::Parsers::SourceAddress::FetchContentError => e
|
47
|
+
puts "ERROR: #{e.message}"
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def source_file
|
52
|
+
fixtures_dir.join("#{basename_file}.source.html")
|
53
|
+
end
|
54
|
+
|
55
|
+
def url_file
|
56
|
+
fixtures_dir.join("#{basename_file}.url")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class OriginalContentDownloads
|
61
|
+
SONG_METADATAS = [{
|
62
|
+
artist: 'Michael Jackson',
|
63
|
+
title: 'Thriller',
|
64
|
+
album: 'Thriller',
|
65
|
+
track: '4',
|
66
|
+
year: '1982'
|
67
|
+
}, {
|
68
|
+
artist: 'Milton Nascimento',
|
69
|
+
title: 'Rouxinol',
|
70
|
+
album: 'Nascimento',
|
71
|
+
track: '5',
|
72
|
+
year: '1997'
|
73
|
+
}].freeze
|
74
|
+
|
75
|
+
def run
|
76
|
+
downloads.each(&:run)
|
77
|
+
end
|
78
|
+
|
79
|
+
def downloads
|
80
|
+
song_metadatas.lazy.flat_map do |song_metadata|
|
81
|
+
providers.map { |provider| ::OriginalContentDownload.new(song_metadata, provider) }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def song_metadatas
|
86
|
+
SONG_METADATAS.map { |data| ::UltimateLyrics::SongMetadata.new(data) }
|
87
|
+
end
|
88
|
+
|
89
|
+
def providers
|
90
|
+
::UltimateLyrics::Provider.all
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
::OriginalContentDownloads.new.run
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UltimateLyrics
|
4
|
+
class Field
|
5
|
+
DATA = {
|
6
|
+
'artist' => %w[artist lower],
|
7
|
+
'artist2' => %w[artist lower nospace],
|
8
|
+
'album' => %w[album lower],
|
9
|
+
'album2' => %w[album lower nospace],
|
10
|
+
'title' => %w[title lower],
|
11
|
+
'Artist' => %w[artist],
|
12
|
+
'Album' => %w[album],
|
13
|
+
'ARTIST' => %w[artist upper],
|
14
|
+
'year' => %w[year pretty],
|
15
|
+
'Title' => %w[title],
|
16
|
+
'Title2' => %w[title titlecase],
|
17
|
+
'a' => %w[artist firstchar],
|
18
|
+
'track' => %w[track number]
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
BEGIN_DELIMITER = '{'
|
22
|
+
END_DELIMITER = '}'
|
23
|
+
|
24
|
+
class << self
|
25
|
+
# @return [Array<UltimateLyrics::Field>]
|
26
|
+
def all
|
27
|
+
@all ||= DATA.map do |name, modifiers|
|
28
|
+
source_attr = modifiers.shift
|
29
|
+
new(name, source_attr, modifiers)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [UltimateLyrics::Field]
|
34
|
+
def by_name(name)
|
35
|
+
all.find { |field| field.name == name } ||
|
36
|
+
raise("No field with name \"#{name}\" (Available: #{all.values})")
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Array<UltimateLyrics::Field>]
|
40
|
+
def from_string(string)
|
41
|
+
names_from_string(string).map { |name| by_name(name) }
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Array<String>]
|
45
|
+
def names_from_string(string)
|
46
|
+
string.scan(/#{::Regexp.quote(BEGIN_DELIMITER)}[^\}]+#{::Regexp.quote(END_DELIMITER)}/)
|
47
|
+
.map { |s| s.delimited_inner(BEGIN_DELIMITER, END_DELIMITER) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
common_constructor :name, :source_attribute, :modifiers
|
52
|
+
|
53
|
+
def apply(string, value)
|
54
|
+
string.gsub(apply_pattern, value)
|
55
|
+
end
|
56
|
+
|
57
|
+
def apply_pattern
|
58
|
+
/#{::Regexp.quote(BEGIN_DELIMITER)}#{::Regexp.quote(name)}#{::Regexp.quote(END_DELIMITER)}/
|
59
|
+
end
|
60
|
+
|
61
|
+
def value(value)
|
62
|
+
modifiers.inject(value.to_s) { |a, e| send(e, a) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_s
|
66
|
+
name
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def firstchar(string)
|
72
|
+
string[0]
|
73
|
+
end
|
74
|
+
|
75
|
+
def lower(string)
|
76
|
+
string.downcase
|
77
|
+
end
|
78
|
+
|
79
|
+
def nospace(string)
|
80
|
+
string.gsub(/\s/, '')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
|
5
|
+
module UltimateLyrics
|
6
|
+
class Parser
|
7
|
+
common_constructor :provider, :original_content
|
8
|
+
|
9
|
+
def original_content_invalid?
|
10
|
+
provider.invalid_indicators.any? { |v| original_content.include?(v) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def result
|
14
|
+
return nil if original_content_invalid?
|
15
|
+
|
16
|
+
sanitize_text(provider.rules.inject(original_content) { |a, e| e.apply(a) })
|
17
|
+
end
|
18
|
+
|
19
|
+
def sanitize_text(text)
|
20
|
+
r = text.to_s.gsub("\t", ' ').gsub("\r", '').gsub(/ +/, ' ').gsub(/\n{2,}/m, "\n\n").strip
|
21
|
+
r.present? ? "#{r}\n" : nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def url?
|
25
|
+
provider.extract_rules.any?(&:url?)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ultimate_lyrics/provider/item'
|
4
|
+
|
5
|
+
module UltimateLyrics
|
6
|
+
class Provider
|
7
|
+
class ExcludeItem < ::UltimateLyrics::Provider::Item
|
8
|
+
def apply_from_delimiters(source)
|
9
|
+
source.delimited_without_outer(begin_with, end_with)
|
10
|
+
end
|
11
|
+
|
12
|
+
def apply_from_tag(source)
|
13
|
+
source.delimited_without_outer(tag, "</#{tag_name}>")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ultimate_lyrics/provider/item'
|
4
|
+
|
5
|
+
module UltimateLyrics
|
6
|
+
class Provider
|
7
|
+
class ExtractItem < ::UltimateLyrics::Provider::Item
|
8
|
+
def apply_from_delimiters(source)
|
9
|
+
source.delimited_inner(begin_with, end_with)
|
10
|
+
end
|
11
|
+
|
12
|
+
def apply_from_tag(source)
|
13
|
+
source.delimited_inner(tag, "</#{tag_name}>")
|
14
|
+
end
|
15
|
+
|
16
|
+
def apply_from_url(source)
|
17
|
+
url.gsub('{id}', source)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UltimateLyrics
|
4
|
+
class Provider
|
5
|
+
class Item
|
6
|
+
METHODS_ATTRIBUTES = { tag: :tag, begin_with: :begin, end_with: :end, url: :url }.freeze
|
7
|
+
TAG_NAME_PARSER = /<(\w+).*>/.to_parser { |m| m[1] }
|
8
|
+
|
9
|
+
common_constructor :rule, :node
|
10
|
+
|
11
|
+
METHODS_ATTRIBUTES.each do |method_name, attr|
|
12
|
+
define_method method_name do
|
13
|
+
::CGI.unescapeHTML(node.attribute(attr.to_s).if_present('', &:text)).presence
|
14
|
+
end
|
15
|
+
|
16
|
+
define_method "#{method_name}?" do
|
17
|
+
send(method_name).present?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def delimiters?
|
22
|
+
begin_with? || end_with?
|
23
|
+
end
|
24
|
+
|
25
|
+
def apply(source)
|
26
|
+
%w[url tag delimiters].each do |m|
|
27
|
+
next unless send("#{m}?")
|
28
|
+
|
29
|
+
return send("apply_from_#{m}", source)
|
30
|
+
end
|
31
|
+
|
32
|
+
raise 'Invalid branch hit'
|
33
|
+
end
|
34
|
+
|
35
|
+
def tag_name
|
36
|
+
TAG_NAME_PARSER.parse(tag)
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_s
|
40
|
+
self.class.name.demodulize + '[' + METHODS_ATTRIBUTES
|
41
|
+
.select { |m, _a| send(m).present? }
|
42
|
+
.map { |m, a| "#{a}: #{send(m)}" }
|
43
|
+
.join(', ') +
|
44
|
+
']'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UltimateLyrics
|
4
|
+
class Provider
|
5
|
+
class ReplaceFields
|
6
|
+
common_constructor :metadata, :source_string
|
7
|
+
|
8
|
+
def result
|
9
|
+
fields_found.inject(source_string) { |a, e| a.gsub("{#{e}}", metadata.send(e)) }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UltimateLyrics
|
4
|
+
class Provider
|
5
|
+
class Rule
|
6
|
+
common_constructor :node
|
7
|
+
|
8
|
+
def items
|
9
|
+
node.xpath('./item').map { |v| item_class.new(self, v) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def item_class
|
13
|
+
::UltimateLyrics::Provider.const_get(type.camelize + 'Item')
|
14
|
+
end
|
15
|
+
|
16
|
+
def apply(string)
|
17
|
+
url? ? apply_with_url(string) : apply_without_url(string)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
"Rule[#{type} | #{items.join(', ')}]"
|
22
|
+
end
|
23
|
+
|
24
|
+
def type
|
25
|
+
node.name
|
26
|
+
end
|
27
|
+
|
28
|
+
def url?
|
29
|
+
items.any?(&:url?)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def apply_without_url(string)
|
35
|
+
items.inject(string) { |a, e| e.apply(a) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def apply_with_url(string)
|
39
|
+
url_item, id_item = url_items
|
40
|
+
id_item.apply(string).if_present { |v| url_item.apply(v) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def url_items
|
44
|
+
raise "There is more than 2 items in #{rule}" if items.count != 2
|
45
|
+
|
46
|
+
url_item = items.find(&:url?) || raise("No URL item found in #{rule}")
|
47
|
+
id_item = items.find(&:delimiters?) || raise("No ID item found in #{rule}")
|
48
|
+
[url_item, id_item]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module UltimateLyrics
|
4
|
+
class Provider
|
5
|
+
class UrlFormat
|
6
|
+
common_constructor :node
|
7
|
+
|
8
|
+
def apply(value)
|
9
|
+
replace_chars.inject(value) { |a, e| a.gsub(e, with) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def replace_chars
|
13
|
+
::CGI.unescapeHTML(node.attribute('replace').text).each_char.to_a
|
14
|
+
end
|
15
|
+
|
16
|
+
def with
|
17
|
+
node.attribute('with').text
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'ultimate_lyrics/field'
|
6
|
+
|
7
|
+
module UltimateLyrics
|
8
|
+
class Provider
|
9
|
+
require_sub __FILE__
|
10
|
+
|
11
|
+
class << self
|
12
|
+
# @return [Array<UltimateLyrics::Provider>]
|
13
|
+
def all
|
14
|
+
@all ||= ::Nokogiri::XML(
|
15
|
+
::File.new(::File.join(__dir__, 'providers_data.xml'))
|
16
|
+
).xpath('/lyricproviders/provider').map { |node| new(node) }
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [UltimateLyrics::Provider]
|
20
|
+
def by_name(name)
|
21
|
+
by_attribute('name', name)
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [UltimateLyrics::Provider]
|
25
|
+
def by_identifier(identifier)
|
26
|
+
by_attribute('identifier', identifier)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [UltimateLyrics::Provider]
|
30
|
+
def by_attribute(attribute, value)
|
31
|
+
all.find { |provider| provider.send(attribute) == value } ||
|
32
|
+
raise("No provider with name \"#{value}\" (Available: " +
|
33
|
+
all.map { |p| p.send(attrbute) }.join(', ') + ')')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
common_constructor :node
|
38
|
+
|
39
|
+
def extract_rules
|
40
|
+
node.xpath('./extract').map do |v|
|
41
|
+
::UltimateLyrics::Provider::Rule.new(v)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def exclude_rules
|
46
|
+
node.xpath('./exclude').map do |v|
|
47
|
+
::UltimateLyrics::Provider::Rule.new(v)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def identifier
|
52
|
+
name.variableize
|
53
|
+
end
|
54
|
+
|
55
|
+
def invalid_indicators
|
56
|
+
node.xpath('/invalidIndicator/@value').map(&:text)
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [String]
|
60
|
+
def name
|
61
|
+
node.attribute('name').text
|
62
|
+
end
|
63
|
+
|
64
|
+
def rules
|
65
|
+
extract_rules + exclude_rules
|
66
|
+
end
|
67
|
+
|
68
|
+
# @return [String]
|
69
|
+
def url
|
70
|
+
node.attribute('url').text
|
71
|
+
end
|
72
|
+
|
73
|
+
# @return [Array<UltimateLyrics::Field>]
|
74
|
+
def url_fields
|
75
|
+
::UltimateLyrics::Field.from_string(url)
|
76
|
+
end
|
77
|
+
|
78
|
+
def url_formats
|
79
|
+
node.xpath('./urlFormat').map do |v|
|
80
|
+
::UltimateLyrics::Provider::UrlFormat.new(v)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def title
|
85
|
+
node.attribute('text').text
|
86
|
+
end
|
87
|
+
|
88
|
+
def to_s
|
89
|
+
name
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aranha/parsers/source_address'
|
4
|
+
require 'eac_ruby_utils/core_ext'
|
5
|
+
require 'ultimate_lyrics/lyrics'
|
6
|
+
require 'ultimate_lyrics/parser'
|
7
|
+
|
8
|
+
module UltimateLyrics
|
9
|
+
class ProviderSearch
|
10
|
+
common_constructor :provider, :song_metadata
|
11
|
+
|
12
|
+
# @return [String]
|
13
|
+
def url
|
14
|
+
url_fields.inject(provider.url) { |a, e| e.apply(a) }
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return []
|
18
|
+
def url_fields
|
19
|
+
provider.url_fields.map { |provider_url_field| song_metadata.field(provider_url_field) }
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [UltimateLyrics::Lyrics]
|
23
|
+
def lyrics
|
24
|
+
::UltimateLyrics::Lyrics.new(song_metadata, provider.name, parser.result)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [UltimateLyrics::Parser]
|
28
|
+
def lyrics_original_text
|
29
|
+
::Aranha::Parsers::SourceAddress.detect_sub(url).content
|
30
|
+
rescue ::Aranha::Parsers::SourceAddress::FetchContentError
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [UltimateLyrics::Parser]
|
35
|
+
def parser
|
36
|
+
::UltimateLyrics::Parser.new(provider, lyrics_original_text)
|
37
|
+
end
|
38
|
+
|
39
|
+
def to_s
|
40
|
+
"ProviderSearch[#{provider} | #{song_metadata}]"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|