Refactor proxy_list into global

This commit is contained in:
Omar Roth 2019-06-28 21:17:56 -05:00
parent a58f5a925a
commit e30d70b6d4
No known key found for this signature in database
GPG key ID: B8254FB7EC3D37F2
7 changed files with 42 additions and 55 deletions

View file

@ -56,8 +56,8 @@ class RedditListing
})
end
def fetch_youtube_comments(id, db, continuation, proxies, format, locale, thin_mode, region, sort_by = "top")
video = get_video(id, db, proxies, region: region)
def fetch_youtube_comments(id, db, continuation, format, locale, thin_mode, region, sort_by = "top")
video = get_video(id, db, region: region)
session_token = video.info["session_token"]?
ctoken = produce_comment_continuation(id, cursor: "", sort_by: sort_by)
@ -75,7 +75,7 @@ def fetch_youtube_comments(id, db, continuation, proxies, format, locale, thin_m
session_token: session_token,
}
client = make_client(YT_URL, proxies, video.info["region"]?)
client = make_client(YT_URL, video.info["region"]?)
headers = HTTP::Headers.new
headers["content-type"] = "application/x-www-form-urlencoded"

View file

@ -664,7 +664,7 @@ def copy_in_chunks(input, output, chunk_size = 8192)
end
end
def create_notification_stream(env, proxies, config, kemal_config, decrypt_function, topics, connection_channel)
def create_notification_stream(env, config, kemal_config, decrypt_function, topics, connection_channel)
connection = Channel(PQ::Notification).new(8)
connection_channel.send({true, connection})
@ -682,7 +682,7 @@ def create_notification_stream(env, proxies, config, kemal_config, decrypt_funct
published = Time.utc - Time::Span.new(time_span[0], time_span[1], time_span[2], time_span[3])
video_id = TEST_IDS[rand(TEST_IDS.size)]
video = get_video(video_id, PG_DB, proxies)
video = get_video(video_id, PG_DB)
video.published = published
response = JSON.parse(video.to_json(locale, config, kemal_config, decrypt_function))
@ -758,7 +758,7 @@ def create_notification_stream(env, proxies, config, kemal_config, decrypt_funct
next
end
video = get_video(video_id, PG_DB, proxies)
video = get_video(video_id, PG_DB)
video.published = Time.unix(published)
response = JSON.parse(video.to_json(locale, config, Kemal.config, decrypt_function))

View file

@ -18,24 +18,13 @@ def elapsed_text(elapsed)
"#{(millis * 1000).round(2)}µs"
end
def make_client(url : URI, proxies = {} of String => Array({ip: String, port: Int32}), region = nil)
context = nil
if url.scheme == "https"
context = OpenSSL::SSL::Context::Client.new
context.add_options(
OpenSSL::SSL::Options::ALL |
OpenSSL::SSL::Options::NO_SSL_V2 |
OpenSSL::SSL::Options::NO_SSL_V3
)
end
client = HTTPClient.new(url, context)
client.read_timeout = 10.seconds
client.connect_timeout = 10.seconds
def make_client(url : URI, region = nil)
client = HTTPClient.new(url)
client.read_timeout = 15.seconds
client.connect_timeout = 15.seconds
if region
proxies[region]?.try &.sample(40).each do |proxy|
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)

View file

@ -256,8 +256,8 @@ def channel_search(query, page, channel)
return count, items
end
def search(query, page = 1, search_params = produce_search_params(content_type: "all"), proxies = nil, region = nil)
client = make_client(YT_URL, proxies, region)
def search(query, page = 1, search_params = produce_search_params(content_type: "all"), region = nil)
client = make_client(YT_URL, region)
if query.empty?
return {0, [] of SearchItem}
end

View file

@ -1,4 +1,4 @@
def fetch_trending(trending_type, proxies, region, locale)
def fetch_trending(trending_type, region, locale)
client = make_client(YT_URL)
headers = HTTP::Headers.new
headers["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"

View file

@ -869,7 +869,7 @@ end
class VideoRedirect < Exception
end
def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32}), refresh = true, region = nil, force_refresh = false)
def get_video(id, db, refresh = true, region = nil, force_refresh = false)
if (video = db.query_one?("SELECT * FROM videos WHERE id = $1", id, as: Video)) && !region
# If record was last updated over 10 minutes ago, or video has since premiered,
# refresh (expire param in response lasts for 6 hours)
@ -878,7 +878,7 @@ def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32})
(video.premiere_timestamp && video.premiere_timestamp.as(Time) < Time.utc)) ||
force_refresh
begin
video = fetch_video(id, proxies, region)
video = fetch_video(id, region)
video_array = video.to_a
args = arg_array(video_array[1..-1], 2)
@ -893,7 +893,7 @@ def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32})
end
end
else
video = fetch_video(id, proxies, region)
video = fetch_video(id, region)
video_array = video.to_a
args = arg_array(video_array)
@ -1097,8 +1097,8 @@ def extract_player_config(body, html)
return params
end
def fetch_video(id, proxies, region)
client = make_client(YT_URL, proxies, region)
def fetch_video(id, region)
client = make_client(YT_URL, region)
response = client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999")
if md = response.headers["location"]?.try &.match(/v=(?<id>[a-zA-Z0-9_-]{11})/)
@ -1113,9 +1113,9 @@ def fetch_video(id, proxies, region)
if info["reason"]? && info["reason"].includes? "your country"
bypass_channel = Channel({XML::Node, HTTP::Params} | Nil).new
proxies.each do |proxy_region, list|
PROXY_LIST.each do |proxy_region, list|
spawn do
client = make_client(YT_URL, proxies, proxy_region)
client = make_client(YT_URL, proxy_region)
proxy_response = client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1&has_verified=1&bpctr=9999999999")
proxy_html = XML.parse_html(proxy_response.body)
@ -1131,7 +1131,7 @@ def fetch_video(id, proxies, region)
end
end
proxies.size.times do
PROXY_LIST.size.times do
response = bypass_channel.receive
if response
html, info = response