Created
July 1, 2011 13:15
-
-
Save skahack/1058527 to your computer and use it in GitHub Desktop.
goo歌詞、コピーできなくなってる!!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
require 'rubygems' | |
require 'uri' | |
require 'open-uri' | |
require 'json' | |
require 'nokogiri' | |
require 'appscript' | |
HTTP = 'http://' | |
GOO = 'music.goo.ne.jp' | |
ID_MATCH = 'LYRUTND[0-9]*' | |
def target_uri(doc) | |
content = doc.css('script')[4].content | |
target = /sp\/lyric[^']*/.match(content) | |
"#{HTTP}#{GOO}/#{target.to_s}" | |
end | |
def draw(ary) | |
s = "" | |
ary.each {|n| s+=n} | |
s | |
end | |
def get_lyric(doc) | |
eval(doc.css('p')[0].content) | |
end | |
def lyric_uri(id) | |
"#{HTTP}#{GOO}/lyric/#{id}/index.html" | |
end | |
def lyric(id) | |
a = Nokogiri::HTML(open(lyric_uri(id))) | |
b = Nokogiri::HTML(open(target_uri(a))) | |
get_lyric(b) | |
end | |
def googling(q, s=0) | |
query = "site:#{GOO}/lyric" | |
q.split(' ').each {|n| query += " intitle:#{n}"} | |
uri = URI.escape("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&&start=#{s}&rsz=5&q=#{query}") | |
json = JSON.parse(open(uri).read) | |
results = [] | |
json["responseData"]["results"].each do |n| | |
title = /(.*) 歌詞情報 - goo 音楽/.match(n["titleNoFormatting"]) | |
id = /(#{ID_MATCH})\/index/.match(n["url"]) | |
results.push({"title"=>title[1], "id"=>id[1]}) if title && id | |
end | |
results | |
end | |
def print_result(result) | |
if result.size == 1 | |
puts lyric(result[0]["id"]) | |
else | |
result.each {|n| puts "#{n["title"]} ID: #{n["id"]}"} | |
end | |
end | |
if ARGV.size > 0 | |
if /#{ID_MATCH}/.match(ARGV[0]) | |
ARGV.each do |n| | |
s = /(#{ID_MATCH})/.match(n) | |
puts lyric(s[1]) if s | |
end | |
elsif /^-now$/.match(ARGV[0]) | |
track_name = Appscript.app('iTunes').current_track.name.get() | |
results = googling(track_name) | |
print_result(results) | |
else | |
argv = ARGV.join(' ').gsub(/ /, ' ') | |
results = googling(argv) | |
print_result(results) | |
end | |
else | |
puts "usage: > ruby goo.rb [<options>]" | |
puts "" | |
puts "OPTIONS : search query (use google) ; " | |
puts " -now ; current playing track name of iTunes" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment