Last active
August 29, 2015 13:57
-
-
Save aycabta/9673875 to your computer and use it in GitHub Desktop.
Embedded Niconico Player API "thumb_watch" to JSONP
This file contains 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
require 'sinatra' | |
get '/thumb_watch/:id' do | |
html = <<EOH | |
<html> | |
<head></head> | |
<body style='margin:0;'> | |
<script type='text/javascript' src='http://ext.nicovideo.jp/thumb_watch/#{params[:id]}?w=490&h=307&n=1'></script> | |
</body> | |
</html> | |
EOH | |
[200, {'X-Frame-Options' => 'ALLOW-FROM http://lingr.com'}, html] | |
end | |
This file contains 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
source :rubygems | |
gem 'sinatra' | |
gem 'thin' | |
This file contains 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
GEM | |
remote: http://rubygems.org/ | |
specs: | |
daemons (1.1.9) | |
eventmachine (1.0.3) | |
rack (1.5.2) | |
rack-protection (1.5.2) | |
rack | |
sinatra (1.4.4) | |
rack (~> 1.4) | |
rack-protection (~> 1.4) | |
tilt (~> 1.3, >= 1.3.4) | |
thin (1.6.2) | |
daemons (>= 1.0.9) | |
eventmachine (>= 1.0.0) | |
rack (>= 1.0.0) | |
tilt (1.4.1) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
sinatra | |
thin |
This file contains 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
web: bundle exec ruby shit.rb -p $PORT | |
This file contains 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
require 'sinatra' | |
require 'net/http' | |
JS_ESCAPE_MAP = { | |
'\\' => '\\\\', | |
'</' => '<\/', | |
"\r\n" => '\n', | |
"\n" => '\n', | |
"\r" => '\n', | |
'"' => '\\"', | |
"'" => "\\'", | |
"\342\200\250" => '
' | |
} | |
def escape_js(javascript) | |
javascript.gsub(/(\\|<\/|\r\n|\342\200\250|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] } | |
end | |
get '/thumb_watch_jsonp/:id' do | |
Net::HTTP.start("ext.nicovideo.jp") do |http| | |
resp = http.get("/thumb_watch/#{params[:id]}?w=490&h=307") | |
if resp.body.match(/new Nicovideo\.MiniPlayer\(video, (\{\n.+\s+\}),/m) | |
params_text = $1.gsub(/\n/, '').gsub(': ', '=>') | |
arr = eval(eval("\"#{params_text}\"")) | |
jsonp = "{\n" + arr.map{ |k, v| "\t'#{escape_js(k)}': '#{escape_js(v)}'" }.join(",\n") + "\n}" | |
return jsonp | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment