Introduce Beam.Fluent and Beam.Models projects
Added new Beam.Fluent and Beam.Models projects with staged download builder and data context models. Refactored and moved model classes from Beam.Temporary.Cli to Beam.Models. Added new data providers and extended DataBindings in Beam.Dynamic. Renamed Beam.Puppeteer to Beam.Playwright and updated related classes. Updated project references and package versions. Removed obsolete and unused files from Beam.Temporary.Cli.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using HtmlAgilityPack;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Beam.Dynamic {
|
||||
public class AnchorCollectionDataProvider : IDataProvider<string[]>, IDataProvider<SourceLink[]> {
|
||||
public IBinding? Content { get; set; }
|
||||
public string? RelativeTo { get; set; }
|
||||
|
||||
private string GetAbsolute(string? @base, string relative) {
|
||||
if (@base is null)
|
||||
return relative;
|
||||
|
||||
if (@base.EndsWith('/'))
|
||||
@base = @base[..^1];
|
||||
if (relative.StartsWith('/'))
|
||||
relative = relative[1..];
|
||||
return @base + '/' + relative;
|
||||
}
|
||||
|
||||
public string[] Get(HtmlDocument document) {
|
||||
if (Content is null)
|
||||
return [];
|
||||
|
||||
var node = Content.Select(document);
|
||||
if (node is null)
|
||||
return [];
|
||||
|
||||
List<string> links = [];
|
||||
foreach (var child in node.Descendants())
|
||||
links.Add(child.GetAttributeValue("href", ""));
|
||||
|
||||
return links.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
|
||||
}
|
||||
|
||||
SourceLink[] IDataProvider<SourceLink[]>.Get(HtmlDocument document) {
|
||||
var links = Get(document);
|
||||
|
||||
if (links.Length == 0)
|
||||
return [];
|
||||
|
||||
List<SourceLink> slinks = [];
|
||||
foreach (var link in links)
|
||||
if (Uri.TryCreate(GetAbsolute(RelativeTo, link), UriKind.RelativeOrAbsolute, out _))
|
||||
slinks.Add(new SourceLink(GetAbsolute(RelativeTo, link)));
|
||||
|
||||
return slinks.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using HtmlAgilityPack;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Beam.Dynamic {
|
||||
public class AnchorDataProvider : IDataProvider<SourceLink>, IDataProvider<string> {
|
||||
public IBinding? Content { get; set; }
|
||||
|
||||
public string Get(HtmlDocument document) {
|
||||
if (Content is null)
|
||||
return "";
|
||||
|
||||
return Content.Select(document)?.GetAttributeValue("href", "") ?? "";
|
||||
|
||||
}
|
||||
|
||||
SourceLink IDataProvider<SourceLink>.Get(HtmlDocument document) {
|
||||
var content = Get(document);
|
||||
if (content is null)
|
||||
return SourceLink.InvalidLink;
|
||||
|
||||
if (!Uri.TryCreate(content, UriKind.RelativeOrAbsolute, out _))
|
||||
return SourceLink.InvalidLink;
|
||||
|
||||
return new SourceLink(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,8 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="aeqw89.DataKeys" Version="1.0.1" />
|
||||
<PackageReference Include="aeqw89.PersistentData" Version="1.0.0" />
|
||||
<PackageReference Include="aeqw89.DataKeys" Version="2.0.1" />
|
||||
<PackageReference Include="aeqw89.PersistentData" Version="1.1.0" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />
|
||||
<PackageReference Include="Microsoft.Recognizers.Text.Number" Version="1.8.13" />
|
||||
</ItemGroup>
|
||||
|
||||
+169
-10
@@ -1,22 +1,161 @@
|
||||
using HtmlAgilityPack;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Beam.Dynamic {
|
||||
public record class DataBindings {
|
||||
public IDataProvider<string>? Title { get; set; }
|
||||
public IDataProvider<string[]>? Authors { get; set; }
|
||||
public IDataProvider<string>? Description { get; set; }
|
||||
public IDataProvider<string>? Content { get; set; }
|
||||
public IDataProvider<string[]>? Language { get; set; }
|
||||
public IDataProvider<string[]>? Tags { get; set; }
|
||||
#region ---------------------- Common Bindings ----------------------
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string>? Title {
|
||||
get => Get<string>(nameof(Title));
|
||||
set => Providers[nameof(Title)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string[]>? Authors {
|
||||
get => Get<string[]>(nameof(Authors));
|
||||
set => Providers[nameof(Authors)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string>? Description {
|
||||
get => Get<string>(nameof(Description));
|
||||
set => Providers[nameof(Description)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string>? Content {
|
||||
get => Get<string>(nameof(Content));
|
||||
set => Providers[nameof(Content)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string[]>? Language {
|
||||
get => Get<string[]>(nameof(Language));
|
||||
set => Providers[nameof(Language)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string[]>? Tags {
|
||||
get => Get<string[]>(nameof(Tags));
|
||||
set => Providers[nameof(Tags)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string>? Publisher {
|
||||
get => Get<string>(nameof(Publisher));
|
||||
set => Providers[nameof(Publisher)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<DateTimeOffset>? PublicationDate {
|
||||
get => Get<DateTimeOffset>(nameof(PublicationDate));
|
||||
set => Providers[nameof(PublicationDate)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string>? ISBN {
|
||||
get => Get<string>(nameof(ISBN));
|
||||
set => Providers[nameof(ISBN)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<int>? PageCount {
|
||||
get => Get<int>(nameof(PageCount));
|
||||
set => Providers[nameof(PageCount)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<SourceLink>? CoverImage {
|
||||
get => Get<SourceLink>(nameof(CoverImage));
|
||||
set => Providers[nameof(CoverImage)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string[]>? Series {
|
||||
get => Get<string[]>(nameof(Series));
|
||||
set => Providers[nameof(Series)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<int>? Edition {
|
||||
get => Get<int>(nameof(Edition));
|
||||
set => Providers[nameof(Edition)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string[]>? Contributors {
|
||||
get => Get<string[]>(nameof(Contributors));
|
||||
set => Providers[nameof(Contributors)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string[]>? Subjects {
|
||||
get => Get<string[]>(nameof(Subjects));
|
||||
set => Providers[nameof(Subjects)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<string>? Rights {
|
||||
get => Get<string>(nameof(Rights));
|
||||
set => Providers[nameof(Rights)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<SourceLink[]>? TableOfContents {
|
||||
get => Get<SourceLink[]>(nameof(TableOfContents));
|
||||
set => Providers[nameof(TableOfContents)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<SourceLink[]>? PagesDropDown {
|
||||
get => Get<SourceLink[]>(nameof(PagesDropDown));
|
||||
set => Providers[nameof(PagesDropDown)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<SourceLink>? NextPageButton {
|
||||
get => Get<SourceLink>(nameof(NextPageButton));
|
||||
set => Providers[nameof(NextPageButton)] = value;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public IDataProvider<SourceLink>? PreviousPageButton {
|
||||
get => Get<SourceLink>(nameof(PreviousPageButton));
|
||||
set => Providers[nameof(PreviousPageButton)] = value;
|
||||
}
|
||||
#endregion
|
||||
|
||||
public Dictionary<string, IDataProvider?> Providers { get; set; } = [];
|
||||
|
||||
private IDataProvider<T>? Get<T>(string key) {
|
||||
if (Providers.TryGetValue(key, out var k) && k is IDataProvider<T> ks)
|
||||
return ks;
|
||||
return default;
|
||||
}
|
||||
|
||||
public virtual ResolvedBindings Resolve(HtmlDocument doc) {
|
||||
return new ResolvedBindings() {
|
||||
// explicit fields already handled below
|
||||
var mappedKeys = new HashSet<string> {
|
||||
nameof(Title), nameof(Authors), nameof(Description), nameof(Content),
|
||||
nameof(Language), nameof(Tags), nameof(Publisher), nameof(PublicationDate),
|
||||
nameof(ISBN), nameof(PageCount), nameof(CoverImage), nameof(Series),
|
||||
nameof(Edition), nameof(Contributors), nameof(Subjects), nameof(Rights),
|
||||
nameof(TableOfContents), nameof(PagesDropDown), nameof(NextPageButton),
|
||||
nameof(PreviousPageButton)
|
||||
};
|
||||
|
||||
var additional = new Dictionary<string, object?>();
|
||||
|
||||
foreach (var (key, provider) in Providers) {
|
||||
if (!mappedKeys.Contains(key) && provider is not null) {
|
||||
// dynamic call so any IDataProvider<T> works
|
||||
additional[key] = ((dynamic)provider).Get(doc);
|
||||
}
|
||||
}
|
||||
|
||||
return new ResolvedBindings {
|
||||
Title = Title?.Get(doc),
|
||||
Authors = Authors?.Get(doc) ?? [],
|
||||
Language = Language?.Get(doc),
|
||||
Content = Content?.Get(doc),
|
||||
Description = Description?.Get(doc),
|
||||
Tags = Tags?.Get(doc) ?? []
|
||||
Content = Content?.Get(doc),
|
||||
Language = Language?.Get(doc),
|
||||
Tags = Tags?.Get(doc) ?? [],
|
||||
Publisher = Publisher?.Get(doc),
|
||||
PublicationDate = PublicationDate?.Get(doc),
|
||||
ISBN = ISBN?.Get(doc),
|
||||
PageCount = PageCount?.Get(doc),
|
||||
CoverImage = CoverImage?.Get(doc),
|
||||
Series = Series?.Get(doc) ?? [],
|
||||
Edition = Edition?.Get(doc),
|
||||
Contributors = Contributors?.Get(doc) ?? [],
|
||||
Subjects = Subjects?.Get(doc) ?? [],
|
||||
Rights = Rights?.Get(doc),
|
||||
TableOfContents = TableOfContents?.Get(doc) ?? [],
|
||||
PagesDropDown = PagesDropDown?.Get(doc),
|
||||
NextPageButton = NextPageButton?.Get(doc),
|
||||
PreviousPageButton = PreviousPageButton?.Get(doc),
|
||||
Additional = additional
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -28,5 +167,25 @@ namespace Beam.Dynamic {
|
||||
public string? Content { get; set; }
|
||||
public string[]? Language { get; set; }
|
||||
public string[]? Tags { get; set; }
|
||||
public string? Publisher { get; set; }
|
||||
public DateTimeOffset? PublicationDate { get; set; }
|
||||
public string? ISBN { get; set; }
|
||||
public int? PageCount { get; set; }
|
||||
public SourceLink? CoverImage { get; set; }
|
||||
public string[]? Series { get; set; }
|
||||
public int? Edition { get; set; }
|
||||
public string[]? Contributors { get; set; }
|
||||
public string[]? Subjects { get; set; }
|
||||
public string? Rights { get; set; }
|
||||
public SourceLink[]? TableOfContents { get; set; }
|
||||
public SourceLink[]? PagesDropDown { get; set; }
|
||||
public SourceLink? NextPageButton { get; set; }
|
||||
public SourceLink? PreviousPageButton { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Values resolved from any providers whose keys aren’t represented
|
||||
/// by the named properties above.
|
||||
/// </summary>
|
||||
public Dictionary<string, object?> Additional { get; set; } = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.Marshalling;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
@@ -12,6 +14,18 @@ namespace Beam.Dynamic {
|
||||
IDataProvider<string[]>,
|
||||
IDataProvider<SourceLink[]> {
|
||||
public IBinding? Content { get; set; }
|
||||
public string? RelativeTo { get; set; }
|
||||
|
||||
private string GetAbsolute(string? @base, string relative) {
|
||||
if (@base is null)
|
||||
return relative;
|
||||
|
||||
if (@base.EndsWith('/'))
|
||||
@base = @base[..^1];
|
||||
if (relative.StartsWith('/'))
|
||||
relative = relative[1..];
|
||||
return @base + '/' + relative;
|
||||
}
|
||||
|
||||
public SourceLink[] Get(HtmlDocument document) {
|
||||
if (Content is null)
|
||||
@@ -22,9 +36,9 @@ namespace Beam.Dynamic {
|
||||
List<SourceLink> links = [];
|
||||
foreach (var child in node.ChildNodes.Where(x => x.Name == "option")) {
|
||||
var childValue = child.GetAttributeValue("value", null);
|
||||
if (!Uri.TryCreate(childValue, UriKind.Absolute, out _))
|
||||
if (!Uri.TryCreate(GetAbsolute(RelativeTo, childValue), UriKind.Absolute, out _))
|
||||
continue;
|
||||
links.Add(new SourceLink(childValue));
|
||||
links.Add(new SourceLink(GetAbsolute(RelativeTo, childValue)));
|
||||
}
|
||||
|
||||
return links.ToArray();
|
||||
|
||||
@@ -7,7 +7,14 @@ namespace Beam.Dynamic {
|
||||
[JsonDerivedType(typeof(ContentsArrayDataProvider), "array")]
|
||||
[JsonDerivedType(typeof(ContentsDataProvider), "single")]
|
||||
[JsonDerivedType(typeof(DropDownDataProvider), "dropdown")]
|
||||
public interface IDataProvider<T> {
|
||||
[JsonDerivedType(typeof(AnchorCollectionDataProvider), "anchor-list")]
|
||||
[JsonDerivedType(typeof(AnchorDataProvider), "anchor")]
|
||||
public interface IDataProvider {
|
||||
public string GetString(HtmlDocument document)
|
||||
=> (this as IDataProvider<object>)?.Get(document)?.ToString() ?? "";
|
||||
}
|
||||
|
||||
public interface IDataProvider<out T> : IDataProvider {
|
||||
public T Get(HtmlDocument document);
|
||||
//public HtmlNode? GetNode(HtmlDocument document);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Beam.Dynamic {
|
||||
public class StateChangerFactory {
|
||||
[JsonIgnore]
|
||||
public IStateChangeBehaviour Behavior => FactoryTable[StateChangerKey]();
|
||||
|
||||
[JsonInclude]
|
||||
public string StateChangerKey { get; set; }
|
||||
|
||||
[JsonConstructor]
|
||||
public StateChangerFactory(string stateChangerKey) {
|
||||
if (!Keys.Contains(stateChangerKey))
|
||||
throw new ArgumentException($"{stateChangerKey} not in keys list", nameof(stateChangerKey));
|
||||
StateChangerKey = stateChangerKey;
|
||||
}
|
||||
|
||||
public static Dictionary<string, Func<IStateChangeBehaviour>> FactoryTable = new() {
|
||||
{ LastAsNumber, () => CommonStateChangers.LastAsNumber },
|
||||
{ LastAsNumberPrefixed, () => CommonStateChangers.NthAsNumber(^1, true) },
|
||||
{ Constant, () => CommonStateChangers.Constant },
|
||||
};
|
||||
|
||||
public HashSet<string> Keys = [LastAsNumber, LastAsNumberPrefixed, Constant];
|
||||
public const string LastAsNumber = "LastAsNumber";
|
||||
public const string LastAsNumberPrefixed = "LastAsNumberPrefixed";
|
||||
public const string Constant = "Constant";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user