Remove legacy proxy code

This commit is contained in:
syeopite 2024-04-07 11:08:12 -07:00
parent 08390acd0c
commit 990931ff67
No known key found for this signature in database
GPG key ID: A73C186DA3955A1A
5 changed files with 20 additions and 373 deletions

View file

@ -394,17 +394,6 @@ def fetch_video(id, region)
.dig?("microformat", "playerMicroformatRenderer", "availableCountries") .dig?("microformat", "playerMicroformatRenderer", "availableCountries")
.try &.as_a.map &.as_s || [] of String .try &.as_a.map &.as_s || [] of String
# Check for region-blocks
if info["reason"]?.try &.as_s.includes?("your country")
bypass_regions = PROXY_LIST.keys & allowed_regions
if !bypass_regions.empty?
region = bypass_regions[rand(bypass_regions.size)]
region_info = extract_video_info(video_id: id, proxy_region: region)
region_info["region"] = JSON::Any.new(region) if region
info = region_info if !region_info["reason"]?
end
end
if reason = info["reason"]? if reason = info["reason"]?
if reason == "Video unavailable" if reason == "Video unavailable"
raise NotFoundException.new(reason.as_s || "") raise NotFoundException.new(reason.as_s || "")

View file

@ -50,9 +50,9 @@ def parse_related_video(related : JSON::Any) : Hash(String, JSON::Any)?
} }
end end
def extract_video_info(video_id : String, proxy_region : String? = nil) def extract_video_info(video_id : String)
# Init client config for the API # Init client config for the API
client_config = YoutubeAPI::ClientConfig.new(proxy_region: proxy_region) client_config = YoutubeAPI::ClientConfig.new
# Fetch data from the player endpoint # Fetch data from the player endpoint
player_response = YoutubeAPI.player(video_id: video_id, params: "", client_config: client_config) player_response = YoutubeAPI.player(video_id: video_id, params: "", client_config: client_config)

View file

@ -24,25 +24,20 @@ struct YoutubeConnectionPool
@pool = build_pool() @pool = build_pool()
end end
def client(region = nil, &block) def client(&block)
if region conn = pool.checkout
conn = make_client(url, region, force_resolve = true) begin
response = yield conn response = yield conn
else rescue ex
conn = pool.checkout conn.close
begin conn = HTTP::Client.new(url)
response = yield conn
rescue ex
conn.close
conn = HTTP::Client.new(url)
conn.family = CONFIG.force_resolve conn.family = CONFIG.force_resolve
conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC conn.family = Socket::Family::INET if conn.family == Socket::Family::UNSPEC
conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com" conn.before_request { |r| add_yt_headers(r) } if url.host == "www.youtube.com"
response = yield conn response = yield conn
ensure ensure
pool.release(conn) pool.release(conn)
end
end end
response response
@ -60,9 +55,9 @@ struct YoutubeConnectionPool
end end
def make_client(url : URI, region = nil, force_resolve : Bool = false) def make_client(url : URI, region = nil, force_resolve : Bool = false)
client = HTTPClient.new(url, OpenSSL::SSL::Context::Client.insecure) client = HTTP::Client.new(url)
# Some services do not support IPv6. # Force the usage of a specific configured IP Family
if force_resolve if force_resolve
client.family = CONFIG.force_resolve client.family = CONFIG.force_resolve
end end
@ -71,17 +66,6 @@ def make_client(url : URI, region = nil, force_resolve : Bool = false)
client.read_timeout = 10.seconds client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds client.connect_timeout = 10.seconds
if region
PROXY_LIST[region]?.try &.sample(40).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

File diff suppressed because one or more lines are too long

View file

@ -188,10 +188,6 @@ module YoutubeAPI
# conf_2 = ClientConfig.new(client_type: ClientType::Android) # conf_2 = ClientConfig.new(client_type: ClientType::Android)
# YoutubeAPI::player(video_id: "dQw4w9WgXcQ", client_config: conf_2) # YoutubeAPI::player(video_id: "dQw4w9WgXcQ", client_config: conf_2)
# #
# # Proxy request through russian proxies
# conf_3 = ClientConfig.new(proxy_region: "RU")
# YoutubeAPI::next({video_id: "dQw4w9WgXcQ"}, client_config: conf_3)
# ```
# #
struct ClientConfig struct ClientConfig
# Type of client to emulate. # Type of client to emulate.
@ -202,16 +198,11 @@ module YoutubeAPI
# (this is passed as the `gl` parameter). # (this is passed as the `gl` parameter).
property region : String | Nil property region : String | Nil
# ISO code of country where the proxy is located.
# Used in case of geo-restricted videos.
property proxy_region : String | Nil
# Initialization function # Initialization function
def initialize( def initialize(
*, *,
@client_type = ClientType::Web, @client_type = ClientType::Web,
@region = "US", @region = "US"
@proxy_region = nil
) )
end end
@ -271,9 +262,8 @@ module YoutubeAPI
# Convert to string, for logging purposes # Convert to string, for logging purposes
def to_s def to_s
return { return {
client_type: self.name, client_type: self.name,
region: @region, region: @region,
proxy_region: @proxy_region,
}.to_s }.to_s
end end
end end
@ -620,7 +610,7 @@ module YoutubeAPI
LOGGER.trace("YoutubeAPI: POST data: #{data}") LOGGER.trace("YoutubeAPI: POST data: #{data}")
# Send the POST request # Send the POST request
body = YT_POOL.client(client_config.proxy_region) do |client| body = YT_POOL.client() do |client|
client.post(url, headers: headers, body: data.to_json) do |response| client.post(url, headers: headers, body: data.to_json) do |response|
self._decompress(response.body_io, response.headers["Content-Encoding"]?) self._decompress(response.body_io, response.headers["Content-Encoding"]?)
end end