Add 'region' parameter for bypassing region locks

pull/258/head
Omar Roth 6 years ago
parent 879586d7f5
commit 16964ca6ce

@ -2215,9 +2215,10 @@ get "/api/v1/videos/:id" do |env|
env.response.content_type = "application/json" env.response.content_type = "application/json"
id = env.params.url["id"] id = env.params.url["id"]
region = env.params.query["region"]?
begin begin
video = get_video(id, PG_DB, proxies) video = get_video(id, PG_DB, proxies, region)
rescue ex : VideoRedirect rescue ex : VideoRedirect
next env.redirect "/api/v1/videos/#{ex.message}" next env.redirect "/api/v1/videos/#{ex.message}"
rescue ex rescue ex

@ -18,16 +18,28 @@ def elapsed_text(elapsed)
"#{(millis * 1000).round(2)}µs" "#{(millis * 1000).round(2)}µs"
end end
def make_client(url) def make_client(url, proxies = {} of String => Array({ip: String, port: Int32}), region = nil)
context = OpenSSL::SSL::Context::Client.new context = OpenSSL::SSL::Context::Client.new
context.add_options( context.add_options(
OpenSSL::SSL::Options::ALL | OpenSSL::SSL::Options::ALL |
OpenSSL::SSL::Options::NO_SSL_V2 | OpenSSL::SSL::Options::NO_SSL_V2 |
OpenSSL::SSL::Options::NO_SSL_V3 OpenSSL::SSL::Options::NO_SSL_V3
) )
client = HTTP::Client.new(url, context) client = HTTPClient.new(url, context)
client.read_timeout = 10.seconds client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds client.connect_timeout = 10.seconds
if region
proxies[region]?.try &.each do |proxy|
begin
proxy = HTTPProxy.new(proxy_host: proxy[:ip], proxy_port: proxy[:port])
client.set_proxy(proxy)
break
rescue ex
end
end
end
return client return client
end end

@ -507,14 +507,14 @@ end
class VideoRedirect < Exception class VideoRedirect < Exception
end end
def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32}), refresh = true) def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32}), refresh = true, region = nil)
if db.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", id, as: Bool) if db.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", id, as: Bool)
video = db.query_one("SELECT * FROM videos WHERE id = $1", id, as: Video) video = db.query_one("SELECT * FROM videos WHERE id = $1", id, as: Video)
# If record was last updated over 10 minutes ago, refresh (expire param in response lasts for 6 hours) # If record was last updated over 10 minutes ago, refresh (expire param in response lasts for 6 hours)
if refresh && Time.now - video.updated > 10.minutes if refresh && Time.now - video.updated > 10.minutes
begin begin
video = fetch_video(id, proxies) video = fetch_video(id, proxies, region)
video_array = video.to_a video_array = video.to_a
args = arg_array(video_array[1..-1], 2) args = arg_array(video_array[1..-1], 2)
@ -529,7 +529,7 @@ def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32})
end end
end end
else else
video = fetch_video(id, proxies) video = fetch_video(id, proxies, region)
video_array = video.to_a video_array = video.to_a
args = arg_array(video_array) args = arg_array(video_array)
@ -540,12 +540,12 @@ def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32})
return video return video
end end
def fetch_video(id, proxies) def fetch_video(id, proxies, region)
html_channel = Channel(XML::Node | String).new html_channel = Channel(XML::Node | String).new
info_channel = Channel(HTTP::Params).new info_channel = Channel(HTTP::Params).new
spawn do spawn do
client = make_client(YT_URL) client = make_client(YT_URL, proxies, region)
html = client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999") html = client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999")
if md = html.headers["location"]?.try &.match(/v=(?<id>[a-zA-Z0-9_-]{11})/) if md = html.headers["location"]?.try &.match(/v=(?<id>[a-zA-Z0-9_-]{11})/)
@ -557,7 +557,7 @@ def fetch_video(id, proxies)
end end
spawn do spawn do
client = make_client(YT_URL) client = make_client(YT_URL, proxies, region)
info = client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en&disable_polymer=1") info = client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en&disable_polymer=1")
info = HTTP::Params.parse(info.body) info = HTTP::Params.parse(info.body)
@ -628,9 +628,9 @@ def fetch_video(id, proxies)
end end
proxy = {ip: proxy.proxy_host, port: proxy.proxy_port} proxy = {ip: proxy.proxy_host, port: proxy.proxy_port}
region = proxies.select { |region, list| list.includes? proxy } region_proxies = proxies.select { |region, list| list.includes? proxy }
if !region.empty? if !region_proxies.empty?
info["region"] = region.keys[0] info["region"] = region_proxies.keys[0]
end end
break break
@ -736,11 +736,13 @@ def process_video_params(query, preferences)
listen = query["listen"]? && (query["listen"] == "true" || query["listen"] == "1").to_unsafe listen = query["listen"]? && (query["listen"] == "true" || query["listen"] == "1").to_unsafe
preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase } preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase }
quality = query["quality"]? quality = query["quality"]?
region = query["region"]?
speed = query["speed"]?.try &.to_f? speed = query["speed"]?.try &.to_f?
video_loop = query["loop"]?.try &.to_i? video_loop = query["loop"]?.try &.to_i?
volume = query["volume"]?.try &.to_i? volume = query["volume"]?.try &.to_i?
if preferences if preferences
# region ||= preferences.region
autoplay ||= preferences.autoplay.to_unsafe autoplay ||= preferences.autoplay.to_unsafe
continue ||= preferences.continue.to_unsafe continue ||= preferences.continue.to_unsafe
listen ||= preferences.listen.to_unsafe listen ||= preferences.listen.to_unsafe
@ -798,6 +800,7 @@ def process_video_params(query, preferences)
preferred_captions: preferred_captions, preferred_captions: preferred_captions,
quality: quality, quality: quality,
raw: raw, raw: raw,
region: region,
speed: speed, speed: speed,
video_end: video_end, video_end: video_end,
video_loop: video_loop, video_loop: video_loop,

Loading…
Cancel
Save