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,11 +24,7 @@ struct YoutubeConnectionPool
@pool = build_pool() @pool = build_pool()
end end
def client(region = nil, &block) def client(&block)
if region
conn = make_client(url, region, force_resolve = true)
response = yield conn
else
conn = pool.checkout conn = pool.checkout
begin begin
response = yield conn response = yield conn
@ -43,7 +39,6 @@ struct YoutubeConnectionPool
ensure ensure
pool.release(conn) pool.release(conn)
end end
end
response response
end end
@ -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
@ -273,7 +264,6 @@ module YoutubeAPI
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