]>
git.r.bdr.sh - rbdr/lyricli.rb/blob - lrc.rb
496d73be30e404afa7288e2f831aa461c47114c3
10 # This shit causes a lot of warnings. Quick Hack.
11 original_verbosity
= $VERBOSE
14 $VERBOSE = original_verbosity
22 @rdio_key = "sddac5t8akqrzh5b6kg53jfm"
23 @rdio_secret = "PRcB8TggFr"
24 @token_path = File
.expand_path("~/.rdio_token")
26 #Expand the symlink and get the path
27 if File
.symlink
?(__FILE__
) then
28 path
= File
.dirname(File
.readlink(__FILE__
))
30 path
= File
.dirname(__FILE__
)
33 # Get the current rdio track
37 #Get the current iTunes track
38 current
= `osascript #{path}/current_song.scpt`
39 if current
and not current
.empty
? then
40 current
= current
.split("<-SEP->")
41 @artist ||= current
[0]
48 if File
.exists
?(@token_path)
49 f
= File
.new(@token_path, "r")
51 token
= MultiJson
.decode(f
.read
)
53 token
= create_rdio_token
56 token
= create_rdio_token
59 Rdio
::SimpleRdio.new([@rdio_key, @rdio_secret], token
)
63 u
= @rdio.call('currentUser', {'extras' => "lastSongPlayed"})
64 @artist ||= u
["result"]["lastSongPlayed"]["artist"]
65 @song ||= u
["result"]["lastSongPlayed"]["name"]
69 rdio
= Rdio
::SimpleRdio.new([@rdio_key, @rdio_secret])
70 puts
"Go To This URL To Authorize App:"
71 auth_url
= rdio
.begin_authentication('oob')
74 print
"Please type the authorization code: "
75 auth_code
= gets
.chomp
76 token
= rdio
.complete_authentication(auth_code
)
78 f
= File
.new(@token_path, "w")
79 f
.print(MultiJson
.encode(token
))
86 abort
"Usage: #{$0} artist song"
91 #Use the API to search
92 uri
= URI("http://lyrics.wikia.com/api.php?artist=#{self.sanitize_param @artist}&song=#{self.sanitize_param @song}&fmt=realjson")
94 res
= Net
::HTTP.get(uri
)
95 res
= MultiJson
.decode(res
)
97 #Get the actual lyrics url
98 doc
= Nokogiri
::HTML(open(res
['url']))
99 node
= doc
.search(".lyricbox").first
101 abort
"Lyrics not found :("
104 #Remove the rtMatcher nodes
105 node
.search(".rtMatcher").each
do |n
|
110 node
.search("br").each
do |br
|
119 self.exit_with_error
if @artist.nil? or @artist.empty
?
120 self.exit_with_error
if @song.nil? or @song.empty
?
123 def sanitize_param(p
)
124 URI
.encode_www_form_component(p
.gsub(/ /, "+")).gsub("%2B
", "+
")