diff options
| author | Omar Roth <omarroth@hotmail.com> | 2017-11-23 01:48:55 -0600 |
|---|---|---|
| committer | Omar Roth <omarroth@hotmail.com> | 2017-11-23 01:48:55 -0600 |
| commit | 26959020b732b5d729432294df6246307819e32f (patch) | |
| tree | 0d9ff22c2c1ef8f3ead9950be54dea1d809c21f5 /src | |
| download | invidious-26959020b732b5d729432294df6246307819e32f.tar.gz invidious-26959020b732b5d729432294df6246307819e32f.tar.bz2 invidious-26959020b732b5d729432294df6246307819e32f.zip | |
Initial commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/proxy.cr | 0 | ||||
| -rw-r--r-- | src/visor.cr | 69 |
2 files changed, 69 insertions, 0 deletions
diff --git a/src/proxy.cr b/src/proxy.cr new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/proxy.cr diff --git a/src/visor.cr b/src/visor.cr new file mode 100644 index 00000000..76988b29 --- /dev/null +++ b/src/visor.cr @@ -0,0 +1,69 @@ +require "kemal" +require "xml" +require "http/client" +require "base64" + +macro templated(filename) + render "views/#{{{filename}}}.ecr", "views/layout.ecr" +end + +context = OpenSSL::SSL::Context::Client.insecure +client = HTTP::Client.new("www.youtube.com", 443, context) + +def params_to_hash(params) + pairs = params.split("&") + hash = Hash(String, String).new + pairs.each do |pair| + key, value = pair.split("=") + hash[key] = URI.unescape(value) + end + return hash +end + +get "/" do |env| + templated "index" +end + +get "/watch/:video_id" do |env| + video_id = env.params.url["video_id"] + + if File.exists?("video_info/#{video_id}") + video_info = JSON.parse(File.open("video_info/#{video_id}")) + else + 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 = params_to_hash(video_info_encoded) + + File.write("video_info/#{video_id}", video_info.to_json) + end + + fmt_stream_map = video_info["url_encoded_fmt_stream_map"].to_s.split(",") + fmt_stream = Array(Hash(String, String)).new + fmt_stream_map.each do |fmt| + fmt_stream << params_to_hash(fmt.to_s) + end + fmt_stream.reverse! + templated "watch" +end + +get "/listen/:video_id" do |env| + video_id = env.params.url["video_id"] + + if File.exists?("video_info/#{video_id}") + video_info = JSON.parse(File.open("video_info/#{video_id}")) + else + 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 = params_to_hash(video_info_encoded) + + File.write("video_info/#{video_id}", video_info.to_json) + end + + adaptive_fmt = Array(Hash(String, String)).new + video_info["adaptive_fmts"].to_s.split(",") do |fmt| + adaptive_fmt << params_to_hash(video_info["adaptive_fmts"].to_s) + end + templated "listen" +end + +public_folder "assets" + +Kemal.run |
