lyricfy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +12 -0
- data/lib/lyricfy/lyric_provider.rb +17 -0
- data/lib/lyricfy/providers/metro_lyrics.rb +32 -0
- data/lib/lyricfy/providers/wikia.rb +28 -0
- data/lib/lyricfy/uri_helper.rb +18 -0
- data/lib/lyricfy/version.rb +3 -0
- data/lib/lyricfy.rb +42 -0
- data/lyricfy.gemspec +25 -0
- data/test/fixtures/vcr_cassettes/metro_lyrics_200.yml +490 -0
- data/test/fixtures/vcr_cassettes/metro_lyrics_404.yml +51 -0
- data/test/fixtures/vcr_cassettes/wikia_200.yml +2314 -0
- data/test/fixtures/vcr_cassettes/wikia_404.yml +1426 -0
- data/test/lyricfy_test.rb +47 -0
- data/test/providers/metro_lyrics_test.rb +37 -0
- data/test/providers/wikia_test.rb +38 -0
- data/test/test_helper.rb +12 -0
- metadata +143 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Javier Hidalgo
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Lyricfy
|
2
|
+
|
3
|
+
Song Lyrics for your Ruby apps.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'lyricfy'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install lyricfy
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
You need to create an instance of the <code>Lyricfy::Fetcher</code> class and send the <code>#search</code> message with the artist name and song title respectively. The following example shows the most basic usage.
|
22
|
+
|
23
|
+
fetcher = Lyricfy::Fetcher.new
|
24
|
+
lyric = fetcher.search 'Coldplay', 'Viva la vida'
|
25
|
+
puts lyric.body.join('\n')
|
26
|
+
|
27
|
+
The <code>Lyricfy::Fetcher#search</code> method returns an OpenStruct object with the following methods:
|
28
|
+
|
29
|
+
- artist
|
30
|
+
- song
|
31
|
+
- body
|
32
|
+
|
33
|
+
Where artist is the artist name, song is the song title and body is an Array of the lyric paragraphs.
|
34
|
+
|
35
|
+
### How does it work?
|
36
|
+
|
37
|
+
Under the hood, this library fetch the songs lyrics by scraping some websites called "Providers". The currently supported providers are:
|
38
|
+
|
39
|
+
- [Wikia](http://lyrics.wikia.com/Lyrics_Wiki)
|
40
|
+
- [MetroLyrics](http://www.metrolyrics.com/)
|
41
|
+
|
42
|
+
By default this gem will recursively search for the lyric on each of the providers, if the lyric is not found it will return nil. You can also tell Lyricfy to only use the provider(s) that you want.
|
43
|
+
|
44
|
+
fetcher = Lyricfy::Fetcher.new(:metro_lyrics)
|
45
|
+
lyric = fetcher.search 'Coldplay', 'Viva la vida'
|
46
|
+
puts lyric.body.join('\n')
|
47
|
+
|
48
|
+
In this case Lyricfy will look for the lyric only on [MetroLyrics](http://www.metrolyrics.com/).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Lyricfy
|
2
|
+
class LyricProvider
|
3
|
+
attr_accessor :base_url, :url, :parameters
|
4
|
+
|
5
|
+
def initialize(parameters = {})
|
6
|
+
self.parameters = parameters
|
7
|
+
end
|
8
|
+
|
9
|
+
def search
|
10
|
+
begin
|
11
|
+
data = open(self.url)
|
12
|
+
rescue OpenURI::HTTPError
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Lyricfy
|
2
|
+
class MetroLyrics < Lyricfy::LyricProvider
|
3
|
+
include URIHelper
|
4
|
+
|
5
|
+
def initialize(parameters)
|
6
|
+
super(parameters)
|
7
|
+
self.base_url = "http://m.metrolyrics.com/"
|
8
|
+
self.url = URI.escape(self.base_url + format_parameters)
|
9
|
+
end
|
10
|
+
|
11
|
+
def search
|
12
|
+
if data = super
|
13
|
+
html = Nokogiri::HTML(data)
|
14
|
+
container = html.css('p.lyricsbody').first || html.css('p.gnlyricsbody').first
|
15
|
+
elements = container.children.to_a
|
16
|
+
paragraphs = elements.select { |ele| ele.text? }
|
17
|
+
paragraphs.map! { |paragraph| paragraph.text.strip.chomp if paragraph.text != "\n" }.reject! { |ele| ele.empty? }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def prepare_parameter(parameter)
|
23
|
+
parameter.downcase.split(' ').map { |w| w.gsub(/\W/, '') }.join('-')
|
24
|
+
end
|
25
|
+
|
26
|
+
def format_parameters
|
27
|
+
artist_name = prepare_parameter(self.parameters[:artist_name])
|
28
|
+
song_name = prepare_parameter(self.parameters[:song_name])
|
29
|
+
"#{song_name}-lyrics-#{artist_name}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Lyricfy
|
2
|
+
class Wikia < Lyricfy::LyricProvider
|
3
|
+
include URIHelper
|
4
|
+
|
5
|
+
def initialize(parameters)
|
6
|
+
super(parameters)
|
7
|
+
self.base_url = "http://lyrics.wikia.com/"
|
8
|
+
self.url = URI.escape(self.base_url + format_parameters)
|
9
|
+
end
|
10
|
+
|
11
|
+
def search
|
12
|
+
if data = super
|
13
|
+
html = Nokogiri::HTML(data)
|
14
|
+
container = html.css('div.lyricbox').first
|
15
|
+
elements = container.children.to_a
|
16
|
+
paragraphs = elements.select { |ele| ele.text? }
|
17
|
+
paragraphs.map! { |paragraph| paragraph.text.strip.chomp if paragraph.text != "\n" }.reject! { |ele| ele.nil? }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def format_parameters
|
23
|
+
artist_name = tilde_to_vocal(self.parameters[:artist_name]).gsub(" ", "_")
|
24
|
+
song_name = tilde_to_vocal(self.parameters[:song_name]).gsub(" ", "_")
|
25
|
+
"#{artist_name}:#{song_name}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Lyricfy
|
4
|
+
module URIHelper
|
5
|
+
def tilde_to_vocal(string)
|
6
|
+
string.split('').map do |c|
|
7
|
+
case c
|
8
|
+
when "á" then "a"
|
9
|
+
when "é" then "e"
|
10
|
+
when "í" then "i"
|
11
|
+
when "ó" then "o"
|
12
|
+
when "ú" then "u"
|
13
|
+
else c
|
14
|
+
end
|
15
|
+
end.join('')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/lyricfy.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require "open-uri"
|
3
|
+
require "nokogiri"
|
4
|
+
require_relative "lyricfy/version"
|
5
|
+
require_relative "lyricfy/uri_helper"
|
6
|
+
require_relative "lyricfy/lyric_provider"
|
7
|
+
require_relative "lyricfy/providers/wikia"
|
8
|
+
require_relative "lyricfy/providers/metro_lyrics"
|
9
|
+
|
10
|
+
module Lyricfy
|
11
|
+
class Fetcher
|
12
|
+
attr_reader :providers
|
13
|
+
|
14
|
+
def initialize(*args)
|
15
|
+
@providers = {
|
16
|
+
wikia: Wikia,
|
17
|
+
metro_lyrics: MetroLyrics
|
18
|
+
}
|
19
|
+
|
20
|
+
if !args.empty?
|
21
|
+
passed_providers = {}
|
22
|
+
args.each do |provider|
|
23
|
+
raise Exception if !@providers.has_key?(provider)
|
24
|
+
passed_providers[provider] = @providers[provider]
|
25
|
+
end
|
26
|
+
@providers = passed_providers
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def search(artist, song)
|
31
|
+
result = nil
|
32
|
+
@providers.each_pair do |provider, klass|
|
33
|
+
fetcher = klass.new(artist_name: artist, song_name: song)
|
34
|
+
|
35
|
+
if lyric_body = fetcher.search
|
36
|
+
result = OpenStruct.new(artist: artist, song: song, body: lyric_body)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lyricfy.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lyricfy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "lyricfy"
|
8
|
+
gem.version = Lyricfy::VERSION
|
9
|
+
gem.authors = ["Javier Hidalgo"]
|
10
|
+
gem.email = ["hola@soyjavierhidalgo.com"]
|
11
|
+
gem.description = %q{Song Lyrics for your Ruby apps}
|
12
|
+
gem.summary = %q{Lyricfy lets you get song lyrics that you can use on your apps}
|
13
|
+
gem.homepage = "https://github.com/javichito/lyricfy"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency "nokogiri", [">= 1.3.3"]
|
21
|
+
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
gem.add_development_dependency "webmock", ["1.8.0"]
|
24
|
+
gem.add_development_dependency "vcr", ["~> 2.4.0"]
|
25
|
+
end
|