summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOmar Roth <omarroth@hotmail.com>2018-01-21 17:49:27 -0600
committerOmar Roth <omarroth@hotmail.com>2018-01-21 17:49:27 -0600
commitcf49306ffb0a18984dc99066cc0bc7381fd2749c (patch)
treeba92ef5af4e92e48d51ec5f414a38c4fa62606ea /src
parent1a4fbce5e5f3371785459270d1fd0a94761c31b8 (diff)
downloadinvidious-cf49306ffb0a18984dc99066cc0bc7381fd2749c.tar.gz
invidious-cf49306ffb0a18984dc99066cc0bc7381fd2749c.tar.bz2
invidious-cf49306ffb0a18984dc99066cc0bc7381fd2749c.zip
Fix index out of bounds error
Diffstat (limited to 'src')
-rw-r--r--src/helpers.cr19
-rw-r--r--src/invidious.cr38
2 files changed, 35 insertions, 22 deletions
diff --git a/src/helpers.cr b/src/helpers.cr
index d2186d6c..1a6037a5 100644
--- a/src/helpers.cr
+++ b/src/helpers.cr
@@ -64,3 +64,22 @@ def get_video(id, refresh = true)
return video
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
diff --git a/src/invidious.cr b/src/invidious.cr
index 1c449722..9c143dc5 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -24,27 +24,21 @@ end
# Refresh connections by crawling YT
spawn do
# Start video
- id = Deque.new(10, "_wbqqI0IgY8")
-
- client = get_client
+ ids = Deque.new(10, "_wbqqI0IgY8")
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|
- 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
- id << link.content.split("=")[1]
- id.shift
- end
- end
+ search(random.base64(3)) do |id|
+ ids << id
end
- POOL << client
-
loop do
+ if ids.size < 5
+ search(random.base64) do |id|
+ ids << id
+ puts "refreshed ids"
+ end
+ end
+
if rand(600) < 1
client = get_client
client = HTTP::Client.new(URL, CONTEXT)
@@ -55,9 +49,9 @@ spawn do
time = Time.now
begin
- i = id[rand(id.size)]
- video = get_video(i, false)
- id.delete(i)
+ id = ids[rand(ids.size)]
+ video = get_video(id, false)
+ ids.delete(id)
rescue ex
puts ex
next
@@ -73,9 +67,9 @@ spawn do
rvs.each do |rv|
if rv.has_key?("id")
if !PG_DB.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", rv["id"], as: Bool)
- id << rv["id"]
- if id.size == 50
- id.shift
+ ids << rv["id"]
+ if ids.size == 50
+ ids.shift
end
end
end