lyrix 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/lyrix +4 -0
- data/lib/lyrix.rb +107 -0
- metadata +47 -0
data/bin/lyrix
ADDED
data/lib/lyrix.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.require :default
|
4
|
+
|
5
|
+
# ruby standard library modules
|
6
|
+
require 'open-uri'
|
7
|
+
require 'net/http'
|
8
|
+
|
9
|
+
class ITunesLibrary
|
10
|
+
def initialize(itunes_library_path)
|
11
|
+
@library = ITunes::Library.load(itunes_library_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
def each
|
15
|
+
@library.music.tracks.each do |track|
|
16
|
+
yield(track.location_path)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Song
|
22
|
+
attr_reader :title, :artist, :lyrics, :path
|
23
|
+
|
24
|
+
def initialize(path)
|
25
|
+
@path = path
|
26
|
+
|
27
|
+
# TODO: need to refactor this with a block
|
28
|
+
# TODO: research namespacing issue for blocks
|
29
|
+
f = TagLib::MPEG::File.new(path)
|
30
|
+
tag = f.id3v2_tag
|
31
|
+
# read basic attributes
|
32
|
+
@title = tag.title
|
33
|
+
@artist = tag.artist
|
34
|
+
|
35
|
+
# Add lyrics frame
|
36
|
+
uslt = TagLib::ID3v2::UnsynchronizedLyricsFrame.new
|
37
|
+
uslt.text = fetch_lyrics
|
38
|
+
tag.add_frame(uslt)
|
39
|
+
|
40
|
+
f.save
|
41
|
+
f.close
|
42
|
+
# end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
# fetches song lyrics, sets @lyrics
|
47
|
+
def fetch_lyrics
|
48
|
+
@lyrics = WebHelper::fetch_lucky_content_for "#{@title} by #{@artist} lyrics"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
module WebHelper
|
54
|
+
|
55
|
+
# does a google lucky search for msg and returns a readablity parsed output
|
56
|
+
def self.fetch_lucky_content_for(msg)
|
57
|
+
url = self.get_google_lucky_url(msg)
|
58
|
+
source = self.get_source_from_url(url)
|
59
|
+
content = self.get_content_from(source)
|
60
|
+
end
|
61
|
+
|
62
|
+
# does a google lucky search for msg, returns a URL
|
63
|
+
def self.get_google_lucky_url(msg)
|
64
|
+
uri=URI.parse "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s"\
|
65
|
+
% URI.escape(msg)
|
66
|
+
req = Net::HTTP::Get.new(uri.request_uri)
|
67
|
+
http = Net::HTTP.new(uri.host)
|
68
|
+
http.read_timeout = 5
|
69
|
+
http.open_timeout = 5
|
70
|
+
res = http.start { |server|
|
71
|
+
server.request(req)
|
72
|
+
}
|
73
|
+
JSON.parse(res.body)['responseData']['results'][0]['url']
|
74
|
+
end
|
75
|
+
|
76
|
+
# visits the url and returns the page's source
|
77
|
+
def self.get_source_from_url(url)
|
78
|
+
open(url).read
|
79
|
+
end
|
80
|
+
|
81
|
+
# parses source with readibility and returns the content
|
82
|
+
def self.get_content_from(source)
|
83
|
+
content = Readability::Document.new(source, tags: %w[br div p]).content
|
84
|
+
|
85
|
+
# TODO: refactor blacklist html tag substitution
|
86
|
+
blacklisted_tags = [
|
87
|
+
'<div>', '<p>', '</div>', '</p>'
|
88
|
+
]
|
89
|
+
blacklisted_tags.each do |tag|
|
90
|
+
content.gsub! tag, ''
|
91
|
+
end
|
92
|
+
content.gsub! '<br>', "\n"
|
93
|
+
|
94
|
+
return content
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class Lyrix < Thor
|
99
|
+
desc "load 'ITUNES_LIBRARY_PATH'", 'loads iTunes Library and fetches all lyrics. Use single quotes arond library path, point to the "iTunes Music Library.xml" file'
|
100
|
+
def load(itunes_library_path)
|
101
|
+
@library = ITunesLibrary.new(itunes_library_path)
|
102
|
+
@library.each do |song_path|
|
103
|
+
Song.new song_path
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lyrix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Martino Visintin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Fetches lyrics for all songs in your iTunes library
|
15
|
+
email: martino.visintin@gmail.com
|
16
|
+
executables:
|
17
|
+
- lyrix
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/lyrix.rb
|
22
|
+
- bin/lyrix
|
23
|
+
homepage: https://github.com/vise890/lyrix
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.24
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Lyrics fetcher for iTunes
|
47
|
+
test_files: []
|