invidious/src/visor.cr

96 lines
2.2 KiB
Crystal
Raw Normal View History

require "http/client"
require "json"
2017-11-23 02:48:55 -05:00
require "kemal"
require "pg"
2017-11-23 02:48:55 -05:00
require "xml"
macro templated(filename)
render "views/#{{{filename}}}.ecr", "views/layout.ecr"
end
# pg = DB.open("postgres://kemal@visor/dev")
alias Type = String | Hash(String, Type)
def object_to_hash(value)
object = {} of String => Type
items = value.split("&")
items.each do |item|
key, value = item.split("=")
value = URI.unescape(value)
object[key] = parse_uri(value)
end
return object
end
2017-11-23 02:48:55 -05:00
def array_to_hash(value)
array = {} of String => Type
items = value.split(",")
count = 0
items.each do |item|
array[count.to_s] = parse_uri(item)
count += 1
2017-11-23 02:48:55 -05:00
end
return array
2017-11-23 02:48:55 -05:00
end
def parse_uri(value)
if value.starts_with?("http") || value.starts_with?("[")
return value
else
if value.includes?(",")
return array_to_hash(value)
elsif value.includes?("&")
return object_to_hash(value)
else
return value
end
end
end
context = OpenSSL::SSL::Context::Client.insecure
client = HTTP::Client.new("www.youtube.com", 443, context)
2017-11-23 02:48:55 -05:00
get "/" do |env|
templated "index"
end
get "/watch/:video_id" do |env|
video_id = env.params.url["video_id"]
video_info_encoded = HTTP::Client.get("https://www.youtube.com/get_video_info?video_id=#{video_id}&el=info&ps=default&eurl=&gl=US&hl=en", nil, nil, tls = context).body
video_info = object_to_hash(video_info_encoded)
body = client.get("/watch?v=#{video_id}").body
doc = XML.parse(body)
2017-11-23 02:48:55 -05:00
likes = doc.xpath_node(%q(//button[@title="I like this"]/span))
if likes
likes = likes.content
else
likes = "n/a"
2017-11-23 02:48:55 -05:00
end
dislikes = doc.xpath_node(%q(//button[@title="I dislike this"]/span))
if dislikes
dislikes.content
else
dislikes = "n/a"
2017-11-23 02:48:55 -05:00
end
File.write("video_info/#{video_id}", video_info.to_json)
2017-11-23 02:48:55 -05:00
templated "watch"
end
# get "/listen/:video_id" do |env|
# video_id = env.params.url["video_id"]
2017-11-23 02:48:55 -05:00
# video_info_encoded = HTTP::Client.get("https://www.youtube.com/get_video_info?video_id=#{video_id}&el=info&ps=default&eurl=&gl=US&hl=en", nil, nil, tls = context).body
# video_info = object_to_hash(video_info_encoded)
# File.write("video_info/#{video_id}", video_info.to_json)
# templated "listen"
# end
2017-11-23 02:48:55 -05:00
public_folder "assets"
Kemal.run