Refactor name to ucid conversion

pull/186/head
Omar Roth 6 years ago
parent 51d00b16c3
commit a68924f0ce

@ -1399,43 +1399,17 @@ get "/feed/subscriptions" do |env|
end end
get "/feed/channel/:ucid" do |env| get "/feed/channel/:ucid" do |env|
env.response.content_type = "text/xml"
ucid = env.params.url["ucid"] ucid = env.params.url["ucid"]
client = make_client(YT_URL) begin
author, ucid, auto_generated = get_about_info(ucid)
if !ucid.match(/UC[a-zA-Z0-9_-]{22}/) rescue ex
rss = client.get("/feeds/videos.xml?user=#{ucid}") error_message = "User does not exist"
rss = XML.parse_html(rss.body) halt env, status_code: 404, response: error_message
ucid = rss.xpath_node("//feed/channelid")
if !ucid
error_message = "User does not exist."
halt env, status_code: 404, response: error_message
end
ucid = ucid.content
author = rss.xpath_node("//author/name").not_nil!.content
next env.redirect "/feed/channel/#{ucid}"
else
rss = client.get("/feeds/videos.xml?channel_id=#{ucid}")
rss = XML.parse_html(rss.body)
ucid = rss.xpath_node("//feed/channelid")
if !ucid
error_message = "User does not exist."
next templated "error"
end
ucid = ucid.content
author = rss.xpath_node("//author/name").not_nil!.content
end end
# Auto-generated channels client = make_client(YT_URL)
# https://support.google.com/youtube/answer/2579942
if author.ends_with?(" - Topic") ||
{"Popular on YouTube", "Music", "Sports", "Gaming"}.includes? author
auto_generated = true
end
page = 1 page = 1
@ -1459,8 +1433,6 @@ get "/feed/channel/:ucid" do |env|
end end
end end
channel = get_channel(ucid, client, PG_DB, pull_all_videos: false)
host_url = make_host_url(Kemal.config.ssl || CONFIG.https_only, env.request.headers["Host"]?) host_url = make_host_url(Kemal.config.ssl || CONFIG.https_only, env.request.headers["Host"]?)
path = env.request.path path = env.request.path
@ -1470,11 +1442,11 @@ get "/feed/channel/:ucid" do |env|
xml.element("link", rel: "self", href: "#{host_url}#{path}") xml.element("link", rel: "self", href: "#{host_url}#{path}")
xml.element("id") { xml.text "yt:channel:#{ucid}" } xml.element("id") { xml.text "yt:channel:#{ucid}" }
xml.element("yt:channelId") { xml.text ucid } xml.element("yt:channelId") { xml.text ucid }
xml.element("title") { xml.text channel.author } xml.element("title") { xml.text author }
xml.element("link", rel: "alternate", href: "#{host_url}/channel/#{ucid}") xml.element("link", rel: "alternate", href: "#{host_url}/channel/#{ucid}")
xml.element("author") do xml.element("author") do
xml.element("name") { xml.text channel.author } xml.element("name") { xml.text author }
xml.element("uri") { xml.text "#{host_url}/channel/#{ucid}" } xml.element("uri") { xml.text "#{host_url}/channel/#{ucid}" }
end end
@ -1513,7 +1485,6 @@ get "/feed/channel/:ucid" do |env|
end end
end end
env.response.content_type = "text/xml"
feed feed
end end
@ -1691,40 +1662,11 @@ get "/channel/:ucid" do |env|
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1
client = make_client(YT_URL) begin
author, ucid, auto_generated = get_about_info(ucid)
if !ucid.match(/UC[a-zA-Z0-9_-]{22}/) rescue ex
rss = client.get("/feeds/videos.xml?user=#{ucid}") error_message = "User does not exist"
rss = XML.parse_html(rss.body) next templated "error"
ucid = rss.xpath_node("//feed/channelid")
if !ucid
error_message = "User does not exist."
next templated "error"
end
ucid = ucid.content
author = rss.xpath_node("//author/name").not_nil!.content
next env.redirect "/channel/#{ucid}"
else
rss = client.get("/feeds/videos.xml?channel_id=#{ucid}")
rss = XML.parse_html(rss.body)
ucid = rss.xpath_node("//feed/channelid")
if !ucid
error_message = "User does not exist."
next templated "error"
end
ucid = ucid.content
author = rss.xpath_node("//author/name").not_nil!.content
end
# Auto-generated channels
# https://support.google.com/youtube/answer/2579942
if author.ends_with?(" - Topic") ||
{"Popular on YouTube", "Music", "Sports", "Gaming"}.includes? author
auto_generated = true
end end
if !auto_generated if !auto_generated
@ -1735,6 +1677,8 @@ get "/channel/:ucid" do |env|
end end
end end
client = make_client(YT_URL)
videos = [] of SearchVideo videos = [] of SearchVideo
2.times do |i| 2.times do |i|
url = produce_channel_videos_url(ucid, page * 2 + (i - 1), auto_generated: auto_generated) url = produce_channel_videos_url(ucid, page * 2 + (i - 1), auto_generated: auto_generated)
@ -2175,8 +2119,8 @@ get "/api/v1/videos/:id" do |env|
video = get_video(id, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
env.response.content_type = "application/json" env.response.content_type = "application/json"
response = {"error" => ex.message}.to_json error_message = {"error" => ex.message}.to_json
halt env, status_code: 500, response: response halt env, status_code: 500, response: error_message
end end
fmt_stream = video.fmt_stream(decrypt_function) fmt_stream = video.fmt_stream(decrypt_function)
@ -2425,42 +2369,20 @@ get "/api/v1/top" do |env|
end end
get "/api/v1/channels/:ucid" do |env| get "/api/v1/channels/:ucid" do |env|
ucid = env.params.url["ucid"] env.response.content_type = "application/json"
client = make_client(YT_URL)
if !ucid.match(/UC[a-zA-Z0-9_-]{22}/)
rss = client.get("/feeds/videos.xml?user=#{ucid}")
rss = XML.parse_html(rss.body)
ucid = rss.xpath_node("//feed/channelid")
if !ucid
env.response.content_type = "application/json"
next {"error" => "User does not exist"}.to_json
end
ucid = ucid.content ucid = env.params.url["ucid"]
author = rss.xpath_node("//author/name").not_nil!.content
next env.redirect "/api/v1/channels/#{ucid}"
else
rss = client.get("/feeds/videos.xml?channel_id=#{ucid}")
rss = XML.parse_html(rss.body)
ucid = rss.xpath_node("//feed/channelid") begin
if !ucid author, ucid, auto_generated = get_about_info(ucid)
error_message = "User does not exist." rescue ex
next templated "error" puts ex.message
end
ucid = ucid.content error_message = {"error" => "User does not exist"}.to_json
author = rss.xpath_node("//author/name").not_nil!.content halt env, status_code: 404, response: error_message
end end
# Auto-generated channels client = make_client(YT_URL)
# https://support.google.com/youtube/answer/2579942
if author.ends_with?(" - Topic") ||
{"Popular on YouTube", "Music", "Sports", "Gaming"}.includes? author
auto_generated = true
end
page = 1 page = 1
@ -2601,51 +2523,25 @@ get "/api/v1/channels/:ucid" do |env|
end end
end end
env.response.content_type = "application/json"
channel_info channel_info
end end
["/api/v1/channels/:ucid/videos", "/api/v1/channels/videos/:ucid"].each do |route| ["/api/v1/channels/:ucid/videos", "/api/v1/channels/videos/:ucid"].each do |route|
get route do |env| get route do |env|
env.response.content_type = "application/json"
ucid = env.params.url["ucid"] ucid = env.params.url["ucid"]
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
page ||= 1 page ||= 1
client = make_client(YT_URL) begin
author, ucid, auto_generated = get_about_info(ucid)
if !ucid.match(/UC[a-zA-Z0-9_-]{22}/) rescue ex
rss = client.get("/feeds/videos.xml?user=#{ucid}") error_message = {"error" => "User does not exist"}.to_json
rss = XML.parse_html(rss.body) halt env, status_code: 404, response: error_message
ucid = rss.xpath_node("//feed/channelid")
if !ucid
env.response.content_type = "application/json"
next {"error" => "User does not exist"}.to_json
end
ucid = ucid.content
author = rss.xpath_node("//author/name").not_nil!.content
next env.redirect "/feed/channel/#{ucid}"
else
rss = client.get("/feeds/videos.xml?channel_id=#{ucid}")
rss = XML.parse_html(rss.body)
ucid = rss.xpath_node("//feed/channelid")
if !ucid
error_message = "User does not exist."
next templated "error"
end
ucid = ucid.content
author = rss.xpath_node("//author/name").not_nil!.content
end end
# Auto-generated channels client = make_client(YT_URL)
# https://support.google.com/youtube/answer/2579942
if author.ends_with?(" - Topic") ||
{"Popular on YouTube", "Music", "Sports", "Gaming"}.includes? author
auto_generated = true
end
videos = [] of SearchVideo videos = [] of SearchVideo
2.times do |i| 2.times do |i|
@ -2700,7 +2596,6 @@ end
end end
end end
env.response.content_type = "application/json"
result result
end end
end end
@ -2826,6 +2721,7 @@ get "/api/v1/search" do |env|
end end
get "/api/v1/playlists/:plid" do |env| get "/api/v1/playlists/:plid" do |env|
env.response.content_type = "application/json"
plid = env.params.url["plid"] plid = env.params.url["plid"]
page = env.params.query["page"]?.try &.to_i? page = env.params.query["page"]?.try &.to_i?
@ -2834,9 +2730,8 @@ get "/api/v1/playlists/:plid" do |env|
begin begin
videos = extract_playlist(plid, page) videos = extract_playlist(plid, page)
rescue ex rescue ex
env.response.content_type = "application/json" error_message = {"error" => "Playlist is empty"}.to_json
response = {"error" => "Playlist is empty"}.to_json halt env, status_code: 404, response: error_message
halt env, status_code: 404, response: response
end end
playlist = fetch_playlist(plid) playlist = fetch_playlist(plid)
@ -2881,7 +2776,6 @@ get "/api/v1/playlists/:plid" do |env|
end end
end end
env.response.content_type = "application/json"
response response
end end

@ -176,3 +176,32 @@ def produce_channel_videos_url(ucid, page = 1, auto_generated = nil)
return url return url
end end
def get_about_info(ucid)
client = make_client(YT_URL)
about = client.get("/user/#{ucid}/about?disable_polymer=1")
about = XML.parse_html(about.body)
if !about.xpath_node(%q(//span[@class="qualified-channel-title-text"]/a))
about = client.get("/channel/#{ucid}/about?disable_polymer=1")
about = XML.parse_html(about.body)
end
if !about.xpath_node(%q(//span[@class="qualified-channel-title-text"]/a))
raise "User does not exist."
end
author = about.xpath_node(%q(//span[@class="qualified-channel-title-text"]/a)).not_nil!.content
ucid = about.xpath_node(%q(//link[@rel="canonical"])).not_nil!["href"].split("/")[-1]
# Auto-generated channels
# https://support.google.com/youtube/answer/2579942
auto_generated = false
if about.xpath_node(%q(//ul[@class="about-custom-links"]/li/a[@title="Auto-generated by YouTube"])) ||
about.xpath_node(%q(//span[@class="qualified-channel-title-badge"]/span[@title="Auto-generated by YouTube"]))
auto_generated = true
end
return {author, ucid, auto_generated}
end

Loading…
Cancel
Save