Fix index out of bounds error

This commit is contained in:
Omar Roth 2018-01-21 17:49:27 -06:00
parent 1a4fbce5e5
commit cf49306ffb
2 changed files with 35 additions and 22 deletions

View file

@ -64,3 +64,22 @@ def get_video(id, refresh = true)
return video return video
end end
def search(query)
client = get_client
html = client.get("https://www.youtube.com/results?q=#{query}&sp=EgIQAVAU").body
html = XML.parse_html(html)
html.xpath_nodes(%q(//ol[@class="item-section"]/li)).each do |item|
root = item.xpath_node(%q(div[contains(@class,"yt-lockup-video")]/div))
if root
link = root.xpath_node(%q(div[contains(@class,"yt-lockup-thumbnail")]/a/@href))
if link
yield link.content.split("=")[1]
end
end
end
POOL << client
end

View file

@ -24,27 +24,21 @@ end
# Refresh connections by crawling YT # Refresh connections by crawling YT
spawn do spawn do
# Start video # Start video
id = Deque.new(10, "_wbqqI0IgY8") ids = Deque.new(10, "_wbqqI0IgY8")
client = get_client
random = Random.new random = Random.new
html = client.get("https://www.youtube.com/results?q=#{random.base64(3)}&sp=EgIQAVAU").body
html = XML.parse_html(html)
html.xpath_nodes(%q(//ol[@class="item-section"]/li)).each do |item| search(random.base64(3)) do |id|
root = item.xpath_node(%q(div[contains(@class,"yt-lockup-video")]/div)) ids << id
if root
link = root.xpath_node(%q(div[contains(@class,"yt-lockup-thumbnail")]/a/@href))
if link
id << link.content.split("=")[1]
id.shift
end
end
end end
POOL << client
loop do loop do
if ids.size < 5
search(random.base64) do |id|
ids << id
puts "refreshed ids"
end
end
if rand(600) < 1 if rand(600) < 1
client = get_client client = get_client
client = HTTP::Client.new(URL, CONTEXT) client = HTTP::Client.new(URL, CONTEXT)
@ -55,9 +49,9 @@ spawn do
time = Time.now time = Time.now
begin begin
i = id[rand(id.size)] id = ids[rand(ids.size)]
video = get_video(i, false) video = get_video(id, false)
id.delete(i) ids.delete(id)
rescue ex rescue ex
puts ex puts ex
next next
@ -73,9 +67,9 @@ spawn do
rvs.each do |rv| rvs.each do |rv|
if rv.has_key?("id") if rv.has_key?("id")
if !PG_DB.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", rv["id"], as: Bool) if !PG_DB.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", rv["id"], as: Bool)
id << rv["id"] ids << rv["id"]
if id.size == 50 if ids.size == 50
id.shift ids.shift
end end
end end
end end