summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOmar Roth <omarroth@hotmail.com>2019-02-19 17:00:06 -0600
committerOmar Roth <omarroth@hotmail.com>2019-02-19 17:00:06 -0600
commita210327318548f2ef6d14b3a331ec916c6aa2bf2 (patch)
tree5a1ad648d4eff9ae1570bb216336bb96e3f4d96f
parent58fb74179b4d4f01041037ce5770d819f553c2ee (diff)
downloadinvidious-a210327318548f2ef6d14b3a331ec916c6aa2bf2.tar.gz
invidious-a210327318548f2ef6d14b3a331ec916c6aa2bf2.tar.bz2
invidious-a210327318548f2ef6d14b3a331ec916c6aa2bf2.zip
Add `/api/v1/channels/latest/:ucid`
-rw-r--r--src/invidious.cr50
-rw-r--r--src/invidious/channels.cr18
2 files changed, 68 insertions, 0 deletions
diff --git a/src/invidious.cr b/src/invidious.cr
index fa9fcacf..587929ff 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -3129,6 +3129,56 @@ end
end
end
+["/api/v1/channels/:ucid/latest", "/api/v1/channels/latest/:ucid"].each do |route|
+ get route do |env|
+ 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
+ halt env, status_code: 500, response: error_message
+ 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
+ generate_thumbnails(json, video.id)
+ end
+
+ json.field "description", video.description
+ json.field "descriptionHtml", video.description_html
+
+ json.field "viewCount", video.views
+ json.field "published", video.published.to_unix
+ json.field "publishedText", "#{recode_date(video.published)} ago"
+ 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
+
+ if env.params.query["pretty"]? && env.params.query["pretty"] == "1"
+ JSON.parse(response).to_pretty_json
+ else
+ response
+ end
+ end
+end
+
get "/api/v1/channels/search/:ucid" do |env|
locale = LOCALES[env.get("locale").as(String)]?
diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr
index b6692919..d852e517 100644
--- a/src/invidious/channels.cr
+++ b/src/invidious/channels.cr
@@ -460,3 +460,21 @@ def get_60_videos(ucid, page, auto_generated, sort_by = "newest")
return videos, count
end
+
+def get_latest_videos(ucid)
+ client = make_client(YT_URL)
+ videos = [] of SearchVideo
+
+ url = produce_channel_videos_url(ucid, 0)
+ response = client.get(url)
+ json = JSON.parse(response.body)
+
+ if json["content_html"]? && !json["content_html"].as_s.empty?
+ document = XML.parse_html(json["content_html"].as_s)
+ nodeset = document.xpath_nodes(%q(//li[contains(@class, "feed-item-container")]))
+
+ videos = extract_videos(nodeset, ucid)
+ end
+
+ return videos
+end