1
0
Fork 0
mirror of https://github.com/sbrl/PolyFeed.git synced 2024-11-14 05:13:00 +00:00

Absolutify urls before generating feeds

This commit is contained in:
Starbeamrainbowlabs 2019-07-29 19:36:43 +01:00
parent 6bb8da3660
commit 5f3d1f824d
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 33 additions and 0 deletions

View file

@ -67,6 +67,9 @@ namespace PolyFeed
HtmlNode document = html.DocumentNode;
document.AbsolutifyUris(new Uri(source.Feed.Url));
await Console.Error.WriteLineAsync("[Builder/Html] Generating feed content");
// Add the title

View file

@ -1,4 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;
@ -30,5 +32,33 @@ namespace PolyFeed.Helpers
return selectedNode.Attributes[settings.Attribute].Value;
}
/// <summary>
/// Searches for and converts all the links that are children of the current
/// <see cref="HtmlNode" /> to absolute URIs.
/// </summary>
/// <param name="rootNode">The root node to search from.</param>
/// <param name="baseUri">The base URI to use for conversion.</param>
/// <returns>The number of nodes updated.</returns>
public static int AbsolutifyUris(this HtmlNode rootNode, Uri baseUri)
{
int nodesUpdated = 0;
Parallel.ForEach(rootNode.QuerySelectorAll("a, img"), (HtmlNode node) => {
string attributeName = null;
if (node.Attributes["href"] != null) attributeName = "href";
if (node.Attributes["src"] != null) attributeName = "src";
if (node.Attributes[attributeName] == null)
return;
node.Attributes[attributeName].Value = new Uri(
baseUri,
node.Attributes[attributeName].Value
).ToString();
Interlocked.Increment(ref nodesUpdated);
});
return nodesUpdated;
}
}
}