Skip to content

Commit

Permalink
Rss autodiscovery (#134)
Browse files Browse the repository at this point in the history
* Create helm chart for easier deployment in k8s

* Add RSS autodiscovery

* RSS feed for each channel
  • Loading branch information
dsezerpy committed Apr 5, 2024
1 parent 327908c commit 7cb2ee0
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
11 changes: 10 additions & 1 deletion LightTube/Contexts/BaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class BaseContext
public string Title;
public bool IsMobile;
public List<IHtmlContent> HeadTags = new();
public List<IHtmlContent> rssElement = new();
public List<IHtmlContent> EndTags = new();
public bool GuideHidden = false;
public HttpContext Context;
Expand Down Expand Up @@ -38,7 +39,15 @@ public void AddStylesheet(string href)
stylesheet.Attributes.Add("href", href + "?v=" + Utils.GetVersion());
HeadTags.Add(stylesheet);
}

public void AddRSSUrl(string href)
{
TagBuilder rss = new("link");
rss.Attributes.Add("rel", "alternate");
rss.Attributes.Add("type", "application/rss+xml");
rss.Attributes.Add("title", "RSS");
rss.Attributes.Add("href", href);
rssElement.Add(rss);
}
public void AddMeta(string property, string content)
{
TagBuilder stylesheet = new("meta");
Expand Down
2 changes: 2 additions & 0 deletions LightTube/Contexts/ChannelContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public ChannelContext(HttpContext context, ChannelTabs tab, InnerTubeChannelResp
AddMeta("og:url", $"{context.Request.Scheme}:https://{context.Request.Host}/{context.Request.Path}{context.Request.QueryString}");
AddMeta("og:image", channel.Header?.Avatars.Last().Url.ToString() ?? "");
AddMeta("twitter:card", channel.Header?.Avatars.Last().Url.ToString() ?? "");
AddRSSUrl(context.Request.Scheme + ":https://" + context.Request.Host + "/feed/" + Id + "/rss.xml");

if (channel.Contents.Any(x => x is ChannelVideoPlayerRenderer || x is ItemSectionRenderer isr && isr.Contents.Any(y => y is ChannelVideoPlayerRenderer)))
{
Expand Down Expand Up @@ -85,6 +86,7 @@ public ChannelContext(HttpContext context, ChannelTabs tab, InnerTubeChannelResp
AddMeta("og:url", $"{context.Request.Scheme}:https://{context.Request.Host}/{context.Request.Path}{context.Request.QueryString}");
AddMeta("og:image", channel.Header?.Avatars.Last().Url.ToString() ?? "");
AddMeta("twitter:card", channel.Header?.Avatars.Last().Url.ToString() ?? "");
AddRSSUrl(context.Request.Scheme + ":https://" + context.Request.Host + "/feed/" + Id + "/rss.xml");
}

public ChannelContext(HttpContext context, DatabaseUser? channel, string id) : base(context)
Expand Down
1 change: 1 addition & 0 deletions LightTube/Contexts/HomepageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class HomepageContext : BaseContext
public FeedVideo[] Videos;
public HomepageContext(HttpContext context) : base(context)
{
AddRSSUrl(context.Request.Scheme + ":https://" + context.Request.Host + "/feed/rss.xml");
if (User != null)
{
Videos = Task.Run(async () =>
Expand Down
72 changes: 72 additions & 0 deletions LightTube/Controllers/FeedController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,78 @@ public FeedController(InnerTube.InnerTube youtube)
_youtube = youtube;
}

[Route("channel/{c}/rss.xml")]
[HttpGet]
public async Task<IActionResult> ChannelFeed(string c)
{
ChannelFeed ytchannel = await YoutubeRSS.GetChannelFeed(c);
try
{
XmlDocument document = new();
XmlElement rss = document.CreateElement("rss");
rss.SetAttribute("version", "2.0");

XmlElement channel = document.CreateElement("channel");

XmlElement title = document.CreateElement("title");
title.InnerText = "LightTube channnel RSS feed for " + ytchannel.Name;
channel.AppendChild(title);

XmlElement description = document.CreateElement("description");
description.InnerText = $"LightTube channnel RSS feed for {ytchannel.Name}";
channel.AppendChild(description);

foreach (FeedVideo video in ytchannel.Videos.Take(15))
{
XmlElement item = document.CreateElement("item");

XmlElement id = document.CreateElement("id");
id.InnerText = $"id:video:{video.Id}";
item.AppendChild(id);

XmlElement vtitle = document.CreateElement("title");
vtitle.InnerText = video.Title;
item.AppendChild(vtitle);

XmlElement vdescription = document.CreateElement("description");
vdescription.InnerText = video.Description;
item.AppendChild(vdescription);

XmlElement link = document.CreateElement("link");
link.InnerText = $"https://{Request.Host}/watch?v={video.Id}";
item.AppendChild(link);

XmlElement published = document.CreateElement("pubDate");
published.InnerText = video.PublishedDate.ToString("R");
item.AppendChild(published);

XmlElement author = document.CreateElement("author");

XmlElement name = document.CreateElement("name");
name.InnerText = video.ChannelName;
author.AppendChild(name);

XmlElement uri = document.CreateElement("uri");
uri.InnerText = $"https://{Request.Host}/channel/{video.ChannelId}";
author.AppendChild(uri);

item.AppendChild(author);
channel.AppendChild(item);
}

rss.AppendChild(channel);

document.AppendChild(rss);
return File(Encoding.UTF8.GetBytes(document.OuterXml), "application/xml");

}
catch (Exception)
{

throw;
}
}

[Route("subscriptions")]
public async Task<IActionResult> Subscription()
{
Expand Down
4 changes: 4 additions & 0 deletions LightTube/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<link rel="stylesheet" href="~/css/[email protected]()">
<link rel="stylesheet" href="~/css/[email protected]()">
<link rel="stylesheet" href="~/fonts/roboto/roboto.css">
@foreach (IHtmlContent item in Model.rssElement)
{
@item
}
@if (Configuration.GetVariable("LIGHTTUBE_CUSTOM_CSS_PATH") != null)
{
<link rel="stylesheet" href="/css/custom.css">
Expand Down

0 comments on commit 7cb2ee0

Please sign in to comment.