Skip to content

Instantly share code, notes, and snippets.

@aycabta
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aycabta/9673875 to your computer and use it in GitHub Desktop.
Save aycabta/9673875 to your computer and use it in GitHub Desktop.
Embedded Niconico Player API "thumb_watch" to JSONP
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
source :rubygems
gem 'sinatra'
gem 'thin'
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
web: bundle exec ruby shit.rb -p $PORT
require 'sinatra'
require 'net/http'
JS_ESCAPE_MAP = {
'\\' => '\\\\',
'</' => '<\/',
"\r\n" => '\n',
"\n" => '\n',
"\r" => '\n',
'"' => '\\"',
"'" => "\\'",
"\342\200\250" => '&#x2028;'
}
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