2018-09-04 09:22:10 -05:00
|
|
|
# "Invidious" (which is an alternative front-end to YouTube)
|
2019-03-15 11:44:53 -05:00
|
|
|
# Copyright (C) 2019 Omar Roth
|
2018-01-28 11:32:40 -06:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-11-22 13:26:08 -06:00
|
|
|
require "digest/md5"
|
2019-01-23 14:15:19 -06:00
|
|
|
require "file_utils"
|
2017-11-23 01:48:55 -06:00
|
|
|
require "kemal"
|
2019-03-12 20:51:23 -05:00
|
|
|
require "markdown"
|
2018-07-18 14:26:02 -05:00
|
|
|
require "openssl/hmac"
|
2018-02-03 16:13:14 -06:00
|
|
|
require "option_parser"
|
2017-11-23 22:06:43 -06:00
|
|
|
require "pg"
|
2018-11-21 17:12:13 -06:00
|
|
|
require "sqlite3"
|
2018-01-16 14:02:35 -06:00
|
|
|
require "xml"
|
2018-03-09 12:42:23 -06:00
|
|
|
require "yaml"
|
2018-07-30 12:34:57 -05:00
|
|
|
require "zip"
|
2018-08-04 15:30:44 -05:00
|
|
|
require "./invidious/helpers/*"
|
2018-07-06 07:59:56 -05:00
|
|
|
require "./invidious/*"
|
2017-11-29 15:33:46 -06:00
|
|
|
|
2018-07-18 14:26:02 -05:00
|
|
|
CONFIG = Config.from_yaml(File.read("config/config.yml"))
|
2019-03-03 19:18:23 -06:00
|
|
|
HMAC_KEY = CONFIG.hmac_key || Random::Secure.hex(32)
|
2018-03-04 10:59:03 -06:00
|
|
|
|
2019-03-01 16:06:45 -06:00
|
|
|
config = CONFIG
|
2019-01-23 14:15:19 -06:00
|
|
|
logger = Invidious::LogHandler.new
|
|
|
|
|
2018-02-05 19:37:09 -06:00
|
|
|
Kemal.config.extra_options do |parser|
|
2018-02-03 16:13:14 -06:00
|
|
|
parser.banner = "Usage: invidious [arguments]"
|
2019-03-01 16:06:45 -06:00
|
|
|
parser.on("-c THREADS", "--channel-threads=THREADS", "Number of threads for refreshing channels (default: #{config.channel_threads})") do |number|
|
2018-04-09 22:07:09 -05:00
|
|
|
begin
|
2019-03-01 16:06:45 -06:00
|
|
|
config.channel_threads = number.to_i
|
2018-04-09 22:07:09 -05:00
|
|
|
rescue ex
|
|
|
|
puts "THREADS must be integer"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
2019-03-01 16:06:45 -06:00
|
|
|
parser.on("-f THREADS", "--feed-threads=THREADS", "Number of threads for refreshing feeds (default: #{config.feed_threads})") do |number|
|
2018-10-09 17:24:29 -05:00
|
|
|
begin
|
2019-03-01 16:06:45 -06:00
|
|
|
config.feed_threads = number.to_i
|
2018-10-09 17:24:29 -05:00
|
|
|
rescue ex
|
|
|
|
puts "THREADS must be integer"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
2019-01-23 14:15:19 -06:00
|
|
|
parser.on("-o OUTPUT", "--output=OUTPUT", "Redirect output (default: STDOUT)") do |output|
|
|
|
|
FileUtils.mkdir_p(File.dirname(output))
|
|
|
|
logger = Invidious::LogHandler.new(File.open(output, mode: "a"))
|
|
|
|
end
|
2018-02-13 10:43:15 -06:00
|
|
|
end
|
2018-02-11 16:48:27 -06:00
|
|
|
|
2019-02-27 16:52:37 -06:00
|
|
|
Kemal::CLI.new ARGV
|
2018-02-03 16:13:14 -06:00
|
|
|
|
2018-11-22 13:26:08 -06:00
|
|
|
YT_URL = URI.parse("https://www.youtube.com")
|
|
|
|
REDDIT_URL = URI.parse("https://www.reddit.com")
|
|
|
|
LOGIN_URL = URI.parse("https://accounts.google.com")
|
2019-03-03 19:18:23 -06:00
|
|
|
PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")
|
2019-03-20 09:20:51 -05:00
|
|
|
TEXTCAPTCHA_URL = URI.parse("http://textcaptcha.com/omarroth@protonmail.com.json")
|
2019-03-19 20:50:34 -05:00
|
|
|
CURRENT_BRANCH = {{ "#{`git branch | sed -n '/\* /s///p'`.strip}" }}
|
|
|
|
CURRENT_COMMIT = {{ "#{`git rev-list HEAD --max-count=1 --abbrev-commit`.strip}" }}
|
|
|
|
CURRENT_VERSION = {{ "#{`git describe --tags --abbrev=0`.strip}" }}
|
2018-03-04 22:25:03 -06:00
|
|
|
|
2018-12-20 15:32:09 -06:00
|
|
|
LOCALES = {
|
|
|
|
"ar" => load_locale("ar"),
|
|
|
|
"de" => load_locale("de"),
|
|
|
|
"en-US" => load_locale("en-US"),
|
2019-03-01 19:24:53 -06:00
|
|
|
"eu" => load_locale("eu"),
|
2019-01-21 15:04:09 -06:00
|
|
|
"fr" => load_locale("fr"),
|
2019-02-19 17:46:31 -06:00
|
|
|
"it" => load_locale("it"),
|
2018-12-26 09:29:12 -06:00
|
|
|
"nb_NO" => load_locale("nb_NO"),
|
2018-12-20 15:32:09 -06:00
|
|
|
"nl" => load_locale("nl"),
|
|
|
|
"pl" => load_locale("pl"),
|
|
|
|
"ru" => load_locale("ru"),
|
|
|
|
}
|
|
|
|
|
2019-03-03 23:22:34 -06:00
|
|
|
statistics = {
|
|
|
|
"error" => "Statistics are not availabile.",
|
|
|
|
}
|
2018-08-04 15:30:44 -05:00
|
|
|
|
2019-03-25 09:31:51 -05:00
|
|
|
proxies = PROXY_LIST
|
|
|
|
|
2018-10-31 11:07:58 -05:00
|
|
|
decrypt_function = [] of {name: String, value: Int32}
|
2019-02-09 12:22:18 -06:00
|
|
|
spawn do
|
|
|
|
update_decrypt_function do |function|
|
|
|
|
decrypt_function = function
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
2018-03-24 22:38:35 -05:00
|
|
|
|
2018-12-28 10:06:01 -06:00
|
|
|
before_all do |env|
|
|
|
|
env.response.headers["X-XSS-Protection"] = "1; mode=block;"
|
|
|
|
env.response.headers["X-Content-Type-Options"] = "nosniff"
|
2018-08-01 10:44:02 -05:00
|
|
|
|
2019-03-25 09:31:51 -05:00
|
|
|
preferences = DEFAULT_USER_PREFERENCES.dup
|
|
|
|
|
2018-12-28 10:06:01 -06:00
|
|
|
locale = env.params.query["hl"]?
|
|
|
|
locale ||= "en-US"
|
2019-03-25 09:31:51 -05:00
|
|
|
|
|
|
|
preferences.locale = locale
|
|
|
|
|
|
|
|
env.set "preferences", preferences
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2019-03-03 23:31:39 -06:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
# API Endpoints
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2019-03-01 19:25:16 -06:00
|
|
|
get "/api/v1/stats" do |env|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
if !config.statistics_enabled
|
|
|
|
error_message = {"error" => "Statistics are not enabled."}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 400
|
|
|
|
next error_message
|
2019-03-01 19:25:16 -06:00
|
|
|
end
|
|
|
|
|
2019-03-04 14:43:17 -06:00
|
|
|
if statistics["error"]?
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next statistics.to_json
|
2019-03-01 19:25:16 -06:00
|
|
|
end
|
2019-03-04 14:43:17 -06:00
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
statistics.to_json
|
2019-03-01 19:25:16 -06:00
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
get "/api/v1/captions/:id" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-26 18:44:37 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
id = env.params.url["id"]
|
2018-11-17 17:37:57 -06:00
|
|
|
region = env.params.query["region"]?
|
2018-07-29 21:01:28 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
client = make_client(YT_URL)
|
|
|
|
begin
|
2018-11-18 17:59:02 -06:00
|
|
|
video = fetch_video(id, proxies, region: region)
|
2018-10-06 22:22:22 -05:00
|
|
|
rescue ex : VideoRedirect
|
|
|
|
next env.redirect "/api/v1/captions/#{ex.message}"
|
2018-08-04 15:30:44 -05:00
|
|
|
rescue ex
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next
|
2018-07-28 08:24:53 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
captions = video.captions
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
label = env.params.query["label"]?
|
2018-09-30 10:13:07 -05:00
|
|
|
lang = env.params.query["lang"]?
|
|
|
|
tlang = env.params.query["tlang"]?
|
|
|
|
|
|
|
|
if !label && !lang
|
2018-08-04 15:30:44 -05:00
|
|
|
response = JSON.build do |json|
|
|
|
|
json.object do
|
|
|
|
json.field "captions" do
|
|
|
|
json.array do
|
2018-08-04 23:07:38 -05:00
|
|
|
captions.each do |caption|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.object do
|
2018-08-06 18:25:25 -05:00
|
|
|
json.field "label", caption.name.simpleText
|
|
|
|
json.field "languageCode", caption.languageCode
|
2018-09-19 14:08:59 -05:00
|
|
|
json.field "url", "/api/v1/captions/#{id}?label=#{URI.escape(caption.name.simpleText)}"
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-28 08:24:53 -05:00
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
next response
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2018-09-30 10:13:07 -05:00
|
|
|
env.response.content_type = "text/vtt"
|
|
|
|
|
2018-08-06 18:25:25 -05:00
|
|
|
caption = captions.select { |caption| caption.name.simpleText == label }
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2018-09-30 10:13:07 -05:00
|
|
|
if lang
|
|
|
|
caption = captions.select { |caption| caption.languageCode == lang }
|
|
|
|
end
|
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
if caption.empty?
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 404
|
|
|
|
next
|
2018-08-04 15:30:44 -05:00
|
|
|
else
|
2018-08-04 23:07:38 -05:00
|
|
|
caption = caption[0]
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2018-09-30 10:13:07 -05:00
|
|
|
caption_xml = client.get(caption.baseUrl + "&tlang=#{tlang}").body
|
2018-08-04 23:07:38 -05:00
|
|
|
caption_xml = XML.parse(caption_xml)
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
webvtt = <<-END_VTT
|
|
|
|
WEBVTT
|
|
|
|
Kind: captions
|
2018-09-30 10:13:07 -05:00
|
|
|
Language: #{tlang || caption.languageCode}
|
2018-07-28 08:24:53 -05:00
|
|
|
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
END_VTT
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2018-08-07 07:36:51 -05:00
|
|
|
caption_nodes = caption_xml.xpath_nodes("//transcript/text")
|
|
|
|
caption_nodes.each_with_index do |node, i|
|
2018-08-04 15:30:44 -05:00
|
|
|
start_time = node["start"].to_f.seconds
|
|
|
|
duration = node["dur"]?.try &.to_f.seconds
|
|
|
|
duration ||= start_time
|
2018-08-07 07:36:51 -05:00
|
|
|
|
|
|
|
if caption_nodes.size > i + 1
|
|
|
|
end_time = caption_nodes[i + 1]["start"].to_f.seconds
|
|
|
|
else
|
|
|
|
end_time = start_time + duration
|
|
|
|
end
|
2018-07-28 08:24:53 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
start_time = "#{start_time.hours.to_s.rjust(2, '0')}:#{start_time.minutes.to_s.rjust(2, '0')}:#{start_time.seconds.to_s.rjust(2, '0')}.#{start_time.milliseconds.to_s.rjust(3, '0')}"
|
|
|
|
end_time = "#{end_time.hours.to_s.rjust(2, '0')}:#{end_time.minutes.to_s.rjust(2, '0')}:#{end_time.seconds.to_s.rjust(2, '0')}.#{end_time.milliseconds.to_s.rjust(3, '0')}"
|
|
|
|
|
|
|
|
text = HTML.unescape(node.content)
|
2018-08-07 07:36:51 -05:00
|
|
|
text = text.gsub(/<font color="#[a-fA-F0-9]{6}">/, "")
|
|
|
|
text = text.gsub(/<\/font>/, "")
|
2018-08-04 15:30:44 -05:00
|
|
|
if md = text.match(/(?<name>.*) : (?<text>.*)/)
|
|
|
|
text = "<v #{md["name"]}>#{md["text"]}</v>"
|
2018-07-28 08:24:53 -05:00
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
|
|
|
|
webvtt = webvtt + <<-END_CUE
|
|
|
|
#{start_time} --> #{end_time}
|
|
|
|
#{text}
|
|
|
|
|
|
|
|
|
|
|
|
END_CUE
|
2018-07-28 08:24:53 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
webvtt
|
2018-07-28 08:24:53 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
get "/api/v1/comments/:id" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2019-02-06 17:55:22 -06:00
|
|
|
region = env.params.query["region"]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-26 18:44:37 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
id = env.params.url["id"]
|
2018-07-20 11:19:49 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
source = env.params.query["source"]?
|
|
|
|
source ||= "youtube"
|
2018-07-20 11:19:49 -05:00
|
|
|
|
2019-03-27 11:31:05 -05:00
|
|
|
thin_mode = env.params.query["thin_mode"]?
|
|
|
|
thin_mode = thin_mode == "true"
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
format = env.params.query["format"]?
|
|
|
|
format ||= "json"
|
2018-07-20 11:19:49 -05:00
|
|
|
|
2018-10-31 16:47:53 -05:00
|
|
|
continuation = env.params.query["continuation"]?
|
2018-08-04 15:30:44 -05:00
|
|
|
|
2018-10-31 16:47:53 -05:00
|
|
|
if source == "youtube"
|
|
|
|
begin
|
2019-03-29 16:34:19 -05:00
|
|
|
comments = fetch_youtube_comments(id, continuation, proxies, format, locale, thin_mode, region)
|
2018-10-31 16:47:53 -05:00
|
|
|
rescue ex
|
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-07-20 11:19:49 -05:00
|
|
|
end
|
|
|
|
|
2018-11-10 09:05:26 -06:00
|
|
|
next comments
|
2018-08-04 15:30:44 -05:00
|
|
|
elsif source == "reddit"
|
|
|
|
begin
|
2018-10-31 16:47:53 -05:00
|
|
|
comments, reddit_thread = fetch_reddit_comments(id)
|
2018-12-20 15:32:09 -06:00
|
|
|
content_html = template_reddit_comments(comments, locale)
|
2018-03-30 20:52:10 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
content_html = fill_links(content_html, "https", "www.reddit.com")
|
2018-09-03 22:15:47 -05:00
|
|
|
content_html = replace_links(content_html)
|
2018-08-04 15:30:44 -05:00
|
|
|
rescue ex
|
2018-09-06 10:19:28 -05:00
|
|
|
comments = nil
|
2018-08-04 15:30:44 -05:00
|
|
|
reddit_thread = nil
|
|
|
|
content_html = ""
|
|
|
|
end
|
2018-07-16 11:24:24 -05:00
|
|
|
|
2018-09-06 10:19:28 -05:00
|
|
|
if !reddit_thread || !comments
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 404
|
|
|
|
next
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-03-30 20:52:10 -05:00
|
|
|
|
2018-09-06 10:19:28 -05:00
|
|
|
if format == "json"
|
|
|
|
reddit_thread = JSON.parse(reddit_thread.to_json).as_h
|
|
|
|
reddit_thread["comments"] = JSON.parse(comments.to_json)
|
2019-01-25 10:50:18 -06:00
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
next reddit_thread.to_json
|
2018-09-06 10:19:28 -05:00
|
|
|
else
|
2019-01-25 10:50:18 -06:00
|
|
|
response = {
|
2018-09-06 10:19:28 -05:00
|
|
|
"title" => reddit_thread.title,
|
2018-09-06 18:18:36 -05:00
|
|
|
"permalink" => reddit_thread.permalink,
|
|
|
|
"contentHtml" => content_html,
|
2019-01-25 10:50:18 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
next response.to_json
|
2018-09-06 18:18:36 -05:00
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
2018-03-30 20:52:10 -05:00
|
|
|
|
2018-09-17 20:08:26 -05:00
|
|
|
get "/api/v1/insights/:id" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-17 20:08:26 -05:00
|
|
|
id = env.params.url["id"]
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2019-01-25 10:38:28 -06:00
|
|
|
error_message = {"error" => "YouTube has removed publicly-available analytics."}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 410
|
|
|
|
next error_message
|
2019-01-25 10:38:28 -06:00
|
|
|
|
2018-09-17 20:08:26 -05:00
|
|
|
client = make_client(YT_URL)
|
|
|
|
headers = HTTP::Headers.new
|
2018-09-25 17:55:32 -05:00
|
|
|
html = client.get("/watch?v=#{id}&gl=US&hl=en&disable_polymer=1")
|
2018-09-17 20:08:26 -05:00
|
|
|
|
|
|
|
headers["cookie"] = html.cookies.add_request_headers(headers)["cookie"]
|
|
|
|
headers["content-type"] = "application/x-www-form-urlencoded"
|
|
|
|
|
|
|
|
headers["x-client-data"] = "CIi2yQEIpbbJAQipncoBCNedygEIqKPKAQ=="
|
|
|
|
headers["x-spf-previous"] = "https://www.youtube.com/watch?v=#{id}"
|
|
|
|
headers["x-spf-referer"] = "https://www.youtube.com/watch?v=#{id}"
|
|
|
|
|
|
|
|
headers["x-youtube-client-name"] = "1"
|
|
|
|
headers["x-youtube-client-version"] = "2.20180719"
|
|
|
|
|
|
|
|
body = html.body
|
|
|
|
session_token = body.match(/'XSRF_TOKEN': "(?<session_token>[A-Za-z0-9\_\-\=]+)"/).not_nil!["session_token"]
|
|
|
|
|
|
|
|
post_req = {
|
|
|
|
"session_token" => session_token,
|
|
|
|
}
|
|
|
|
post_req = HTTP::Params.encode(post_req)
|
|
|
|
|
|
|
|
response = client.post("/insight_ajax?action_get_statistics_and_data=1&v=#{id}", headers, post_req).body
|
|
|
|
response = XML.parse(response)
|
|
|
|
|
|
|
|
html_content = XML.parse_html(response.xpath_node(%q(//html_content)).not_nil!.content)
|
|
|
|
graph_data = response.xpath_node(%q(//graph_data))
|
|
|
|
if !graph_data
|
|
|
|
error = html_content.xpath_node(%q(//p)).not_nil!.content
|
|
|
|
next {"error" => error}.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
graph_data = JSON.parse(graph_data.content)
|
|
|
|
|
|
|
|
view_count = 0_i64
|
|
|
|
time_watched = 0_i64
|
|
|
|
subscriptions_driven = 0
|
|
|
|
shares = 0
|
|
|
|
|
|
|
|
stats_nodes = html_content.xpath_nodes(%q(//table/tr/td))
|
|
|
|
stats_nodes.each do |node|
|
|
|
|
key = node.xpath_node(%q(.//span))
|
|
|
|
value = node.xpath_node(%q(.//div))
|
|
|
|
|
|
|
|
if !key || !value
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
key = key.content
|
|
|
|
value = value.content
|
|
|
|
|
|
|
|
case key
|
|
|
|
when "Views"
|
|
|
|
view_count = value.delete(", ").to_i64
|
|
|
|
when "Time watched"
|
|
|
|
time_watched = value
|
|
|
|
when "Subscriptions driven"
|
|
|
|
subscriptions_driven = value.delete(", ").to_i
|
|
|
|
when "Shares"
|
|
|
|
shares = value.delete(", ").to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
avg_view_duration_seconds = html_content.xpath_node(%q(//div[@id="stats-chart-tab-watch-time"]/span/span[2])).not_nil!.content
|
|
|
|
avg_view_duration_seconds = decode_length_seconds(avg_view_duration_seconds)
|
|
|
|
|
2019-01-25 10:50:18 -06:00
|
|
|
response = {
|
2018-09-17 20:08:26 -05:00
|
|
|
"viewCount" => view_count,
|
|
|
|
"timeWatchedText" => time_watched,
|
|
|
|
"subscriptionsDriven" => subscriptions_driven,
|
|
|
|
"shares" => shares,
|
|
|
|
"avgViewDurationSeconds" => avg_view_duration_seconds,
|
|
|
|
"graphData" => graph_data,
|
2019-01-25 10:50:18 -06:00
|
|
|
}
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
next response.to_json
|
2018-09-17 20:08:26 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
get "/api/v1/videos/:id" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-28 23:12:35 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
id = env.params.url["id"]
|
2018-11-17 17:33:30 -06:00
|
|
|
region = env.params.query["region"]?
|
2018-03-30 20:52:10 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
begin
|
2018-11-18 17:59:02 -06:00
|
|
|
video = fetch_video(id, proxies, region: region)
|
2018-10-06 22:22:22 -05:00
|
|
|
rescue ex : VideoRedirect
|
|
|
|
next env.redirect "/api/v1/videos/#{ex.message}"
|
2018-08-04 15:30:44 -05:00
|
|
|
rescue ex
|
2018-09-21 09:40:04 -05:00
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-03-30 20:52:10 -05:00
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
fmt_stream = video.fmt_stream(decrypt_function)
|
|
|
|
adaptive_fmts = video.adaptive_fmts(decrypt_function)
|
2018-03-30 20:52:10 -05:00
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
captions = video.captions
|
2018-03-30 20:52:10 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
video_info = JSON.build do |json|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-16 11:24:24 -05:00
|
|
|
|
2018-09-04 08:52:30 -05:00
|
|
|
video.description, description = html_to_content(video.description)
|
2018-07-05 19:48:55 -05:00
|
|
|
|
2018-08-10 08:44:23 -05:00
|
|
|
json.field "description", description
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "descriptionHtml", video.description
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "published", video.published.to_unix
|
2019-02-20 08:49:39 -06:00
|
|
|
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
|
2018-11-02 08:09:28 -05:00
|
|
|
json.field "keywords", video.keywords
|
2018-07-17 08:19:45 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "viewCount", video.views
|
|
|
|
json.field "likeCount", video.likes
|
|
|
|
json.field "dislikeCount", video.dislikes
|
2018-07-17 08:19:45 -05:00
|
|
|
|
2018-10-16 11:15:14 -05:00
|
|
|
json.field "paid", video.paid
|
|
|
|
json.field "premium", video.premium
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "isFamilyFriendly", video.is_family_friendly
|
|
|
|
json.field "allowedRegions", video.allowed_regions
|
|
|
|
json.field "genre", video.genre
|
2018-09-04 09:50:19 -05:00
|
|
|
json.field "genreUrl", video.genre_url
|
2018-08-04 15:30:44 -05:00
|
|
|
|
|
|
|
json.field "author", video.author
|
|
|
|
json.field "authorId", video.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{video.ucid}"
|
2018-10-13 20:01:58 -05:00
|
|
|
|
|
|
|
json.field "authorThumbnails" do
|
|
|
|
json.array do
|
2019-03-25 10:00:18 -05:00
|
|
|
qualities = {32, 48, 76, 100, 176, 512}
|
2018-10-13 20:01:58 -05:00
|
|
|
|
|
|
|
qualities.each do |quality|
|
|
|
|
json.object do
|
|
|
|
json.field "url", video.author_thumbnail.gsub("=s48-", "=s#{quality}-")
|
|
|
|
json.field "width", quality
|
|
|
|
json.field "height", quality
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-12 21:17:37 -05:00
|
|
|
json.field "subCountText", video.sub_count_text
|
2018-07-05 19:48:55 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "lengthSeconds", video.info["length_seconds"].to_i
|
2019-03-22 10:32:42 -05:00
|
|
|
json.field "allowRatings", video.allow_ratings
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "rating", video.info["avg_rating"].to_f32
|
2019-03-22 10:32:42 -05:00
|
|
|
json.field "isListed", video.is_listed
|
|
|
|
json.field "liveNow", video.live_now
|
2019-03-22 11:06:58 -05:00
|
|
|
json.field "isUpcoming", video.is_upcoming
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2019-03-22 12:24:47 -05:00
|
|
|
if video.premiere_timestamp
|
|
|
|
json.field "premiereTimestamp", video.premiere_timestamp.not_nil!.to_unix
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2019-01-12 12:00:44 -06:00
|
|
|
if video.player_response["streamingData"]?.try &.["hlsManifestUrl"]?
|
2019-03-05 12:56:59 -06:00
|
|
|
host_url = make_host_url(config, Kemal.config)
|
2019-01-19 10:41:20 -06:00
|
|
|
|
2018-08-07 12:36:55 -05:00
|
|
|
host_params = env.request.query_params
|
|
|
|
host_params.delete_all("v")
|
|
|
|
|
2019-01-12 12:00:44 -06:00
|
|
|
hlsvp = video.player_response["streamingData"]["hlsManifestUrl"].as_s
|
2018-08-07 12:36:55 -05:00
|
|
|
hlsvp = hlsvp.gsub("https://manifest.googlevideo.com", host_url)
|
|
|
|
|
2018-08-07 14:45:37 -05:00
|
|
|
json.field "hlsUrl", hlsvp
|
2018-08-07 12:36:55 -05:00
|
|
|
end
|
2018-07-31 23:56:17 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "adaptiveFormats" do
|
|
|
|
json.array do
|
2018-08-12 09:24:59 -05:00
|
|
|
adaptive_fmts.each do |fmt|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.object do
|
2018-08-12 09:24:59 -05:00
|
|
|
json.field "index", fmt["index"]
|
|
|
|
json.field "bitrate", fmt["bitrate"]
|
|
|
|
json.field "init", fmt["init"]
|
|
|
|
json.field "url", fmt["url"]
|
|
|
|
json.field "itag", fmt["itag"]
|
|
|
|
json.field "type", fmt["type"]
|
|
|
|
json.field "clen", fmt["clen"]
|
|
|
|
json.field "lmt", fmt["lmt"]
|
|
|
|
json.field "projectionType", fmt["projection_type"]
|
|
|
|
|
|
|
|
fmt_info = itag_to_metadata?(fmt["itag"])
|
|
|
|
if fmt_info
|
|
|
|
fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.to_i || 30
|
|
|
|
json.field "fps", fps
|
|
|
|
json.field "container", fmt_info["ext"]
|
|
|
|
json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"]
|
|
|
|
|
|
|
|
if fmt_info["height"]?
|
|
|
|
json.field "resolution", "#{fmt_info["height"]}p"
|
|
|
|
|
|
|
|
quality_label = "#{fmt_info["height"]}p"
|
|
|
|
if fps > 30
|
|
|
|
quality_label += "60"
|
|
|
|
end
|
|
|
|
json.field "qualityLabel", quality_label
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-12 09:24:59 -05:00
|
|
|
if fmt_info["width"]?
|
|
|
|
json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}"
|
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "formatStreams" do
|
|
|
|
json.array do
|
|
|
|
fmt_stream.each do |fmt|
|
|
|
|
json.object do
|
|
|
|
json.field "url", fmt["url"]
|
|
|
|
json.field "itag", fmt["itag"]
|
|
|
|
json.field "type", fmt["type"]
|
|
|
|
json.field "quality", fmt["quality"]
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-12 09:24:59 -05:00
|
|
|
fmt_info = itag_to_metadata?(fmt["itag"])
|
|
|
|
if fmt_info
|
|
|
|
fps = fmt_info["fps"]?.try &.to_i || fmt["fps"]?.try &.to_i || 30
|
|
|
|
json.field "fps", fps
|
|
|
|
json.field "container", fmt_info["ext"]
|
|
|
|
json.field "encoding", fmt_info["vcodec"]? || fmt_info["acodec"]
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-12 09:24:59 -05:00
|
|
|
if fmt_info["height"]?
|
|
|
|
json.field "resolution", "#{fmt_info["height"]}p"
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-12 09:24:59 -05:00
|
|
|
quality_label = "#{fmt_info["height"]}p"
|
|
|
|
if fps > 30
|
|
|
|
quality_label += "60"
|
|
|
|
end
|
|
|
|
json.field "qualityLabel", quality_label
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-12 09:24:59 -05:00
|
|
|
if fmt_info["width"]?
|
|
|
|
json.field "size", "#{fmt_info["width"]}x#{fmt_info["height"]}"
|
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "captions" do
|
|
|
|
json.array do
|
|
|
|
captions.each do |caption|
|
|
|
|
json.object do
|
2018-08-06 18:25:25 -05:00
|
|
|
json.field "label", caption.name.simpleText
|
|
|
|
json.field "languageCode", caption.languageCode
|
2018-09-19 14:08:59 -05:00
|
|
|
json.field "url", "/api/v1/captions/#{id}?label=#{URI.escape(caption.name.simpleText)}"
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
end
|
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "recommendedVideos" do
|
|
|
|
json.array do
|
2018-08-13 10:50:09 -05:00
|
|
|
video.info["rvs"]?.try &.split(",").each do |rv|
|
2018-08-04 15:30:44 -05:00
|
|
|
rv = HTTP::Params.parse(rv)
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
if rv["id"]?
|
|
|
|
json.object do
|
|
|
|
json.field "videoId", rv["id"]
|
|
|
|
json.field "title", rv["title"]
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, rv["id"], config, Kemal.config)
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
json.field "author", rv["author"]
|
2018-08-05 12:18:14 -05:00
|
|
|
json.field "lengthSeconds", rv["length_seconds"].to_i
|
2018-08-31 22:53:14 -05:00
|
|
|
json.field "viewCountText", rv["short_view_count_text"]
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
video_info
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
get "/api/v1/trending" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2019-01-25 10:50:18 -06:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2018-11-20 11:18:12 -06:00
|
|
|
region = env.params.query["region"]?
|
|
|
|
trending_type = env.params.query["type"]?
|
|
|
|
|
|
|
|
begin
|
2018-12-20 15:32:09 -06:00
|
|
|
trending = fetch_trending(trending_type, proxies, region, locale)
|
2018-11-20 11:18:12 -06:00
|
|
|
rescue ex
|
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-11-20 11:18:12 -06:00
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
|
|
|
|
videos = JSON.build do |json|
|
|
|
|
json.array do
|
2018-11-20 11:18:12 -06:00
|
|
|
trending.each do |video|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.object do
|
2018-08-10 09:44:19 -05:00
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2018-08-10 09:44:19 -05:00
|
|
|
json.field "lengthSeconds", video.length_seconds
|
|
|
|
json.field "viewCount", video.views
|
2018-08-05 12:43:54 -05:00
|
|
|
|
2018-08-10 09:44:19 -05:00
|
|
|
json.field "author", video.author
|
2018-09-19 10:37:00 -05:00
|
|
|
json.field "authorId", video.ucid
|
2018-08-10 09:44:19 -05:00
|
|
|
json.field "authorUrl", "/channel/#{video.ucid}"
|
2018-08-05 12:43:54 -05:00
|
|
|
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "published", video.published.to_unix
|
2019-02-20 08:49:39 -06:00
|
|
|
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
|
2018-08-10 09:44:19 -05:00
|
|
|
json.field "description", video.description
|
|
|
|
json.field "descriptionHtml", video.description_html
|
2018-11-20 11:18:12 -06:00
|
|
|
json.field "liveNow", video.live_now
|
|
|
|
json.field "paid", video.paid
|
|
|
|
json.field "premium", video.premium
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-30 12:34:57 -05:00
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
videos
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
get "/api/v1/channels/:ucid" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-21 09:40:04 -05:00
|
|
|
env.response.content_type = "application/json"
|
2018-08-10 09:44:19 -05:00
|
|
|
|
2018-09-21 09:40:04 -05:00
|
|
|
ucid = env.params.url["ucid"]
|
2018-11-13 19:04:25 -06:00
|
|
|
sort_by = env.params.query["sort_by"]?.try &.downcase
|
|
|
|
sort_by ||= "newest"
|
2018-09-04 21:04:40 -05:00
|
|
|
|
2018-09-21 09:40:04 -05:00
|
|
|
begin
|
2018-12-20 15:32:09 -06:00
|
|
|
author, ucid, auto_generated = get_about_info(ucid, locale)
|
2018-09-21 09:40:04 -05:00
|
|
|
rescue ex
|
2018-10-23 20:58:07 -05:00
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-09-04 21:04:40 -05:00
|
|
|
end
|
|
|
|
|
2018-09-06 08:43:22 -05:00
|
|
|
page = 1
|
2019-02-24 16:39:44 -06:00
|
|
|
if auto_generated
|
|
|
|
videos = [] of SearchVideo
|
|
|
|
count = 0
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
videos, count = get_60_videos(ucid, page, auto_generated, sort_by)
|
|
|
|
rescue ex
|
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2019-02-24 16:39:44 -06:00
|
|
|
end
|
2018-11-08 17:10:14 -06:00
|
|
|
end
|
2018-08-28 20:29:08 -05:00
|
|
|
|
2018-10-14 09:06:04 -05:00
|
|
|
client = make_client(YT_URL)
|
2018-08-04 15:30:44 -05:00
|
|
|
channel_html = client.get("/channel/#{ucid}/about?disable_polymer=1").body
|
|
|
|
channel_html = XML.parse_html(channel_html)
|
|
|
|
banner = channel_html.xpath_node(%q(//div[@id="gh-banner"]/style)).not_nil!.content
|
|
|
|
banner = "https:" + banner.match(/background-image: url\((?<url>[^)]+)\)/).not_nil!["url"]
|
2018-03-31 09:51:14 -05:00
|
|
|
|
2018-08-28 20:29:08 -05:00
|
|
|
author = channel_html.xpath_node(%q(//a[contains(@class, "branded-page-header-title-link")])).not_nil!.content
|
2018-08-04 15:30:44 -05:00
|
|
|
author_url = channel_html.xpath_node(%q(//a[@class="channel-header-profile-image-container spf-link"])).not_nil!["href"]
|
|
|
|
author_thumbnail = channel_html.xpath_node(%q(//img[@class="channel-header-profile-image"])).not_nil!["src"]
|
2018-09-04 19:27:10 -05:00
|
|
|
description_html = channel_html.xpath_node(%q(//div[contains(@class,"about-description")]))
|
|
|
|
description_html, description = html_to_content(description_html)
|
2018-03-31 09:51:14 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
paid = channel_html.xpath_node(%q(//meta[@itemprop="paid"])).not_nil!["content"] == "True"
|
|
|
|
is_family_friendly = channel_html.xpath_node(%q(//meta[@itemprop="isFamilyFriendly"])).not_nil!["content"] == "True"
|
|
|
|
allowed_regions = channel_html.xpath_node(%q(//meta[@itemprop="regionsAllowed"])).not_nil!["content"].split(",")
|
2018-03-31 09:51:14 -05:00
|
|
|
|
2018-11-27 22:07:45 -06:00
|
|
|
related_channels = channel_html.xpath_nodes(%q(//div[contains(@class, "branded-page-related-channels")]/ul/li))
|
|
|
|
related_channels = related_channels.map do |node|
|
|
|
|
related_id = node["data-external-id"]?
|
|
|
|
related_id ||= ""
|
|
|
|
|
|
|
|
anchor = node.xpath_node(%q(.//h3[contains(@class, "yt-lockup-title")]/a))
|
|
|
|
related_title = anchor.try &.["title"]
|
|
|
|
related_title ||= ""
|
|
|
|
|
|
|
|
related_author_url = anchor.try &.["href"]
|
|
|
|
related_author_url ||= ""
|
|
|
|
|
|
|
|
related_author_thumbnail = node.xpath_node(%q(.//img)).try &.["data-thumb"]
|
|
|
|
related_author_thumbnail ||= ""
|
|
|
|
|
|
|
|
{
|
|
|
|
id: related_id,
|
|
|
|
author: related_title,
|
|
|
|
author_url: related_author_url,
|
|
|
|
author_thumbnail: related_author_thumbnail,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-09-04 19:27:10 -05:00
|
|
|
total_views = 0_i64
|
|
|
|
sub_count = 0_i64
|
2018-11-04 09:37:12 -06:00
|
|
|
joined = Time.unix(0)
|
2018-09-04 19:27:10 -05:00
|
|
|
metadata = channel_html.xpath_nodes(%q(//span[@class="about-stat"]))
|
|
|
|
metadata.each do |item|
|
|
|
|
case item.content
|
|
|
|
when .includes? "views"
|
|
|
|
total_views = item.content.delete("views •,").to_i64
|
|
|
|
when .includes? "subscribers"
|
|
|
|
sub_count = item.content.delete("subscribers").delete(",").to_i64
|
|
|
|
when .includes? "Joined"
|
|
|
|
joined = Time.parse(item.content.lchop("Joined "), "%b %-d, %Y", Time::Location.local)
|
|
|
|
end
|
2018-08-22 11:06:31 -05:00
|
|
|
end
|
2018-03-31 09:51:14 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
channel_info = JSON.build do |json|
|
|
|
|
json.object do
|
2018-08-28 20:29:08 -05:00
|
|
|
json.field "author", author
|
|
|
|
json.field "authorId", ucid
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "authorUrl", author_url
|
|
|
|
|
|
|
|
json.field "authorBanners" do
|
|
|
|
json.array do
|
2019-03-25 10:00:18 -05:00
|
|
|
qualities = {
|
|
|
|
{width: 2560, height: 424},
|
|
|
|
{width: 2120, height: 351},
|
|
|
|
{width: 1060, height: 175},
|
|
|
|
}
|
2018-08-04 15:30:44 -05:00
|
|
|
qualities.each do |quality|
|
|
|
|
json.object do
|
|
|
|
json.field "url", banner.gsub("=w1060", "=w#{quality[:width]}")
|
|
|
|
json.field "width", quality[:width]
|
|
|
|
json.field "height", quality[:height]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
json.object do
|
|
|
|
json.field "url", banner.rchop("=w1060-fcrop64=1,00005a57ffffa5a8-nd-c0xffffffff-rj-k-no")
|
|
|
|
json.field "width", 512
|
|
|
|
json.field "height", 288
|
|
|
|
end
|
2018-07-18 14:26:02 -05:00
|
|
|
end
|
|
|
|
end
|
2018-03-31 09:51:14 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "authorThumbnails" do
|
|
|
|
json.array do
|
2019-03-25 10:00:18 -05:00
|
|
|
qualities = {32, 48, 76, 100, 176, 512}
|
2018-07-18 14:26:02 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
qualities.each do |quality|
|
|
|
|
json.object do
|
|
|
|
json.field "url", author_thumbnail.gsub("/s100-", "/s#{quality}-")
|
|
|
|
json.field "width", quality
|
|
|
|
json.field "height", quality
|
|
|
|
end
|
|
|
|
end
|
2018-07-18 14:26:02 -05:00
|
|
|
end
|
2018-03-31 09:51:14 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "subCount", sub_count
|
|
|
|
json.field "totalViews", total_views
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "joined", joined.to_unix
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "paid", paid
|
2018-03-31 09:51:14 -05:00
|
|
|
|
2019-02-24 16:39:44 -06:00
|
|
|
json.field "autoGenerated", auto_generated
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "isFamilyFriendly", is_family_friendly
|
|
|
|
json.field "description", description
|
2018-09-04 19:27:10 -05:00
|
|
|
json.field "descriptionHtml", description_html
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "allowedRegions", allowed_regions
|
2018-07-28 20:40:59 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "latestVideos" do
|
|
|
|
json.array do
|
2018-08-28 20:29:08 -05:00
|
|
|
videos.each do |video|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
2018-07-28 20:40:59 -05:00
|
|
|
|
2018-09-04 21:35:25 -05:00
|
|
|
if auto_generated
|
|
|
|
json.field "author", video.author
|
|
|
|
json.field "authorId", video.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{video.ucid}"
|
|
|
|
else
|
|
|
|
json.field "author", author
|
|
|
|
json.field "authorId", ucid
|
|
|
|
json.field "authorUrl", "/channel/#{ucid}"
|
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-08-28 20:29:08 -05:00
|
|
|
|
|
|
|
json.field "description", video.description
|
|
|
|
json.field "descriptionHtml", video.description_html
|
|
|
|
|
|
|
|
json.field "viewCount", video.views
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "published", video.published.to_unix
|
2019-02-20 08:49:39 -06:00
|
|
|
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
|
2018-08-28 20:29:08 -05:00
|
|
|
json.field "lengthSeconds", video.length_seconds
|
2018-11-08 16:35:57 -06:00
|
|
|
json.field "liveNow", video.live_now
|
2018-10-16 11:15:14 -05:00
|
|
|
json.field "paid", video.paid
|
|
|
|
json.field "premium", video.premium
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-27 22:07:45 -06:00
|
|
|
|
|
|
|
json.field "relatedChannels" do
|
|
|
|
json.array do
|
|
|
|
related_channels.each do |related_channel|
|
|
|
|
json.object do
|
|
|
|
json.field "author", related_channel[:author]
|
|
|
|
json.field "authorId", related_channel[:id]
|
|
|
|
json.field "authorUrl", related_channel[:author_url]
|
|
|
|
|
|
|
|
json.field "authorThumbnails" do
|
|
|
|
json.array do
|
2019-03-25 10:00:18 -05:00
|
|
|
qualities = {32, 48, 76, 100, 176, 512}
|
2018-11-27 22:07:45 -06:00
|
|
|
|
|
|
|
qualities.each do |quality|
|
|
|
|
json.object do
|
|
|
|
json.field "url", related_channel[:author_thumbnail].gsub("=s48-", "=s#{quality}-")
|
|
|
|
json.field "width", quality
|
|
|
|
json.field "height", quality
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
2018-07-28 20:40:59 -05:00
|
|
|
end
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
channel_info
|
2018-07-16 08:18:59 -05:00
|
|
|
end
|
|
|
|
|
2018-09-18 10:47:22 -05:00
|
|
|
["/api/v1/channels/:ucid/videos", "/api/v1/channels/videos/:ucid"].each do |route|
|
|
|
|
get route do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-21 09:40:04 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
ucid = env.params.url["ucid"]
|
|
|
|
page = env.params.query["page"]?.try &.to_i?
|
|
|
|
page ||= 1
|
2019-02-15 17:28:54 -06:00
|
|
|
sort_by = env.params.query["sort"]?.try &.downcase
|
|
|
|
sort_by ||= env.params.query["sort_by"]?.try &.downcase
|
2018-11-13 19:11:16 -06:00
|
|
|
sort_by ||= "newest"
|
2018-07-16 08:18:59 -05:00
|
|
|
|
2018-09-21 09:40:04 -05:00
|
|
|
begin
|
2018-12-20 15:32:09 -06:00
|
|
|
author, ucid, auto_generated = get_about_info(ucid, locale)
|
2018-09-21 09:40:04 -05:00
|
|
|
rescue ex
|
2018-10-23 20:58:07 -05:00
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-09-20 09:36:09 -05:00
|
|
|
end
|
2018-07-16 08:18:59 -05:00
|
|
|
|
2018-11-08 17:10:14 -06:00
|
|
|
begin
|
2018-11-13 19:11:16 -06:00
|
|
|
videos, count = get_60_videos(ucid, page, auto_generated, sort_by)
|
2018-11-08 17:10:14 -06:00
|
|
|
rescue ex
|
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-11-08 17:10:14 -06:00
|
|
|
end
|
2018-07-29 21:01:28 -05:00
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
result = JSON.build do |json|
|
|
|
|
json.array do
|
|
|
|
videos.each do |video|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
2018-08-04 15:30:44 -05:00
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
if auto_generated
|
|
|
|
json.field "author", video.author
|
|
|
|
json.field "authorId", video.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{video.ucid}"
|
|
|
|
else
|
|
|
|
json.field "author", author
|
|
|
|
json.field "authorId", ucid
|
|
|
|
json.field "authorUrl", "/channel/#{ucid}"
|
|
|
|
end
|
2018-09-04 21:35:25 -05:00
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-09-20 09:36:09 -05:00
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "description", video.description
|
|
|
|
json.field "descriptionHtml", video.description_html
|
2018-08-04 15:30:44 -05:00
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "viewCount", video.views
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "published", video.published.to_unix
|
2019-02-20 08:49:39 -06:00
|
|
|
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "lengthSeconds", video.length_seconds
|
2018-11-08 16:17:47 -06:00
|
|
|
json.field "liveNow", video.live_now
|
2018-10-16 11:15:14 -05:00
|
|
|
json.field "paid", video.paid
|
|
|
|
json.field "premium", video.premium
|
2018-09-20 09:36:09 -05:00
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-16 08:18:59 -05:00
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
result
|
2018-09-20 09:36:09 -05:00
|
|
|
end
|
2018-09-18 10:47:22 -05:00
|
|
|
end
|
2018-07-16 08:18:59 -05:00
|
|
|
|
2019-02-19 17:00:06 -06:00
|
|
|
["/api/v1/channels/:ucid/latest", "/api/v1/channels/latest/:ucid"].each do |route|
|
|
|
|
get route do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2019-02-20 08:49:39 -06:00
|
|
|
|
2019-02-19 17:00:06 -06:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
ucid = env.params.url["ucid"]
|
|
|
|
|
|
|
|
begin
|
|
|
|
videos = get_latest_videos(ucid)
|
|
|
|
rescue ex
|
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2019-02-19 17:00:06 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
response = JSON.build do |json|
|
|
|
|
json.array do
|
|
|
|
videos.each do |video|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
|
|
|
|
|
|
|
json.field "authorId", ucid
|
|
|
|
json.field "authorUrl", "/channel/#{ucid}"
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2019-02-19 17:00:06 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
json.field "description", video.description
|
|
|
|
json.field "descriptionHtml", video.description_html
|
|
|
|
|
|
|
|
json.field "viewCount", video.views
|
|
|
|
json.field "published", video.published.to_unix
|
2019-02-20 08:49:39 -06:00
|
|
|
json.field "publishedText", translate(locale, "`x` ago", recode_date(video.published, locale))
|
2019-02-19 17:00:06 -06:00
|
|
|
json.field "lengthSeconds", video.length_seconds
|
|
|
|
json.field "liveNow", video.live_now
|
|
|
|
json.field "paid", video.paid
|
|
|
|
json.field "premium", video.premium
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
response
|
2019-02-19 17:00:06 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-19 17:05:27 -06:00
|
|
|
["/api/v1/channels/:ucid/playlists", "/api/v1/channels/playlists/:ucid"].each do |route|
|
|
|
|
get route do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2019-02-19 17:05:27 -06:00
|
|
|
|
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
ucid = env.params.url["ucid"]
|
|
|
|
continuation = env.params.query["continuation"]?
|
|
|
|
sort_by = env.params.query["sort"]?.try &.downcase
|
|
|
|
sort_by ||= env.params.query["sort_by"]?.try &.downcase
|
|
|
|
sort_by ||= "last"
|
|
|
|
|
|
|
|
begin
|
|
|
|
author, ucid, auto_generated = get_about_info(ucid, locale)
|
|
|
|
rescue ex
|
2019-03-23 10:24:30 -05:00
|
|
|
error_message = {"error" => ex.message}.to_json
|
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2019-02-19 17:05:27 -06:00
|
|
|
end
|
|
|
|
|
2019-02-24 16:39:44 -06:00
|
|
|
items, continuation = fetch_channel_playlists(ucid, author, auto_generated, continuation, sort_by)
|
2019-02-19 17:05:27 -06:00
|
|
|
|
|
|
|
response = JSON.build do |json|
|
|
|
|
json.object do
|
|
|
|
json.field "playlists" do
|
|
|
|
json.array do
|
|
|
|
items.each do |item|
|
|
|
|
json.object do
|
|
|
|
if item.is_a?(SearchPlaylist)
|
|
|
|
json.field "title", item.title
|
|
|
|
json.field "playlistId", item.id
|
|
|
|
|
|
|
|
json.field "author", item.author
|
|
|
|
json.field "authorId", item.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{item.ucid}"
|
|
|
|
|
|
|
|
json.field "videoCount", item.video_count
|
|
|
|
json.field "videos" do
|
|
|
|
json.array do
|
|
|
|
item.videos.each do |video|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
|
|
|
json.field "lengthSeconds", video.length_seconds
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2019-02-19 17:05:27 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
json.field "continuation", continuation
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
response
|
2019-02-19 17:05:27 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-22 10:49:42 -05:00
|
|
|
get "/api/v1/channels/search/:ucid" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-22 10:49:42 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
ucid = env.params.url["ucid"]
|
|
|
|
|
|
|
|
query = env.params.query["q"]?
|
|
|
|
query ||= ""
|
|
|
|
|
|
|
|
page = env.params.query["page"]?.try &.to_i?
|
|
|
|
page ||= 1
|
|
|
|
|
|
|
|
count, search_results = channel_search(query, page, ucid)
|
|
|
|
response = JSON.build do |json|
|
|
|
|
json.array do
|
|
|
|
search_results.each do |item|
|
|
|
|
json.object do
|
|
|
|
case item
|
|
|
|
when SearchVideo
|
|
|
|
json.field "type", "video"
|
|
|
|
json.field "title", item.title
|
|
|
|
json.field "videoId", item.id
|
|
|
|
|
|
|
|
json.field "author", item.author
|
|
|
|
json.field "authorId", item.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{item.ucid}"
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, item.id, config, Kemal.config)
|
2018-09-22 10:49:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
json.field "description", item.description
|
|
|
|
json.field "descriptionHtml", item.description_html
|
|
|
|
|
|
|
|
json.field "viewCount", item.views
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "published", item.published.to_unix
|
2019-02-20 08:49:39 -06:00
|
|
|
json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published, locale))
|
2018-09-22 10:49:42 -05:00
|
|
|
json.field "lengthSeconds", item.length_seconds
|
|
|
|
json.field "liveNow", item.live_now
|
2018-10-16 11:15:14 -05:00
|
|
|
json.field "paid", item.paid
|
|
|
|
json.field "premium", item.premium
|
2018-09-22 10:49:42 -05:00
|
|
|
when SearchPlaylist
|
|
|
|
json.field "type", "playlist"
|
|
|
|
json.field "title", item.title
|
|
|
|
json.field "playlistId", item.id
|
|
|
|
|
|
|
|
json.field "author", item.author
|
|
|
|
json.field "authorId", item.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{item.ucid}"
|
|
|
|
|
2018-09-22 11:14:57 -05:00
|
|
|
json.field "videoCount", item.video_count
|
2018-09-22 10:49:42 -05:00
|
|
|
json.field "videos" do
|
|
|
|
json.array do
|
|
|
|
item.videos.each do |video|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
|
|
|
json.field "lengthSeconds", video.length_seconds
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-09-22 10:49:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when SearchChannel
|
|
|
|
json.field "type", "channel"
|
|
|
|
json.field "author", item.author
|
|
|
|
json.field "authorId", item.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{item.ucid}"
|
|
|
|
|
|
|
|
json.field "authorThumbnails" do
|
|
|
|
json.array do
|
2019-03-25 10:00:18 -05:00
|
|
|
qualities = {32, 48, 76, 100, 176, 512}
|
2018-09-22 10:49:42 -05:00
|
|
|
|
|
|
|
qualities.each do |quality|
|
|
|
|
json.object do
|
|
|
|
json.field "url", item.author_thumbnail.gsub("=s176-", "=s#{quality}-")
|
|
|
|
json.field "width", quality
|
|
|
|
json.field "height", quality
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
json.field "subCount", item.subscriber_count
|
|
|
|
json.field "videoCount", item.video_count
|
|
|
|
json.field "description", item.description
|
|
|
|
json.field "descriptionHtml", item.description_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
response
|
2018-09-22 10:49:42 -05:00
|
|
|
end
|
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
get "/api/v1/search" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2019-02-06 18:21:40 -06:00
|
|
|
region = env.params.query["region"]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-22 10:49:42 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
query = env.params.query["q"]?
|
|
|
|
query ||= ""
|
2018-08-02 17:18:57 -05:00
|
|
|
|
2018-08-04 15:30:44 -05:00
|
|
|
page = env.params.query["page"]?.try &.to_i?
|
|
|
|
page ||= 1
|
|
|
|
|
2018-08-04 17:12:58 -05:00
|
|
|
sort_by = env.params.query["sort_by"]?.try &.downcase
|
|
|
|
sort_by ||= "relevance"
|
|
|
|
|
|
|
|
date = env.params.query["date"]?.try &.downcase
|
|
|
|
date ||= ""
|
|
|
|
|
2019-02-26 14:31:37 -06:00
|
|
|
duration = env.params.query["duration"]?.try &.downcase
|
2018-08-04 17:12:58 -05:00
|
|
|
duration ||= ""
|
|
|
|
|
|
|
|
features = env.params.query["features"]?.try &.split(",").map { |feature| feature.downcase }
|
|
|
|
features ||= [] of String
|
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
content_type = env.params.query["type"]?.try &.downcase
|
|
|
|
content_type ||= "video"
|
2018-08-04 17:12:58 -05:00
|
|
|
|
|
|
|
begin
|
2018-09-17 16:38:18 -05:00
|
|
|
search_params = produce_search_params(sort_by, date, content_type, duration, features)
|
2018-08-04 17:12:58 -05:00
|
|
|
rescue ex
|
2018-09-20 09:36:09 -05:00
|
|
|
env.response.status_code = 400
|
2018-08-04 17:12:58 -05:00
|
|
|
next JSON.build do |json|
|
|
|
|
json.object do
|
|
|
|
json.field "error", ex.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-06 18:21:40 -06:00
|
|
|
count, search_results = search(query, page, search_params, proxies, region).as(Tuple)
|
2018-08-04 23:07:38 -05:00
|
|
|
response = JSON.build do |json|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.array do
|
2018-09-20 09:36:09 -05:00
|
|
|
search_results.each do |item|
|
2018-08-04 15:30:44 -05:00
|
|
|
json.object do
|
2018-09-20 09:36:09 -05:00
|
|
|
case item
|
|
|
|
when SearchVideo
|
|
|
|
json.field "type", "video"
|
|
|
|
json.field "title", item.title
|
|
|
|
json.field "videoId", item.id
|
|
|
|
|
|
|
|
json.field "author", item.author
|
|
|
|
json.field "authorId", item.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{item.ucid}"
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, item.id, config, Kemal.config)
|
2018-09-20 09:36:09 -05:00
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "description", item.description
|
|
|
|
json.field "descriptionHtml", item.description_html
|
|
|
|
|
|
|
|
json.field "viewCount", item.views
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "published", item.published.to_unix
|
2019-02-20 08:49:39 -06:00
|
|
|
json.field "publishedText", translate(locale, "`x` ago", recode_date(item.published, locale))
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "lengthSeconds", item.length_seconds
|
|
|
|
json.field "liveNow", item.live_now
|
2018-10-16 11:15:14 -05:00
|
|
|
json.field "paid", item.paid
|
|
|
|
json.field "premium", item.premium
|
2018-09-20 09:36:09 -05:00
|
|
|
when SearchPlaylist
|
|
|
|
json.field "type", "playlist"
|
|
|
|
json.field "title", item.title
|
|
|
|
json.field "playlistId", item.id
|
|
|
|
|
|
|
|
json.field "author", item.author
|
|
|
|
json.field "authorId", item.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{item.ucid}"
|
|
|
|
|
2018-09-22 11:14:57 -05:00
|
|
|
json.field "videoCount", item.video_count
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "videos" do
|
|
|
|
json.array do
|
|
|
|
item.videos.each do |video|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
|
|
|
json.field "lengthSeconds", video.length_seconds
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-09-20 09:36:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when SearchChannel
|
|
|
|
json.field "type", "channel"
|
|
|
|
json.field "author", item.author
|
|
|
|
json.field "authorId", item.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{item.ucid}"
|
|
|
|
|
|
|
|
json.field "authorThumbnails" do
|
|
|
|
json.array do
|
2019-03-25 10:00:18 -05:00
|
|
|
qualities = {32, 48, 76, 100, 176, 512}
|
2018-09-20 09:36:09 -05:00
|
|
|
|
|
|
|
qualities.each do |quality|
|
|
|
|
json.object do
|
|
|
|
json.field "url", item.author_thumbnail.gsub("=s176-", "=s#{quality}-")
|
|
|
|
json.field "width", quality
|
|
|
|
json.field "height", quality
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-04 15:30:44 -05:00
|
|
|
|
2018-09-20 09:36:09 -05:00
|
|
|
json.field "subCount", item.subscriber_count
|
|
|
|
json.field "videoCount", item.video_count
|
|
|
|
json.field "description", item.description
|
|
|
|
json.field "descriptionHtml", item.description_html
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
response
|
2018-08-02 17:18:57 -05:00
|
|
|
end
|
|
|
|
|
2018-08-15 10:22:36 -05:00
|
|
|
get "/api/v1/playlists/:plid" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-21 09:40:04 -05:00
|
|
|
env.response.content_type = "application/json"
|
2018-08-15 10:22:36 -05:00
|
|
|
plid = env.params.url["plid"]
|
|
|
|
|
|
|
|
page = env.params.query["page"]?.try &.to_i?
|
|
|
|
page ||= 1
|
|
|
|
|
2018-10-07 21:11:33 -05:00
|
|
|
format = env.params.query["format"]?
|
|
|
|
format ||= "json"
|
|
|
|
|
|
|
|
continuation = env.params.query["continuation"]?
|
|
|
|
|
2018-10-06 22:18:50 -05:00
|
|
|
if plid.starts_with? "RD"
|
|
|
|
next env.redirect "/api/v1/mixes/#{plid}"
|
|
|
|
end
|
|
|
|
|
2018-08-15 10:22:36 -05:00
|
|
|
begin
|
2018-12-20 15:32:09 -06:00
|
|
|
playlist = fetch_playlist(plid, locale)
|
2018-08-15 10:22:36 -05:00
|
|
|
rescue ex
|
2018-09-21 09:40:04 -05:00
|
|
|
error_message = {"error" => "Playlist is empty"}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-08-15 10:22:36 -05:00
|
|
|
end
|
|
|
|
|
2018-09-28 09:54:45 -05:00
|
|
|
begin
|
2018-12-20 15:32:09 -06:00
|
|
|
videos = fetch_playlist_videos(plid, page, playlist.video_count, continuation, locale)
|
2018-09-28 09:54:45 -05:00
|
|
|
rescue ex
|
|
|
|
videos = [] of PlaylistVideo
|
|
|
|
end
|
|
|
|
|
2018-08-15 10:22:36 -05:00
|
|
|
response = JSON.build do |json|
|
|
|
|
json.object do
|
|
|
|
json.field "title", playlist.title
|
2018-09-22 11:34:29 -05:00
|
|
|
json.field "playlistId", playlist.id
|
2018-08-15 10:22:36 -05:00
|
|
|
|
|
|
|
json.field "author", playlist.author
|
|
|
|
json.field "authorId", playlist.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{playlist.ucid}"
|
|
|
|
|
2018-09-25 10:28:40 -05:00
|
|
|
json.field "authorThumbnails" do
|
|
|
|
json.array do
|
2019-03-25 10:00:18 -05:00
|
|
|
qualities = {32, 48, 76, 100, 176, 512}
|
2018-09-25 10:28:40 -05:00
|
|
|
|
|
|
|
qualities.each do |quality|
|
|
|
|
json.object do
|
|
|
|
json.field "url", playlist.author_thumbnail.gsub("=s100-", "=s#{quality}-")
|
|
|
|
json.field "width", quality
|
|
|
|
json.field "height", quality
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-15 10:22:36 -05:00
|
|
|
json.field "description", playlist.description
|
2018-09-04 19:27:10 -05:00
|
|
|
json.field "descriptionHtml", playlist.description_html
|
2018-08-15 10:22:36 -05:00
|
|
|
json.field "videoCount", playlist.video_count
|
|
|
|
|
|
|
|
json.field "viewCount", playlist.views
|
2018-11-04 09:37:12 -06:00
|
|
|
json.field "updated", playlist.updated.to_unix
|
2018-08-15 10:22:36 -05:00
|
|
|
|
|
|
|
json.field "videos" do
|
|
|
|
json.array do
|
|
|
|
videos.each do |video|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
2018-09-22 11:34:29 -05:00
|
|
|
json.field "videoId", video.id
|
2018-08-15 10:22:36 -05:00
|
|
|
|
|
|
|
json.field "author", video.author
|
|
|
|
json.field "authorId", video.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{video.ucid}"
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-08-15 10:22:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
json.field "index", video.index
|
|
|
|
json.field "lengthSeconds", video.length_seconds
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:11:33 -05:00
|
|
|
if format == "html"
|
|
|
|
response = JSON.parse(response)
|
|
|
|
playlist_html = template_playlist(response)
|
|
|
|
next_video = response["videos"].as_a[1]?.try &.["videoId"]
|
|
|
|
|
|
|
|
response = {
|
|
|
|
"playlistHtml" => playlist_html,
|
|
|
|
"nextVideo" => next_video,
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2019-03-23 14:05:13 -05:00
|
|
|
response
|
2018-08-15 10:22:36 -05:00
|
|
|
end
|
|
|
|
|
2018-09-28 23:12:35 -05:00
|
|
|
get "/api/v1/mixes/:rdid" do |env|
|
2019-03-11 12:44:25 -05:00
|
|
|
locale = LOCALES[env.get("preferences").as(Preferences).locale]?
|
2018-12-20 15:32:09 -06:00
|
|
|
|
2018-09-28 23:12:35 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
rdid = env.params.url["rdid"]
|
|
|
|
|
|
|
|
continuation = env.params.query["continuation"]?
|
2019-02-15 17:28:54 -06:00
|
|
|
continuation ||= rdid.lchop("RD")[0, 11]
|
2018-09-28 23:12:35 -05:00
|
|
|
|
2018-10-07 21:11:33 -05:00
|
|
|
format = env.params.query["format"]?
|
|
|
|
format ||= "json"
|
|
|
|
|
2018-09-28 23:12:35 -05:00
|
|
|
begin
|
2018-12-20 15:32:09 -06:00
|
|
|
mix = fetch_mix(rdid, continuation, locale: locale)
|
2018-10-31 09:15:17 -05:00
|
|
|
|
|
|
|
if !rdid.ends_with? continuation
|
|
|
|
mix = fetch_mix(rdid, mix.videos[1].id)
|
|
|
|
index = mix.videos.index(mix.videos.select { |video| video.id == continuation }[0]?)
|
|
|
|
end
|
|
|
|
index ||= 0
|
|
|
|
|
|
|
|
mix.videos = mix.videos[index..-1]
|
2018-09-28 23:12:35 -05:00
|
|
|
rescue ex
|
|
|
|
error_message = {"error" => ex.message}.to_json
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 500
|
|
|
|
next error_message
|
2018-09-28 23:12:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
response = JSON.build do |json|
|
|
|
|
json.object do
|
|
|
|
json.field "title", mix.title
|
|
|
|
json.field "mixId", mix.id
|
|
|
|
|
|
|
|
json.field "videos" do
|
|
|
|
json.array do
|
|
|
|
mix.videos.each do |video|
|
|
|
|
json.object do
|
|
|
|
json.field "title", video.title
|
|
|
|
json.field "videoId", video.id
|
|
|
|
json.field "author", video.author
|
|
|
|
|
|
|
|
json.field "authorId", video.ucid
|
|
|
|
json.field "authorUrl", "/channel/#{video.ucid}"
|
|
|
|
|
|
|
|
json.field "videoThumbnails" do
|
|
|
|
json.array do
|
2019-03-08 14:42:37 -06:00
|
|
|
generate_thumbnails(json, video.id, config, Kemal.config)
|
2018-09-28 23:12:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
json.field "index", video.index
|
|
|
|
json.field "lengthSeconds", video.length_seconds
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-07 21:11:33 -05:00
|
|
|
if format == "html"
|
|
|
|
response = JSON.parse(response)
|
|
|
|
playlist_html = template_mix(response)
|
|
|
|
next_video = response["videos"].as_a[1]?.try &.["videoId"]
|
2018-10-31 09:15:17 -05:00
|
|
|
next_video ||= ""
|
2018-10-07 21:11:33 -05:00
|
|
|
|
|
|
|
response = {
|
|
|
|
"playlistHtml" => playlist_html,
|
|
|
|
"nextVideo" => next_video,
|
|
|
|
}.to_json
|
|
|
|
end
|
|
|
|
|
2019-03-23 10:24:30 -05:00
|
|
|
response
|
2018-09-28 23:12:35 -05:00
|
|
|
end
|
|
|
|
|
2018-08-07 13:10:52 -05:00
|
|
|
get "/api/manifest/dash/id/videoplayback" do |env|
|
2018-08-07 13:23:27 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-08-07 13:10:52 -05:00
|
|
|
env.redirect "/videoplayback?#{env.params.query}"
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/api/manifest/dash/id/videoplayback/*" do |env|
|
2018-08-07 13:23:27 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-08-07 13:10:52 -05:00
|
|
|
env.redirect env.request.path.lchop("/api/manifest/dash/id")
|
|
|
|
end
|
|
|
|
|
2018-07-16 08:18:59 -05:00
|
|
|
get "/api/manifest/dash/id/:id" do |env|
|
|
|
|
env.response.headers.add("Access-Control-Allow-Origin", "*")
|
|
|
|
env.response.content_type = "application/dash+xml"
|
|
|
|
|
|
|
|
local = env.params.query["local"]?.try &.== "true"
|
|
|
|
id = env.params.url["id"]
|
2018-11-17 17:37:57 -06:00
|
|
|
region = env.params.query["region"]?
|
2018-07-16 08:18:59 -05:00
|
|
|
|
|
|
|
client = make_client(YT_URL)
|
|
|
|
begin
|
2018-11-18 17:59:02 -06:00
|
|
|
video = fetch_video(id, proxies, region: region)
|
2018-10-06 22:22:22 -05:00
|
|
|
rescue ex : VideoRedirect
|
2019-02-08 09:49:40 -06:00
|
|
|
url = "/api/manifest/dash/id/#{ex.message}"
|
|
|
|
if local
|
|
|
|
url += "?local=true"
|
|
|
|
end
|
|
|
|
|
|
|
|
next env.redirect url
|
2018-07-16 08:18:59 -05:00
|
|
|
rescue ex
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 403
|
|
|
|
next
|
2018-07-16 08:18:59 -05:00
|
|
|
end
|
|
|
|
|
2019-03-22 10:32:42 -05:00
|
|
|
if dashmpd = video.player_response["streamingData"]?.try &.["dashManifestUrl"]?.try &.as_s
|
2019-02-25 09:11:41 -06:00
|
|
|
manifest = client.get(dashmpd).body
|
2018-07-16 08:18:59 -05:00
|
|
|
|
|
|
|
manifest = manifest.gsub(/<BaseURL>[^<]+<\/BaseURL>/) do |baseurl|
|
|
|
|
url = baseurl.lchop("<BaseURL>")
|
|
|
|
url = url.rchop("</BaseURL>")
|
|
|
|
|
|
|
|
if local
|
2018-08-07 13:14:58 -05:00
|
|
|
url = URI.parse(url).full_path.lchop("/")
|
2018-07-16 08:18:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
"<BaseURL>#{url}</BaseURL>"
|
|
|
|
end
|
|
|
|
|
|
|
|
next manifest
|
|
|
|
end
|
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
adaptive_fmts = video.adaptive_fmts(decrypt_function)
|
2018-07-16 08:18:59 -05:00
|
|
|
|
|
|
|
if local
|
|
|
|
adaptive_fmts.each do |fmt|
|
2018-08-07 13:14:58 -05:00
|
|
|
fmt["url"] = URI.parse(fmt["url"]).full_path.lchop("/")
|
2018-07-16 08:18:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-07 11:40:30 -05:00
|
|
|
audio_streams = video.audio_streams(adaptive_fmts).select { |stream| stream["type"].starts_with? "audio/mp4" }
|
2019-02-08 09:49:40 -06:00
|
|
|
video_streams = video.video_streams(adaptive_fmts).select { |stream| stream["type"].starts_with? "video/mp4" }.uniq { |stream| stream["size"] }
|
2018-08-04 23:07:38 -05:00
|
|
|
|
2018-07-16 08:18:59 -05:00
|
|
|
manifest = XML.build(indent: " ", encoding: "UTF-8") do |xml|
|
2018-08-11 15:01:22 -05:00
|
|
|
xml.element("MPD", "xmlns": "urn:mpeg:dash:schema:mpd:2011",
|
|
|
|
"profiles": "urn:mpeg:dash:profile:isoff-live:2011", minBufferTime: "PT1.5S", type: "static",
|
2018-07-16 08:18:59 -05:00
|
|
|
mediaPresentationDuration: "PT#{video.info["length_seconds"]}S") do
|
|
|
|
xml.element("Period") do
|
2018-08-09 08:56:35 -05:00
|
|
|
xml.element("AdaptationSet", mimeType: "audio/mp4", startWithSAP: 1, subsegmentAlignment: true) do
|
2018-07-16 08:18:59 -05:00
|
|
|
audio_streams.each do |fmt|
|
2018-08-07 11:40:30 -05:00
|
|
|
codecs = fmt["type"].split("codecs=")[1].strip('"')
|
2018-08-11 14:29:51 -05:00
|
|
|
bandwidth = fmt["bitrate"]
|
2018-07-16 08:18:59 -05:00
|
|
|
itag = fmt["itag"]
|
|
|
|
url = fmt["url"]
|
|
|
|
|
|
|
|
xml.element("Representation", id: fmt["itag"], codecs: codecs, bandwidth: bandwidth) do
|
2018-07-29 10:02:41 -05:00
|
|
|
xml.element("AudioChannelConfiguration", schemeIdUri: "urn:mpeg:dash:23003:3:audio_channel_configuration:2011",
|
|
|
|
value: "2")
|
2018-07-16 08:18:59 -05:00
|
|
|
xml.element("BaseURL") { xml.text url }
|
2018-08-09 08:56:35 -05:00
|
|
|
xml.element("SegmentBase", indexRange: fmt["index"]) do
|
|
|
|
xml.element("Initialization", range: fmt["init"])
|
2018-07-16 08:18:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-09 08:56:35 -05:00
|
|
|
xml.element("AdaptationSet", mimeType: "video/mp4", startWithSAP: 1, subsegmentAlignment: true,
|
|
|
|
scanType: "progressive") do
|
2018-07-16 08:18:59 -05:00
|
|
|
video_streams.each do |fmt|
|
2018-08-07 11:40:30 -05:00
|
|
|
codecs = fmt["type"].split("codecs=")[1].strip('"')
|
2018-08-11 14:29:51 -05:00
|
|
|
bandwidth = fmt["bitrate"]
|
2018-07-16 08:18:59 -05:00
|
|
|
itag = fmt["itag"]
|
|
|
|
url = fmt["url"]
|
|
|
|
height, width = fmt["size"].split("x")
|
|
|
|
|
2018-08-09 08:56:35 -05:00
|
|
|
xml.element("Representation", id: itag, codecs: codecs, width: width, height: height,
|
|
|
|
startWithSAP: "1", maxPlayoutRate: "1",
|
|
|
|
bandwidth: bandwidth, frameRate: fmt["fps"]) do
|
2018-07-16 08:18:59 -05:00
|
|
|
xml.element("BaseURL") { xml.text url }
|
2018-08-09 08:56:35 -05:00
|
|
|
xml.element("SegmentBase", indexRange: fmt["index"]) do
|
|
|
|
xml.element("Initialization", range: fmt["init"])
|
2018-07-16 08:18:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
manifest = manifest.gsub(%(<?xml version="1.0" encoding="UTF-8U"?>), %(<?xml version="1.0" encoding="UTF-8"?>))
|
|
|
|
manifest = manifest.gsub(%(<?xml version="1.0" encoding="UTF-8V"?>), %(<?xml version="1.0" encoding="UTF-8"?>))
|
|
|
|
manifest
|
|
|
|
end
|
|
|
|
|
2018-07-27 18:25:58 -05:00
|
|
|
get "/api/manifest/hls_variant/*" do |env|
|
|
|
|
client = make_client(YT_URL)
|
|
|
|
manifest = client.get(env.request.path)
|
|
|
|
|
|
|
|
if manifest.status_code != 200
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = manifest.status_code
|
|
|
|
next
|
2018-07-27 18:25:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
env.response.content_type = "application/x-mpegURL"
|
|
|
|
env.response.headers.add("Access-Control-Allow-Origin", "*")
|
2018-08-04 23:07:38 -05:00
|
|
|
|
2019-03-05 12:56:59 -06:00
|
|
|
host_url = make_host_url(config, Kemal.config)
|
2019-01-19 09:10:52 -06:00
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
manifest = manifest.body
|
|
|
|
manifest.gsub("https://www.youtube.com", host_url)
|
2018-07-27 18:25:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
get "/api/manifest/hls_playlist/*" do |env|
|
|
|
|
client = make_client(YT_URL)
|
|
|
|
manifest = client.get(env.request.path)
|
|
|
|
|
|
|
|
if manifest.status_code != 200
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = manifest.status_code
|
|
|
|
next
|
2018-07-27 18:25:58 -05:00
|
|
|
end
|
|
|
|
|
2019-03-05 12:56:59 -06:00
|
|
|
host_url = make_host_url(config, Kemal.config)
|
2018-07-27 18:25:58 -05:00
|
|
|
|
2018-08-04 23:07:38 -05:00
|
|
|
manifest = manifest.body.gsub("https://www.youtube.com", host_url)
|
|
|
|
manifest = manifest.gsub(/https:\/\/r\d---.{11}\.c\.youtube\.com/, host_url)
|
2018-07-27 18:25:58 -05:00
|
|
|
fvip = manifest.match(/hls_chunk_host\/r(?<fvip>\d)---/).not_nil!["fvip"]
|
|
|
|
manifest = manifest.gsub("seg.ts", "seg.ts/fvip/#{fvip}")
|
|
|
|
|
|
|
|
env.response.content_type = "application/x-mpegURL"
|
|
|
|
env.response.headers.add("Access-Control-Allow-Origin", "*")
|
|
|
|
manifest
|
|
|
|
end
|
|
|
|
|
2019-01-27 20:35:32 -06:00
|
|
|
# YouTube /videoplayback links expire after 6 hours,
|
|
|
|
# so we have a mechanism here to redirect to the latest version
|
|
|
|
get "/latest_version" do |env|
|
2019-02-24 11:04:46 -06:00
|
|
|
if env.params.query["download_widget"]?
|
|
|
|
download_widget = JSON.parse(env.params.query["download_widget"])
|
|
|
|
id = download_widget["id"].as_s
|
|
|
|
itag = download_widget["itag"].as_s
|
|
|
|
title = download_widget["title"].as_s
|
|
|
|
local = "true"
|
|
|
|
end
|
|
|
|
|
|
|
|
id ||= env.params.query["id"]?
|
|
|
|
itag ||= env.params.query["itag"]?
|
2019-01-27 20:35:32 -06:00
|
|
|
|
2019-02-09 12:22:18 -06:00
|
|
|
region = env.params.query["region"]?
|
|
|
|
|
2019-02-24 11:04:46 -06:00
|
|
|
local ||= env.params.query["local"]?
|
2019-01-27 21:20:52 -06:00
|
|
|
local ||= "false"
|
|
|
|
local = local == "true"
|
|
|
|
|
2019-01-27 20:35:32 -06:00
|
|
|
if !id || !itag
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 400
|
|
|
|
next
|
2019-01-27 20:35:32 -06:00
|
|
|
end
|
|
|
|
|
2019-02-09 12:22:18 -06:00
|
|
|
video = fetch_video(id, proxies, region: region)
|
2019-01-27 20:35:32 -06:00
|
|
|
|
|
|
|
fmt_stream = video.fmt_stream(decrypt_function)
|
|
|
|
adaptive_fmts = video.adaptive_fmts(decrypt_function)
|
|
|
|
|
|
|
|
urls = (fmt_stream + adaptive_fmts).select { |fmt| fmt["itag"] == itag }
|
|
|
|
if urls.empty?
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 404
|
|
|
|
next
|
2019-01-27 20:35:32 -06:00
|
|
|
elsif urls.size > 1
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = 409
|
|
|
|
next
|
2019-01-27 20:35:32 -06:00
|
|
|
end
|
|
|
|
|
2019-01-27 21:20:52 -06:00
|
|
|
url = urls[0]["url"]
|
|
|
|
if local
|
|
|
|
url = URI.parse(url).full_path.not_nil!
|
|
|
|
end
|
|
|
|
|
2019-02-24 11:04:46 -06:00
|
|
|
if title
|
|
|
|
url += "&title=#{title}"
|
|
|
|
end
|
|
|
|
|
2019-01-27 21:20:52 -06:00
|
|
|
env.redirect url
|
2019-01-27 20:35:32 -06:00
|
|
|
end
|
|
|
|
|
2018-08-07 13:25:22 -05:00
|
|
|
options "/videoplayback" do |env|
|
2018-08-04 15:30:44 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-08-09 09:43:47 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
|
|
|
|
env.response.headers["Access-Control-Allow-Headers"] = "Content-Type, Range"
|
2018-08-04 15:30:44 -05:00
|
|
|
end
|
|
|
|
|
2018-08-07 11:39:56 -05:00
|
|
|
options "/videoplayback/*" do |env|
|
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-08-09 09:43:47 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
|
|
|
|
env.response.headers["Access-Control-Allow-Headers"] = "Content-Type, Range"
|
2018-08-07 11:39:56 -05:00
|
|
|
end
|
|
|
|
|
2018-08-07 13:18:38 -05:00
|
|
|
options "/api/manifest/dash/id/videoplayback" do |env|
|
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-08-09 09:43:47 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
|
|
|
|
env.response.headers["Access-Control-Allow-Headers"] = "Content-Type, Range"
|
2018-08-07 13:18:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
options "/api/manifest/dash/id/videoplayback/*" do |env|
|
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-08-09 09:43:47 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Methods"] = "GET, OPTIONS"
|
|
|
|
env.response.headers["Access-Control-Allow-Headers"] = "Content-Type, Range"
|
2018-08-07 13:18:38 -05:00
|
|
|
end
|
|
|
|
|
2018-08-07 11:39:56 -05:00
|
|
|
get "/videoplayback/*" do |env|
|
2018-06-06 17:55:51 -05:00
|
|
|
path = env.request.path
|
2018-08-07 11:39:56 -05:00
|
|
|
|
2018-08-07 11:49:14 -05:00
|
|
|
path = path.lchop("/videoplayback/")
|
|
|
|
path = path.rchop("/")
|
2018-07-15 21:53:24 -05:00
|
|
|
|
2018-08-07 11:49:14 -05:00
|
|
|
path = path.gsub(/mime\/\w+\/\w+/) do |mimetype|
|
|
|
|
mimetype = mimetype.split("/")
|
|
|
|
mimetype[0] + "/" + mimetype[1] + "%2F" + mimetype[2]
|
|
|
|
end
|
2018-07-15 21:53:24 -05:00
|
|
|
|
2018-08-07 11:49:14 -05:00
|
|
|
path = path.split("/")
|
2018-06-06 17:55:51 -05:00
|
|
|
|
2018-08-07 11:49:14 -05:00
|
|
|
raw_params = {} of String => Array(String)
|
|
|
|
path.each_slice(2) do |pair|
|
|
|
|
key, value = pair
|
|
|
|
value = URI.unescape(value)
|
2018-06-06 17:55:51 -05:00
|
|
|
|
2018-08-07 11:49:14 -05:00
|
|
|
if raw_params[key]?
|
|
|
|
raw_params[key] << value
|
|
|
|
else
|
|
|
|
raw_params[key] = [value]
|
2018-06-06 17:55:51 -05:00
|
|
|
end
|
2018-08-07 11:49:14 -05:00
|
|
|
end
|
2018-06-06 17:55:51 -05:00
|
|
|
|
2018-08-07 11:49:14 -05:00
|
|
|
query_params = HTTP::Params.new(raw_params)
|
2018-08-07 11:39:56 -05:00
|
|
|
|
2018-08-11 14:29:51 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-08-07 11:39:56 -05:00
|
|
|
env.redirect "/videoplayback?#{query_params}"
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/videoplayback" do |env|
|
2018-08-07 11:49:14 -05:00
|
|
|
query_params = env.params.query
|
2018-04-15 20:47:37 -05:00
|
|
|
|
2019-02-27 08:16:58 -06:00
|
|
|
fvip = query_params["fvip"]? || "3"
|
2019-03-11 14:07:55 -05:00
|
|
|
mns = query_params["mn"].split(",")
|
|
|
|
|
2019-03-27 14:59:53 -05:00
|
|
|
if query_params["region"]?
|
|
|
|
region = query_params["region"]
|
|
|
|
query_params.delete("region")
|
|
|
|
end
|
|
|
|
|
2019-03-11 13:14:30 -05:00
|
|
|
if query_params["host"]? && !query_params["host"].empty?
|
|
|
|
host = "https://#{query_params["host"]}"
|
2019-03-11 13:32:46 -05:00
|
|
|
query_params.delete("host")
|
2019-03-11 13:14:30 -05:00
|
|
|
else
|
2019-03-11 14:07:55 -05:00
|
|
|
host = "https://r#{fvip}---#{mns.pop}.googlevideo.com"
|
2019-03-11 13:14:30 -05:00
|
|
|
end
|
|
|
|
|
2018-04-15 20:47:37 -05:00
|
|
|
url = "/videoplayback?#{query_params.to_s}"
|
|
|
|
|
2019-03-11 11:43:48 -05:00
|
|
|
headers = HTTP::Headers.new
|
2019-03-11 16:17:40 -05:00
|
|
|
{"Accept", "Accept-Encoding", "Connection", "Range"}.each do |header|
|
2019-03-11 11:43:48 -05:00
|
|
|
if env.request.headers[header]?
|
|
|
|
headers[header] = env.request.headers[header]
|
|
|
|
end
|
|
|
|
end
|
2019-01-24 13:52:33 -06:00
|
|
|
|
|
|
|
response = HTTP::Client::Response.new(403)
|
2019-03-11 11:43:48 -05:00
|
|
|
5.times do
|
2019-01-24 13:52:33 -06:00
|
|
|
begin
|
|
|
|
client = make_client(URI.parse(host), proxies, region)
|
|
|
|
response = client.head(url, headers)
|
|
|
|
break
|
2019-03-11 11:43:48 -05:00
|
|
|
rescue Socket::Addrinfo::Error
|
2019-03-11 14:07:55 -05:00
|
|
|
if !mns.empty?
|
|
|
|
mn = mns.pop
|
2019-03-11 11:43:48 -05:00
|
|
|
end
|
|
|
|
fvip = "3"
|
2019-03-11 14:07:55 -05:00
|
|
|
|
2019-03-11 11:43:48 -05:00
|
|
|
host = "https://r#{fvip}---#{mn}.googlevideo.com"
|
2019-01-24 13:52:33 -06:00
|
|
|
rescue ex
|
|
|
|
end
|
|
|
|
end
|
2018-09-30 20:26:28 -05:00
|
|
|
|
2018-11-20 10:07:50 -06:00
|
|
|
if response.headers["Location"]?
|
|
|
|
url = URI.parse(response.headers["Location"])
|
2019-03-27 15:25:08 -05:00
|
|
|
host = url.host
|
2018-11-20 10:07:50 -06:00
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-09-30 20:26:28 -05:00
|
|
|
|
2018-11-20 10:07:50 -06:00
|
|
|
url = url.full_path
|
2019-03-27 15:25:08 -05:00
|
|
|
url += "&host=#{host}"
|
2019-03-27 14:59:53 -05:00
|
|
|
|
2018-11-20 10:07:50 -06:00
|
|
|
if region
|
|
|
|
url += "®ion=#{region}"
|
2018-10-01 19:01:44 -05:00
|
|
|
end
|
2018-09-30 20:26:28 -05:00
|
|
|
|
2018-11-20 10:07:50 -06:00
|
|
|
next env.redirect url
|
2018-09-30 20:26:28 -05:00
|
|
|
end
|
|
|
|
|
2018-11-20 10:07:50 -06:00
|
|
|
if response.status_code >= 400
|
2019-03-23 10:24:30 -05:00
|
|
|
env.response.status_code = response.status_code
|
|
|
|
next
|
2018-08-25 17:24:07 -05:00
|
|
|
end
|
|
|
|
|
2018-11-20 10:07:50 -06:00
|
|
|
client = make_client(URI.parse(host), proxies, region)
|
2018-04-15 20:47:37 -05:00
|
|
|
client.get(url, headers) do |response|
|
2018-08-25 17:24:07 -05:00
|
|
|
env.response.status_code = response.status_code
|
2018-04-15 20:47:37 -05:00
|
|
|
|
2018-08-25 17:24:07 -05:00
|
|
|
response.headers.each do |key, value|
|
|
|
|
env.response.headers[key] = value
|
|
|
|
end
|
2018-04-15 20:47:37 -05:00
|
|
|
|
2019-03-27 14:59:53 -05:00
|
|
|
if response.headers["Location"]?
|
|
|
|
url = URI.parse(response.headers["Location"])
|
2019-03-27 15:25:08 -05:00
|
|
|
host = url.host
|
2019-03-27 14:59:53 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
|
|
|
|
|
|
|
url = url.full_path
|
2019-03-27 15:25:08 -05:00
|
|
|
url += "&host=#{host}"
|
2019-03-27 14:59:53 -05:00
|
|
|
|
|
|
|
if region
|
|
|
|
url += "®ion=#{region}"
|
|
|
|
end
|
|
|
|
|
|
|
|
next env.redirect url
|
|
|
|
end
|
|
|
|
|
2019-03-25 16:32:11 -05:00
|
|
|
if title = query_params["title"]?
|
|
|
|
# https://blog.fastmail.com/2011/06/24/download-non-english-filenames/
|
|
|
|
env.response.headers["Content-Disposition"] = "attachment; filename=\"#{URI.escape(title)}\"; filename*=UTF-8''#{URI.escape(title)}"
|
|
|
|
end
|
|
|
|
|
2018-08-25 17:24:07 -05:00
|
|
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
2018-04-16 19:31:57 -05:00
|
|
|
|
2018-08-25 17:24:07 -05:00
|
|
|
begin
|
|
|
|
chunk_size = 4096
|
|
|
|
size = 1
|
|
|
|
while size > 0
|
|
|
|
size = IO.copy(response.body_io, env.response.output, chunk_size)
|
2018-09-14 21:24:28 -05:00
|
|
|
env.response.flush
|
|
|
|
Fiber.yield
|
|
|
|
end
|
|
|
|
rescue ex
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-25 09:23:42 -05:00
|
|
|
# We need this so the below route works as expected
|
2018-09-17 18:39:28 -05:00
|
|
|
get "/ggpht*" do |env|
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/ggpht/*" do |env|
|
|
|
|
host = "https://yt3.ggpht.com"
|
|
|
|
client = make_client(URI.parse(host))
|
|
|
|
url = env.request.path.lchop("/ggpht")
|
|
|
|
|
2019-03-11 11:43:48 -05:00
|
|
|
headers = HTTP::Headers.new
|
|
|
|
{"Range", "Accept", "Accept-Encoding"}.each do |header|
|
|
|
|
if env.request.headers[header]?
|
|
|
|
headers[header] = env.request.headers[header]
|
|
|
|
end
|
|
|
|
end
|
2018-09-17 18:39:28 -05:00
|
|
|
|
|
|
|
client.get(url, headers) do |response|
|
|
|
|
env.response.status_code = response.status_code
|
|
|
|
response.headers.each do |key, value|
|
|
|
|
env.response.headers[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
if response.status_code == 304
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
chunk_size = 4096
|
|
|
|
size = 1
|
|
|
|
if response.headers.includes_word?("Content-Encoding", "gzip")
|
|
|
|
Gzip::Writer.open(env.response) do |deflate|
|
|
|
|
until size == 0
|
|
|
|
size = IO.copy(response.body_io, deflate)
|
|
|
|
env.response.flush
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif response.headers.includes_word?("Content-Encoding", "deflate")
|
|
|
|
Flate::Writer.open(env.response) do |deflate|
|
|
|
|
until size == 0
|
|
|
|
size = IO.copy(response.body_io, deflate)
|
|
|
|
env.response.flush
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
until size == 0
|
|
|
|
size = IO.copy(response.body_io, env.response, chunk_size)
|
|
|
|
env.response.flush
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-14 21:24:28 -05:00
|
|
|
get "/vi/:id/:name" do |env|
|
|
|
|
id = env.params.url["id"]
|
|
|
|
name = env.params.url["name"]
|
|
|
|
|
|
|
|
host = "https://i.ytimg.com"
|
|
|
|
client = make_client(URI.parse(host))
|
|
|
|
|
|
|
|
if name == "maxres.jpg"
|
2019-03-08 14:42:37 -06:00
|
|
|
build_thumbnails(id, config, Kemal.config).each do |thumb|
|
2018-09-14 21:24:28 -05:00
|
|
|
if client.head("/vi/#{id}/#{thumb[:url]}.jpg").status_code == 200
|
|
|
|
name = thumb[:url] + ".jpg"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
url = "/vi/#{id}/#{name}"
|
|
|
|
|
2019-03-11 11:43:48 -05:00
|
|
|
headers = HTTP::Headers.new
|
|
|
|
{"Range", "Accept", "Accept-Encoding"}.each do |header|
|
|
|
|
if env.request.headers[header]?
|
|
|
|
headers[header] = env.request.headers[header]
|
|
|
|
end
|
|
|
|
end
|
2018-09-14 21:24:28 -05:00
|
|
|
|
2018-09-17 09:38:52 -05:00
|
|
|
client.get(url, headers) do |response|
|
|
|
|
env.response.status_code = response.status_code
|
2018-09-14 21:24:28 -05:00
|
|
|
response.headers.each do |key, value|
|
|
|
|
env.response.headers[key] = value
|
|
|
|
end
|
|
|
|
|
2018-09-17 09:38:52 -05:00
|
|
|
if response.status_code == 304
|
|
|
|
break
|
|
|
|
end
|
2018-09-14 21:24:28 -05:00
|
|
|
|
2018-09-17 09:38:52 -05:00
|
|
|
chunk_size = 4096
|
2018-09-17 14:48:02 -05:00
|
|
|
size = 1
|
2018-09-17 09:38:52 -05:00
|
|
|
if response.headers.includes_word?("Content-Encoding", "gzip")
|
|
|
|
Gzip::Writer.open(env.response) do |deflate|
|
2018-09-17 14:48:02 -05:00
|
|
|
until size == 0
|
2018-09-17 09:38:52 -05:00
|
|
|
size = IO.copy(response.body_io, deflate)
|
|
|
|
env.response.flush
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif response.headers.includes_word?("Content-Encoding", "deflate")
|
|
|
|
Flate::Writer.open(env.response) do |deflate|
|
2018-09-17 14:48:02 -05:00
|
|
|
until size == 0
|
2018-09-17 09:38:52 -05:00
|
|
|
size = IO.copy(response.body_io, deflate)
|
|
|
|
env.response.flush
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2018-09-17 14:48:02 -05:00
|
|
|
until size == 0
|
2018-09-17 09:38:52 -05:00
|
|
|
size = IO.copy(response.body_io, env.response, chunk_size)
|
2018-08-25 17:24:07 -05:00
|
|
|
env.response.flush
|
2018-04-15 20:47:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-10 09:15:23 -06:00
|
|
|
error 404 do |env|
|
2018-10-31 11:07:58 -05:00
|
|
|
env.response.content_type = "application/json"
|
2018-10-06 22:19:36 -05:00
|
|
|
|
2018-10-31 11:07:58 -05:00
|
|
|
error_message = "404 Not Found"
|
|
|
|
{"error" => error_message}.to_json
|
2017-12-30 15:21:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
error 500 do |env|
|
2018-10-31 11:07:58 -05:00
|
|
|
env.response.content_type = "application/json"
|
|
|
|
|
|
|
|
error_message = "500 Server Error"
|
|
|
|
{"error" => error_message}.to_json
|
2017-12-30 15:21:43 -06:00
|
|
|
end
|
|
|
|
|
2018-03-16 19:58:33 -05:00
|
|
|
# Add redirect if SSL is enabled
|
|
|
|
if Kemal.config.ssl
|
2018-03-09 13:22:04 -06:00
|
|
|
spawn do
|
2018-06-19 16:20:08 -05:00
|
|
|
server = HTTP::Server.new do |context|
|
2018-03-09 14:13:26 -06:00
|
|
|
redirect_url = "https://#{context.request.host}#{context.request.path}"
|
|
|
|
if context.request.query
|
|
|
|
redirect_url += "?#{context.request.query}"
|
|
|
|
end
|
2018-03-11 10:24:12 -05:00
|
|
|
context.response.headers.add("Location", redirect_url)
|
2018-03-09 14:11:30 -06:00
|
|
|
context.response.status_code = 301
|
2018-03-09 13:22:04 -06:00
|
|
|
end
|
|
|
|
|
2018-06-19 16:20:08 -05:00
|
|
|
server.bind_tcp "0.0.0.0", 80
|
2018-03-09 13:22:04 -06:00
|
|
|
server.listen
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-30 18:42:45 -05:00
|
|
|
Kemal.config.powered_by_header = false
|
2019-03-25 09:31:51 -05:00
|
|
|
add_handler FilteredCompressHandler.new
|
2019-02-02 22:48:47 -06:00
|
|
|
add_handler APIHandler.new
|
2019-03-25 09:31:51 -05:00
|
|
|
add_handler DenyFrame.new
|
|
|
|
add_context_storage_type(User)
|
|
|
|
add_context_storage_type(Preferences)
|
2017-11-23 01:48:55 -06:00
|
|
|
|
2019-01-23 14:15:19 -06:00
|
|
|
Kemal.config.logger = logger
|
2017-11-23 01:48:55 -06:00
|
|
|
Kemal.run
|