#438 ImportPaperSourcesFromMuseum
This commit is contained in:
parent
113544145c
commit
1058a28f1a
@ -18251,6 +18251,11 @@
|
||||
<param name="categoryId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.ImportPaperSourcesFromMuseum">
|
||||
<summary>
|
||||
import paper sources from museum
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoetSuggestedSpecLinesAsync(System.Int32)">
|
||||
<summary>
|
||||
return list of suggested spec lines
|
||||
|
||||
@ -5,6 +5,12 @@ using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using RMuseum.Models.GanjoorIntegration;
|
||||
using RMuseum.DbContext;
|
||||
using RSecurityBackend.Services.Implementation;
|
||||
using RSecurityBackend.Models.Generic.Db;
|
||||
using System.Collections.Generic;
|
||||
using RMuseum.Models.PDFLibrary;
|
||||
using System.Reflection.PortableExecutable;
|
||||
|
||||
namespace RMuseum.Services.Implementation
|
||||
{
|
||||
@ -32,5 +38,96 @@ namespace RMuseum.Services.Implementation
|
||||
return new RServiceResult<GanjoorPaperSource[]>(null, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// import paper sources from museum
|
||||
/// </summary>
|
||||
public void ImportPaperSourcesFromMuseum()
|
||||
{
|
||||
_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("ImportPaperSourcesFromMuseum", "Query data")).Result;
|
||||
await _ImportPaperSourcesFromMuseumAsync(context, jobProgressServiceEF, job);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task _ImportPaperSourcesFromMuseumAsync(RMuseumDbContext context, LongRunningJobProgressServiceEF jobProgressServiceEF, RLongRunningJobStatus job)
|
||||
{
|
||||
try
|
||||
{
|
||||
var artifacts = await context.Artifacts.AsNoTracking().Include(c => c.CoverImage).ToListAsync();
|
||||
int index = 0;
|
||||
foreach (var artifact in artifacts)
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, index++, $"{index} از {artifacts.Count} - {artifact.Name}");
|
||||
var links = await context.GanjoorLinks.AsNoTracking().Where(l => l.ArtifactId == artifact.Id).ToListAsync();
|
||||
|
||||
Dictionary<int, int> poets = new Dictionary<int, int>();
|
||||
foreach (var link in links)
|
||||
{
|
||||
var poem = await context.GanjoorPoems.AsNoTracking().Where(p => p.Id == link.GanjoorPostId).SingleOrDefaultAsync();
|
||||
if(poem == null) continue;
|
||||
var cat = await context.GanjoorCategories.AsNoTracking().Where(c => c.Id == poem.CatId).SingleOrDefaultAsync();
|
||||
if(cat == null) continue;
|
||||
if(poets.ContainsKey(cat.PoetId))
|
||||
{
|
||||
poets[cat.PoetId]++;
|
||||
}
|
||||
else
|
||||
{
|
||||
poets[cat.PoetId] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(poets.Count > 0)
|
||||
{
|
||||
var mainPoet = poets.First();
|
||||
foreach (var poet in poets)
|
||||
{
|
||||
if(poet.Value > mainPoet.Value)
|
||||
{
|
||||
mainPoet = poet;
|
||||
}
|
||||
}
|
||||
|
||||
if(mainPoet.Value > 3)
|
||||
{
|
||||
var poet = await context.GanjoorPoets.AsNoTracking().Where(p => p.Id == mainPoet.Key).SingleAsync();
|
||||
var cat = await context.GanjoorCategories.AsNoTracking().Where(c => c.PoetId == poet.Id & c.ParentId == null).SingleAsync();
|
||||
GanjoorPaperSource paperSource = new GanjoorPaperSource()
|
||||
{
|
||||
GanjoorPoetId = poet.Id,
|
||||
GanjoorCatId = cat.Id,
|
||||
GanjoorCatFullTitle = poet.Nickname,
|
||||
GanjoorCatFullUrl = cat.FullUrl,
|
||||
BookType = LinkType.Museum,
|
||||
BookFullUrl = $"https://museum.ir/items/{artifact.FriendlyUrl}",
|
||||
NaskbanBookId = 0,
|
||||
BookFullTitle = artifact.Name,
|
||||
CoverThumbnailImageUrl = artifact.CoverImage.ExternalNormalSizeImageUrl.Replace("/norm/", "/thumb/").Replace("/orig/", "/thumb/"),
|
||||
Description = "",
|
||||
IsTextOriginalSource = await context.GanjoorLinks.AsNoTracking().Where(l => l.ArtifactId == artifact.Id && l.IsTextOriginalSource == true).AnyAsync(),
|
||||
MatchPercent = 100,
|
||||
HumanReviewed = true,
|
||||
OrderIndicator = 1,
|
||||
};
|
||||
context.GanjoorPaperSources.Add(paperSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user