Minor refactor

This commit is contained in:
Omar Roth 2018-09-04 08:52:30 -05:00
parent 89d3587861
commit 25c3ee034e
4 changed files with 84 additions and 84 deletions

View file

@ -1385,7 +1385,7 @@ get "/feed/channel/:ucid" do |env|
next env.redirect "/feed/channel/#{ucid}" next env.redirect "/feed/channel/#{ucid}"
end end
url = produce_videos_url(ucid) url = produce_channel_videos_url(ucid)
response = client.get(url) response = client.get(url)
json = JSON.parse(response.body) json = JSON.parse(response.body)
@ -1844,7 +1844,7 @@ get "/api/v1/comments/:id" do |env|
text text
end.join.rchop('\ufeff') end.join.rchop('\ufeff')
content, contentHtml = html_to_description(contentHtml) contentHtml, content = html_to_content(contentHtml)
author = node_comment["authorText"]?.try &.["simpleText"] author = node_comment["authorText"]?.try &.["simpleText"]
author ||= "" author ||= ""
@ -1979,7 +1979,7 @@ get "/api/v1/videos/:id" do |env|
generate_thumbnails(json, video.id) generate_thumbnails(json, video.id)
end end
description, video.description = html_to_description(video.description) video.description, description = html_to_content(video.description)
json.field "description", description json.field "description", description
json.field "descriptionHtml", video.description json.field "descriptionHtml", video.description
@ -2222,7 +2222,7 @@ get "/api/v1/channels/:ucid" do |env|
next env.redirect url next env.redirect url
end end
url = produce_videos_url(ucid, 1) url = produce_channel_videos_url(ucid, 1)
response = client.get(url) response = client.get(url)
json = JSON.parse(response.body) json = JSON.parse(response.body)
@ -2362,7 +2362,7 @@ get "/api/v1/channels/:ucid/videos" do |env|
next env.redirect url next env.redirect url
end end
url = produce_videos_url(ucid, page) url = produce_channel_videos_url(ucid, page)
response = client.get(url) response = client.get(url)
json = JSON.parse(response.body) json = JSON.parse(response.body)

View file

@ -73,7 +73,7 @@ def fetch_channel(ucid, client, db, pull_all_videos = true)
page = 1 page = 1
loop do loop do
url = produce_videos_url(ucid, page) url = produce_channel_videos_url(ucid, page)
response = client.get(url) response = client.get(url)
json = JSON.parse(response.body) json = JSON.parse(response.body)
@ -130,3 +130,32 @@ def fetch_channel(ucid, client, db, pull_all_videos = true)
return channel return channel
end end
def produce_channel_videos_url(ucid, page = 1)
page = "#{page}"
meta = "\x12\x06videos \x00\x30\x02\x38\x01\x60\x01\x6a\x00\x7a"
meta += page.size.to_u8.unsafe_chr
meta += page
meta += "\xb8\x01\x00"
meta = Base64.urlsafe_encode(meta)
meta = URI.escape(meta)
continuation = "\x12"
continuation += ucid.size.to_u8.unsafe_chr
continuation += ucid
continuation += "\x1a"
continuation += meta.size.to_u8.unsafe_chr
continuation += meta
continuation = continuation.size.to_u8.unsafe_chr + continuation
continuation = "\xe2\xa9\x85\xb2\x02" + continuation
continuation = Base64.urlsafe_encode(continuation)
continuation = URI.escape(continuation)
url = "/browse_ajax?continuation=#{continuation}"
return url
end

View file

@ -116,81 +116,6 @@ def login_req(login_form, f_req)
return HTTP::Params.encode(data) return HTTP::Params.encode(data)
end end
def produce_videos_url(ucid, page = 1)
page = "#{page}"
meta = "\x12\x06videos \x00\x30\x02\x38\x01\x60\x01\x6a\x00\x7a"
meta += page.size.to_u8.unsafe_chr
meta += page
meta += "\xb8\x01\x00"
meta = Base64.urlsafe_encode(meta)
meta = URI.escape(meta)
continuation = "\x12"
continuation += ucid.size.to_u8.unsafe_chr
continuation += ucid
continuation += "\x1a"
continuation += meta.size.to_u8.unsafe_chr
continuation += meta
continuation = continuation.size.to_u8.unsafe_chr + continuation
continuation = "\xe2\xa9\x85\xb2\x02" + continuation
continuation = Base64.urlsafe_encode(continuation)
continuation = URI.escape(continuation)
url = "/browse_ajax?continuation=#{continuation}"
return url
end
def read_var_int(bytes)
numRead = 0
result = 0
read = bytes[numRead]
if bytes.size == 1
result = bytes[0].to_i32
else
while ((read & 0b10000000) != 0)
read = bytes[numRead].to_u64
value = (read & 0b01111111)
result |= (value << (7 * numRead))
numRead += 1
if numRead > 5
raise "VarInt is too big"
end
end
end
return result
end
def write_var_int(value : Int)
bytes = [] of UInt8
value = value.to_u32
if value == 0
bytes = [0_u8]
else
while value != 0
temp = (value & 0b01111111).to_u8
value = value >> 7
if value != 0
temp |= 0b10000000
end
bytes << temp
end
end
return bytes
end
def generate_captcha(key) def generate_captcha(key)
minute = Random::Secure.rand(12) minute = Random::Secure.rand(12)
minute_angle = minute * 30 minute_angle = minute * 30
@ -240,7 +165,7 @@ def generate_captcha(key)
return {challenge: challenge, token: token} return {challenge: challenge, token: token}
end end
def html_to_description(description_html) def html_to_content(description_html)
if !description_html if !description_html
description = "" description = ""
description_html = "" description_html = ""
@ -251,7 +176,7 @@ def html_to_description(description_html)
description = XML.parse_html(description).content.strip("\n ") description = XML.parse_html(description).content.strip("\n ")
end end
return description, description_html return description_html, description
end end
def extract_videos(nodeset, ucid = nil) def extract_videos(nodeset, ucid = nil)
@ -319,7 +244,7 @@ def extract_videos(nodeset, ucid = nil)
view_count ||= 0_i64 view_count ||= 0_i64
description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")])) description_html = node.xpath_node(%q(.//div[contains(@class, "yt-lockup-description")]))
description, description_html = html_to_description(description_html) description_html, description = html_to_content(description_html)
length_seconds = node.xpath_node(%q(.//span[@class="video-time"])) length_seconds = node.xpath_node(%q(.//span[@class="video-time"]))
if length_seconds if length_seconds

View file

@ -191,3 +191,49 @@ def get_referer(env, fallback = "/")
return referer return referer
end end
def read_var_int(bytes)
numRead = 0
result = 0
read = bytes[numRead]
if bytes.size == 1
result = bytes[0].to_i32
else
while ((read & 0b10000000) != 0)
read = bytes[numRead].to_u64
value = (read & 0b01111111)
result |= (value << (7 * numRead))
numRead += 1
if numRead > 5
raise "VarInt is too big"
end
end
end
return result
end
def write_var_int(value : Int)
bytes = [] of UInt8
value = value.to_u32
if value == 0
bytes = [0_u8]
else
while value != 0
temp = (value & 0b01111111).to_u8
value = value >> 7
if value != 0
temp |= 0b10000000
end
bytes << temp
end
end
return bytes
end