#445 tajik sitemap

This commit is contained in:
Hamid Reza Mohammadi 2024-07-21 17:24:50 +03:30
parent 81f21acd3a
commit 5255c4c219
5 changed files with 128 additions and 5 deletions

View File

@ -2681,6 +2681,26 @@ namespace RMuseum.Controllers
return Ok();
}
/// <summary>
/// build tj.ganjoor.net site map
/// </summary>
/// <returns></returns>
[HttpPost("sitemap/tajik")]
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public IActionResult StartBuildingTajikSitemap()
{
var res = _ganjoorService.StartBuildingTajikSitemap();
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok();
}
/// <summary>
/// regenerate stats page
/// </summary>

View File

@ -1699,6 +1699,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.StartBuildingTajikSitemap">
<summary>
build tj.ganjoor.net site map
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.StartUpdatingStatsPage">
<summary>
regenerate stats page
@ -16172,6 +16178,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.StartBuildingTajikSitemap">
<summary>
build tj.ganjoor.net site map
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.StartUpdatingStatsPage(System.Guid)">
<summary>
start updating stats page
@ -19176,6 +19188,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.StartBuildingTajikSitemap">
<summary>
build tj.ganjoor.net site map
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemSongs(System.Int32,System.Boolean,RMuseum.Models.Ganjoor.PoemMusicTrackType)">
<summary>
get poem related songs

View File

@ -787,6 +787,12 @@ namespace RMuseum.Services
/// <returns></returns>
RServiceResult<bool> StartBuildingSitemap();
/// <summary>
/// build tj.ganjoor.net site map
/// </summary>
/// <returns></returns>
RServiceResult<bool> StartBuildingTajikSitemap();
/// <summary>
/// start updating stats page
/// </summary>

View File

@ -17,7 +17,7 @@ namespace RMuseum.Services.Implementation
public partial class GanjoorService : IGanjoorService
{
private void WriteSitemap(string filePath, List<string> urls)
private void WriteSitemap(string filePath, List<string> urls, string baseUrl)
{
if (File.Exists(filePath))
File.Delete(filePath);
@ -32,7 +32,7 @@ namespace RMuseum.Services.Implementation
"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
from url in urls
select new XElement(ns + "url",
new XElement(ns + "loc", $"https://ganjoor.net{url}"))
new XElement(ns + "loc", $"{baseUrl}{url}"))
)
);
@ -86,7 +86,8 @@ namespace RMuseum.Services.Implementation
urls.Remove("/hashieha");
WriteSitemap(firstSitemap,
urls
urls,
"https://ganjoor.net"
);
foreach (var poet in await context.GanjoorPoets.Where(p => p.Published).ToListAsync())
@ -96,7 +97,8 @@ namespace RMuseum.Services.Implementation
string poetSitemap = Path.Combine(dir, $"{poet.Id}.xml");
WriteSitemap(poetSitemap,
await context.GanjoorPages.Where(p => p.PoetId == poet.Id && p.UrlSlug != "vazn").OrderBy(p => p.Id).Select(p => p.FullUrl).ToListAsync()
await context.GanjoorPages.Where(p => p.PoetId == poet.Id && p.UrlSlug != "vazn").OrderBy(p => p.Id).Select(p => p.FullUrl).ToListAsync(),
"https://ganjoor.net"
);
sitemaps.Add(poetSitemap);
@ -134,5 +136,81 @@ namespace RMuseum.Services.Implementation
return new RServiceResult<bool>(false, exp.ToString());
}
}
/// <summary>
/// build tj.ganjoor.net site map
/// </summary>
/// <returns></returns>
public RServiceResult<bool> StartBuildingTajikSitemap()
{
try
{
_backgroundTaskQueue.QueueBackgroundWorkItem
(
async token =>
{
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC
{
LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context);
var job = (await jobProgressServiceEF.NewJob("BuildTajikSitemap", "Query data")).Result;
try
{
string xmlSitemap = Configuration.GetSection("Ganjoor")["TajikSitemapLocation"];
if (File.Exists(xmlSitemap))
File.Delete(xmlSitemap);
string dir = Path.GetDirectoryName(xmlSitemap);
await jobProgressServiceEF.UpdateJob(job.Id, 0, "", false);
List<string> sitemaps = new List<string>();
foreach (var poet in await context.TajikPoets.AsNoTracking().ToListAsync())
{
await jobProgressServiceEF.UpdateJob(job.Id, poet.Id, "", false);
string poetSitemap = Path.Combine(dir, $"{poet.Id}.xml");
WriteSitemap(poetSitemap,
await context.GanjoorPages.Where(p => p.PoetId == poet.Id && (p.GanjoorPageType == Models.Ganjoor.GanjoorPageType.PoetPage || p.GanjoorPageType == Models.Ganjoor.GanjoorPageType.CatPage || p.GanjoorPageType == Models.Ganjoor.GanjoorPageType.PoemPage)).OrderBy(p => p.Id).Select(p => p.FullUrl).ToListAsync(),
"https://tj.ganjoor.net"
);
sitemaps.Add(poetSitemap);
}
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "no"),
new XElement(ns + "sitemapindex",
new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
new XAttribute(xsiNs + "schemaLocation",
"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
from sitemap in sitemaps
select new XElement(ns + "sitemap",
new XElement(ns + "loc", $"https://tj.ganjoor.net/{Path.GetFileName(sitemap)}"))
)
);
xDoc.Save(xmlSitemap);
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
}
catch (Exception exp)
{
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
}
}
});
return new RServiceResult<bool>(true);
}
catch (Exception exp)
{
return new RServiceResult<bool>(false, exp.ToString());
}
}
}
}

View File

@ -67,7 +67,8 @@
"BooksCoverTitleFontName": "IranNastaliq",
"BooksCoverTitleFontSize": "40",
"ImportCommentsAsPoemSummary": "True",
"MaxWordLengthInComments": 200
"MaxWordLengthInComments": 200,
"TajikSitemapLocation": "C:\\inetpub\\tj\\wwwroot\\sitemap.xml"
},
"ExternalFTPServer": {
"Host": "localhost",