2017-11-24 09:36:43 +05:30
|
|
|
require "http/client"
|
|
|
|
require "json"
|
2017-11-23 13:18:55 +05:30
|
|
|
require "kemal"
|
2017-11-24 09:36:43 +05:30
|
|
|
require "pg"
|
2017-11-23 13:18:55 +05:30
|
|
|
require "xml"
|
2017-11-29 07:29:51 +05:30
|
|
|
require "./url_encoded"
|
2017-11-23 13:18:55 +05:30
|
|
|
|
|
|
|
macro templated(filename)
|
|
|
|
render "views/#{{{filename}}}.ecr", "views/layout.ecr"
|
|
|
|
end
|
|
|
|
|
2017-11-24 09:36:43 +05:30
|
|
|
context = OpenSSL::SSL::Context::Client.insecure
|
2017-11-29 07:29:51 +05:30
|
|
|
fmt_file = File.open("temp/fmt_stream")
|
2017-11-24 09:36:43 +05:30
|
|
|
|
2017-11-23 13:18:55 +05:30
|
|
|
get "/" do |env|
|
|
|
|
templated "index"
|
|
|
|
end
|
|
|
|
|
2017-11-29 07:29:51 +05:30
|
|
|
|
2017-11-23 13:18:55 +05:30
|
|
|
get "/watch/:video_id" do |env|
|
|
|
|
video_id = env.params.url["video_id"]
|
|
|
|
|
2017-11-29 07:29:51 +05:30
|
|
|
client = HTTP::Client.new("www.youtube.com", 443, context)
|
|
|
|
video_info = client.get("/get_video_info?video_id=#{video_id}&el=info&ps=default&eurl=&gl=US&hl=en").body
|
|
|
|
video_info = HTTP::Params.parse(video_info)
|
|
|
|
pageContent = client.get("/watch?v=#{video_id}").body
|
|
|
|
doc = XML.parse(pageContent)
|
|
|
|
|
|
|
|
fmt_stream = [] of HTTP::Params
|
|
|
|
video_info["url_encoded_fmt_stream_map"].split(",") do |string|
|
|
|
|
fmt_stream << HTTP::Params.parse(string)
|
|
|
|
end
|
2017-11-23 13:18:55 +05:30
|
|
|
|
2017-11-29 07:29:51 +05:30
|
|
|
File.write("temp/#{video_id}", video_info)
|
|
|
|
File.write("temp/#{video_id}_manifest", video_info["dashmpd"])
|
|
|
|
File.open("temp/#{video_id}_fmt_stream_0", "a+").puts fmt_stream[0]["url"]
|
|
|
|
File.open("temp/#{video_id}_fmt_stream_1", "a+").puts fmt_stream[1]["url"]
|
|
|
|
File.open("temp/#{video_id}_fmt_stream_2", "a+").puts fmt_stream[2]["url"]
|
|
|
|
File.open("temp/#{video_id}_fmt_stream_3", "a+").puts fmt_stream[3]["url"]
|
|
|
|
fmt_stream.reverse! # We want lowest quality first
|
|
|
|
# css query [title="I like this"] > span
|
2017-11-24 09:36:43 +05:30
|
|
|
likes = doc.xpath_node(%q(//button[@title="I like this"]/span))
|
|
|
|
if likes
|
2017-11-29 07:29:51 +05:30
|
|
|
likes = likes.content.delete(",").to_i
|
2017-11-24 09:36:43 +05:30
|
|
|
else
|
2017-11-29 07:29:51 +05:30
|
|
|
likes = 1
|
2017-11-23 13:18:55 +05:30
|
|
|
end
|
2017-11-29 07:29:51 +05:30
|
|
|
|
|
|
|
# css query [title="I dislike this"] > span
|
2017-11-24 09:36:43 +05:30
|
|
|
dislikes = doc.xpath_node(%q(//button[@title="I dislike this"]/span))
|
|
|
|
if dislikes
|
2017-11-29 07:29:51 +05:30
|
|
|
dislikes = dislikes.content.delete(",").to_i
|
2017-11-24 09:36:43 +05:30
|
|
|
else
|
2017-11-29 07:29:51 +05:30
|
|
|
dislikes = 1
|
2017-11-23 13:18:55 +05:30
|
|
|
end
|
2017-11-24 09:36:43 +05:30
|
|
|
|
2017-11-29 07:29:51 +05:30
|
|
|
engagement = ((dislikes.to_f32 + likes.to_f32)*100 / video_info["view_count"].to_i).to_i
|
|
|
|
calculated_rating = likes.to_f32/(likes.to_f32 + dislikes.to_f32)*4 + 1
|
|
|
|
|
2017-11-23 13:18:55 +05:30
|
|
|
templated "watch"
|
|
|
|
end
|
|
|
|
|
|
|
|
public_folder "assets"
|
|
|
|
|
|
|
|
Kemal.run
|