056e426572
Updated project files for `Beam.Dynamic`, `Beam.Exports`, `Beam.Puppeteer`, `Beam.Temporary.Cli`, and `Beam` to include new XML headers, reorganize property groups, and add project references. Modified `PuppetedUnitDownloader` to support additional parameters for async transformers. Changed return types in `CommonTransformers` to `AsyncTransformer` for asynchronous processing. Significant refactoring in `DownloadBuilder`, `DownloadContext`, and `DownloadContextBuilder` to introduce generic parameters and improve context management. Updated `SequentialDownloader`, `SequentialFragmentDownloader`, and `UnitDownloader` to accommodate new async transformer types. Introduced `TypeExtensions` for unique type name generation and added `UnitFragmentDownloaderBinary` for handling binary downloads. Updated solution file to include the new `aeqw89.Beam` project, ensuring proper references across the solution. These changes enhance the asynchronous capabilities of the Beam library, improve type safety, and streamline the downloading process.
159 lines
6.6 KiB
C#
159 lines
6.6 KiB
C#
using aeqw89.PersistentData;
|
|
using aeqw89.DataKeys;
|
|
using Beam.Dynamic;
|
|
using HtmlAgilityPack;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.Json.Serialization.Metadata;
|
|
using Beam.Temporary.Cli.Templates.Classic;
|
|
using Beam.Exports;
|
|
|
|
namespace Beam.Temporary.Cli {
|
|
internal class Program {
|
|
|
|
public static JsonSerializerOptions ConversionOptions { get; internal set; } = new();
|
|
|
|
public static BeamDataDictionary BeamData { get; set; } = [];
|
|
|
|
public static IArchitecture Architecture = IArchitecture.Default;
|
|
|
|
const string BeamDataPath = "data/.dat";
|
|
|
|
static async Task Main(string[] args) {
|
|
ConversionOptions.Converters.AddPersistentDataRequiredConverters();
|
|
ConversionOptions.WriteIndented = true;
|
|
|
|
var web = new HtmlWeb();
|
|
|
|
var lf = LoggerFactory.Create((x) => {
|
|
x.AddConsole();
|
|
});
|
|
|
|
ILogger logger = lf
|
|
.CreateLogger("Program");
|
|
|
|
await using var sharedContext = await DataDictionaryContext<BeamDataDictionary>.Create(
|
|
BeamDataPath,
|
|
false,
|
|
DataKind.Shared,
|
|
logger,
|
|
ConversionOptions
|
|
);
|
|
|
|
BeamData = sharedContext.Data;
|
|
|
|
BeamData.Clear();
|
|
NovelStatics.Define_WoDuShu(BeamData);
|
|
NovelStatics.Define_WoDuShu_HouseOfHorrors(BeamData);
|
|
ClassicTemplates.Register(BeamData);
|
|
|
|
await sharedContext.ForceSave();
|
|
|
|
CancellationTokenSource cts = new();
|
|
|
|
var novel = new DataKey<TextResource>("novels:house_of_horrors");
|
|
|
|
var metadata2 = await DownloadBuilder<HtmlDocument, IDocumentMetaData>.FromMeta(novel, BeamData)
|
|
.WithLink()
|
|
.WithTransformer(CommonTransformers.ArticleDataTransformer)
|
|
.Configure((x) => x
|
|
.WithDownloadLogger(logger)
|
|
.WithRetryReporter(new Progress<RetryReport>())
|
|
.WithTimeOut(TimeSpan.FromSeconds(15)))
|
|
.Build()
|
|
.FirstAsync();
|
|
|
|
var downloader2 = DownloadBuilder<HtmlDocument, IDocument>.FromText(novel, BeamData)
|
|
.WithRange(1..5)
|
|
.WithLinkGenerator()
|
|
.WithTransformer((x) => CommonTransformers.DocumentTransformer(x, metadata2.Data))
|
|
.Configure((x) => x
|
|
.WithDownloadLogger(logger)
|
|
.WithDownloadReporter(new Progress<DownloadReport>((x) => logger.LogInformation(x.ToString())))
|
|
.WithTimeOut(TimeSpan.FromSeconds(15))
|
|
)
|
|
.Build();
|
|
|
|
|
|
|
|
List<Task<Ordered<IDocument>>> translationTasks = [];
|
|
List<Ordered<IDocument>> documents = [];
|
|
|
|
await foreach (var download in downloader2.Take(10)) {
|
|
if (!download.Data.MetaData.TryGetValue(Architecture.ChapterKey, out var meta))
|
|
continue;
|
|
if (meta is not ArticleData articleMetaData)
|
|
continue;
|
|
if (!download.Data.MetaData.TryGetValue(Architecture.BookKey, out var bookmeta))
|
|
continue;
|
|
if (meta is not ArticleData bookMetaData)
|
|
continue;
|
|
//Console.WriteLine($"Title: {data.Name}");
|
|
//Console.WriteLine($"Description: {data.Description}");
|
|
//Console.WriteLine($"Categories: {data.Categories.Aggregate((x, y) => $"{x}; {y}")}");
|
|
//Console.WriteLine($"Authors: {data.Authors.Aggregate((x,y) => $"{x}; {y}")}");
|
|
Console.WriteLine($"Chapter title: {articleMetaData.Name}");
|
|
Console.WriteLine($"Book title: {bookMetaData.Name}");
|
|
//Console.WriteLine($"Content: {download}");
|
|
|
|
//translationTasks.Add(Task.Run(async () => {
|
|
// logger.LogInformation("Beginning translation {} task for {}", download.Order, articleMetaData.Name);
|
|
// var ret = new Ordered<IDocument>(await QuickAndDirtyJanitor.TranslateAsync(download.Data), download.Order);
|
|
// logger.LogInformation("Finished translation {} task for {}", download.Order, articleMetaData.Name);
|
|
// return ret;
|
|
//}));
|
|
}
|
|
|
|
documents = (await Task.WhenAll(translationTasks)).ToList();
|
|
|
|
string testDir = Path.Combine("txt", Path.GetRandomFileName());
|
|
Directory.CreateDirectory(testDir);
|
|
|
|
int len = documents.MaxBy((x) => x.Order)?.Order ?? -1;
|
|
foreach (var document in documents.OrderBy((x) => x.Order)) {
|
|
document.Data.MetaData.TryGetValue(Architecture.ChapterKey, out var chapterMetaData);
|
|
Dictionary<string, string> linkButtons = new();
|
|
if (document.Order != 0)
|
|
linkButtons.Add("Previous", $"{document.Order - 1}.html");
|
|
if (document.Order != len)
|
|
linkButtons.Add("Next", $"{document.Order + 1}.html");
|
|
new HtmlExporter(document.Data, chapterMetaData as ArticleData, linkButtons).Write(Path.Combine(testDir, $"{document.Order}.html"));
|
|
}
|
|
|
|
Console.ReadKey();
|
|
|
|
//foreach (var download in documents.OrderBy((x) => x.Order)) {
|
|
// if (download.Data.TryGetTaggedMetaData<ArticleData>(Architecture.ChapterKey, out var meta))
|
|
// Console.WriteLine($"{download.Order}:{meta.Name}");
|
|
//}
|
|
|
|
//string[] templates = new DataKey<File>[] {
|
|
// HtmlBook.Keys.ContentPage,
|
|
// HtmlBook.Keys.NoContentPage,
|
|
// HtmlBook.Keys.TitlePage,
|
|
// HtmlBook.Keys.StylesPage,
|
|
//}.Select(
|
|
// (x) => BeamData.Files.ReadToString(x.WithNamespace("aeqw89:files:templates:classic"))
|
|
//).ToArray();
|
|
|
|
//HtmlBook book = new(
|
|
// bookname: Path.Combine(Path.GetRandomFileName(), "I Alone Level Up"),
|
|
// new CssData(),
|
|
// new ArticleData(),
|
|
// new HtmlBookTemplates() {
|
|
// ContentPageTemplate = templates[0],
|
|
// NoContentTemplate = templates[1],
|
|
// TitlePageTemplate = templates[2],
|
|
// CssTemplate = templates[3],
|
|
// },
|
|
// documents: documents.Select((x) => x.Data).ToList()
|
|
//);
|
|
|
|
//book.Update();
|
|
//Console.WriteLine("One variable!");
|
|
}
|
|
}
|
|
}
|