rss output for recent published recitations: initial codes
This commit is contained in:
parent
3855c0a259
commit
ed09bc5d7e
@ -8,6 +8,7 @@ using RMuseum.Models.GanjoorAudio.ViewModels;
|
||||
using RMuseum.Models.UploadSession;
|
||||
using RMuseum.Models.UploadSession.ViewModels;
|
||||
using RMuseum.Services;
|
||||
using RMuseum.Services.Implementation;
|
||||
using RSecurityBackend.Models.Generic;
|
||||
using RSecurityBackend.Services;
|
||||
using System;
|
||||
@ -46,6 +47,31 @@ namespace RMuseum.Controllers
|
||||
return Ok(res.Result.Items);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// create an RSS file from recent published recitations
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("published/rss/{count}")]
|
||||
[AllowAnonymous]
|
||||
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(FileStreamResult))]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
|
||||
public async Task<IActionResult> GetRssFeed(int count = 200)
|
||||
{
|
||||
var res = await _audioService.GetPublishedRecitations(new PagingParameterModel() { PageNumber = 1, PageSize = count}, "");
|
||||
if (!string.IsNullOrEmpty(res.ExceptionString))
|
||||
return BadRequest(res.ExceptionString);
|
||||
|
||||
string rss = await RecitationsRssBuilder.Build(res.Result.Items);
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream);
|
||||
writer.Write(rss);
|
||||
writer.Flush();
|
||||
stream.Position = 0;
|
||||
|
||||
return new FileStreamResult(stream, "application/rss+xml");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets audio narrations, user must have recitation::moderate permission to be able to see all users narrations
|
||||
/// </summary>
|
||||
@ -131,6 +157,8 @@ namespace RMuseum.Controllers
|
||||
return new FileStreamResult(new FileStream(narration.Result.LocalMp3FilePath, FileMode.Open, FileAccess.Read), "audio/mpeg");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// get the corresponding xml file contemnts (xml) for the narration
|
||||
/// </summary>
|
||||
|
||||
@ -19,6 +19,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
|
||||
<PackageReference Include="MySql.Data" Version="8.0.21" />
|
||||
<PackageReference Include="NAudio" Version="1.10.0" />
|
||||
<PackageReference Include="SSH.NET" Version="2020.0.0-beta1" />
|
||||
|
||||
@ -733,6 +733,13 @@
|
||||
<param name="searchTerm"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.RecitationController.GetRssFeed(System.Int32)">
|
||||
<summary>
|
||||
create an RSS file from recent published recitations
|
||||
</summary>
|
||||
<param name="count"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Controllers.RecitationController.Get(RSecurityBackend.Models.Generic.PagingParameterModel,System.Boolean,RMuseum.Models.GanjoorAudio.AudioReviewStatus,System.String)">
|
||||
<summary>
|
||||
Gets audio narrations, user must have recitation::moderate permission to be able to see all users narrations
|
||||
@ -5273,6 +5280,18 @@
|
||||
<param name="configuration"></param>
|
||||
<param name="simpleImageStorage"></param>
|
||||
</member>
|
||||
<member name="T:RMuseum.Services.Implementation.RecitationsRssBuilder">
|
||||
<summary>
|
||||
creates an RSS feed from recitations
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.RecitationsRssBuilder.Build(RMuseum.Models.GanjoorAudio.ViewModels.PublicRecitationViewModel[])">
|
||||
<summary>
|
||||
build rss
|
||||
</summary>
|
||||
<param name="recitations"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:RMuseum.Services.Implementation.RNotificationService">
|
||||
<summary>
|
||||
Internal messaging system implementation
|
||||
|
||||
82
RMuseum/Services/Implementation/RecitationsRssBuilder.cs
Normal file
82
RMuseum/Services/Implementation/RecitationsRssBuilder.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using Microsoft.SyndicationFeed;
|
||||
using Microsoft.SyndicationFeed.Rss;
|
||||
using RMuseum.Models.GanjoorAudio.ViewModels;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
|
||||
namespace RMuseum.Services.Implementation
|
||||
{
|
||||
/// <summary>
|
||||
/// creates an RSS feed from recitations
|
||||
/// </summary>
|
||||
public static class RecitationsRssBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// build rss
|
||||
/// </summary>
|
||||
/// <param name="recitations"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> Build(PublicRecitationViewModel[] recitations)
|
||||
{
|
||||
var sw = new StringWriterWithEncoding(Encoding.UTF8);
|
||||
|
||||
using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings() { Async = true, Indent = true }))
|
||||
{
|
||||
var writer = new RssFeedWriter(xmlWriter);
|
||||
|
||||
await writer.WriteTitle("خوانشهای گنجور");
|
||||
await writer.WriteDescription("روایتهای صوتی اشعار سایت گنجور");
|
||||
await writer.Write(new SyndicationLink(new Uri("http://ava.ganjoor.net")));
|
||||
await writer.WriteLastBuildDate(recitations.Length > 0 ? recitations[0].PublishDate.ToUniversalTime() : DateTimeOffset.UtcNow);
|
||||
await writer.WriteLanguage(new System.Globalization.CultureInfo("fa-IR"));
|
||||
await writer.WriteGenerator("https://ganjgah.ir");
|
||||
|
||||
foreach(PublicRecitationViewModel recitation in recitations)
|
||||
{
|
||||
var item = new SyndicationItem()
|
||||
{
|
||||
Id = recitation.LegacyAudioGuid.ToString(),
|
||||
Title = $"{recitation.PoemFullTitle} با خوانش {recitation.AudioArtist}",
|
||||
Description = recitation.HtmlText == null ? "" : recitation.HtmlText
|
||||
.Replace("</div>", "")
|
||||
.Replace("<div class=\"b\">", "")
|
||||
.Replace("<div class=\"b2\">", "")
|
||||
.Replace("<div class=\"m1\">", "")
|
||||
.Replace("<div class=\"m2\">", "")
|
||||
.Replace("<div class=\"n\">", ""),
|
||||
Published = recitation.PublishDate.ToUniversalTime(),
|
||||
};
|
||||
|
||||
item.AddLink(new SyndicationLink(new Uri($"https://ava.ganjoor.net/#/{recitation.Id}")));
|
||||
item.AddCategory(new SyndicationCategory("شعرخوانی"));
|
||||
item.AddCategory(new SyndicationCategory("ادبیات"));
|
||||
|
||||
await writer.Write(item);
|
||||
}
|
||||
|
||||
xmlWriter.Flush();
|
||||
}
|
||||
|
||||
|
||||
return sw.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
class StringWriterWithEncoding : StringWriter
|
||||
{
|
||||
private readonly Encoding _encoding;
|
||||
|
||||
public StringWriterWithEncoding(Encoding encoding)
|
||||
{
|
||||
this._encoding = encoding;
|
||||
}
|
||||
|
||||
public override Encoding Encoding
|
||||
{
|
||||
get { return _encoding; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user