A .NET Standard library to extract the main content of a web page based on a port of the Readability library by Mozilla. It also determine and gather metadata about the content, such as language, author, main image, etc.
$ dotnet add package SmartReader
This library supports the .NET Standard 2.0. The core algorithm is a port of the Mozilla Readability library. The original library is stable and used in production inside Firefox. This way we can piggyback on the hard and well-tested work of Mozilla.
SmartReader also added some improvements on the original library, getting more and better metadata:
Some of these fields are now present in the original library.
It also allows to perform custom operations before and after extracting the article.
Feel free to suggest new features.
It is trivial using the NuGet package.
PM> Install-Package SmartReader
There are mainly two ways to use the library:
The first is by creating a new Reader object, with the URI as the argument, and then calling the GetArticle method to obtain the extracted Article
The second one is by using one of the static methods ParseArticle of Reader directly, to return an Article.
Both ways are available also through an async method, called respectively GetArticleAsync and ParseArticleAsync.
The advantage of using an object, instead of the static method, is that it gives you the chance to set some options.
There is also the option to parse directly a String or Stream that you have obtained by some other way. This is available either with one of the ParseArticle methods or by using the proper Reader constructor. In either case, you also need to give the original URI. It will not re-download the text, but it needs the URI to make some checks and fixing the links present on the page. If you cannot provide the original uri, you can use a fake one, like https:\\localhost.
If the extraction fails to extract an article, the returned Article object will have the field IsReadable set to false.
If fetching the resource fails, the library will catch the HttpRequestException, set IsReadable to false, Completed to false and add an Exception to the list of Errors.
The content of the article is unstyled, but it is wrapped in a div with the id readability-content that you can style yourself.
The library tries to detect the correct encoding of the text, if the correct tags are present in the text.
On the Article object you can call GetImagesAsync to obtain a Task for a list of Image objects, representing the images found in the extracted article. The method is async because it makes HEAD Requests, to obtain the size of the images and only returns the ones that are bigger than the specified size. The size by default is 75KB.
This is done to exclude things such as images used in the UI.
On the Article object you can also call ConvertImagesToDataUriAsync to inline the images found in the article using the data URI scheme. The method is async. This will insert the images into the Content property of the Article. This may significantly increase the size of Content.
The data URI scheme is not efficient, because is using Base64 to encode the bytes of the image. Base64 encoded data is approximately 33% larger than the original data. The purpose of this method is to provide an offline article that can be fully stored long term. This is useful in case the original article is not accessible anymore. The method only converts the images that are bigger than the specified size. The size by default is 75KB. This is done to exclude things such as images used in the UI.
Notice that this method will not store other external elements that are not images, such as embedded videos.
If fetching an image fails, the library will throw an HttpRequestException, you should handle the exception.
Using the GetArticle method.
SmartReader.Reader sr = new SmartReader.Reader("https://arstechnica.com/information-technology/2017/02/humans-must-become-cyborgs-to-survive-says-elon-musk/");
sr.Debug = true;
sr.LoggerDelegate = Console.WriteLine;
SmartReader.Article article = sr.GetArticle();
var images = article.GetImagesAsync();
if(article.IsReadable)
{
// do something with it
}
Using the ParseArticle static method.
SmartReader.Article article = SmartReader.Reader.ParseArticle("https://arstechnica.com/information-technology/2017/02/humans-must-become-cyborgs-to-survive-says-elon-musk/");
if(article.IsReadable)
{
Console.WriteLine($"Article title {article.Title}");
}
The following settings on the Reader class can be modified.
int MaxElemsToParseint NTopCandidates bool Debug Action<string> LoggerDelegate ReportLevel Logging LoggerDelegate. The valid values are the ones for the enum ReportLevel: Issue or Info. The first level logs only errors or issue that could prevent correctly obtaining an article. The second level logs all the information needed for debugging a problematic article.bool ContinueIfNotReadable int CharThreshold bool KeepClasses String[] ClassesToPreserve bool DisableJSONLD Dictionary<string, int> MinContentLengthReadearable int MinScoreReaderable Func<IElement, bool> IsNodeVisible bool ForceHeaderEncoding int AncestorsDepth int ParagraphThreshold double linkDensityModifier The settings MinScoreReaderable, CharThreshold and MinContentLengthReadearable are used in the process of determining if an article is readerable or if the result found is valid.
The algorithm for scoring assign some score to each valid node, then it determines the best node depending on their relationships, i.e., what score ancestors and descendants of the node have. The settings NTopCandidates, AncestorsDepth and ParagraphThreshold can help you customize this process. It makes sense to change them if you are interested in some sites that uses a particular style or design of coding.
The settings ParagraphThreshold, MinContentLengthReadearable and CharThreshold should be customized for content written in non-alphabetical languages.
A brief overview of the Article model returned by the library.
Uri UriString TitleString BylineString DirString FeaturedImageString ContentString TextContentString ExcerptString LanguageDictionary<string, Uri> AlternativeLanguageUrisString AuthorString SiteNameint LengthTimeSpan TimeToReadDateTime? PublicationDatebool IsReadablebool CompletedList<Exception> ErrorsIt's important to be aware that the fields Byline, Author and PublicationDate are found independently of each other. So there might be some inconsistencies and unexpected data. For instance, Byline may be a string in the form "@Date by @Author" or "@Author, @Date" or any other combination used by the publication.
The TimeToRead calculation is based on the research found in Standardized Assessment of Reading Performance: The New International Reading Speed Texts IReST. It should be accurate if the article is written in one of the languages in the research, but it is just an educated guess for the others languages.
The FeaturedImage property holds the image indicated by the Open Graph or Twitter meta tags. If neither of these is present, and you called the GetImagesAsync method, it will be set with the first image found.
The TextContent property is based on the pure text content of the HTML (i.e., the concatenations of text nodes. Then we apply some basic formatting, like removing double spaces or the newlines left by the formatting of the HTML code. We also add meaningful newlines for P and BR nodes.
The IsReadable property will be false if no article was extracted, whatever the reason (i.e., the algorithm did not found anything valuable or the request failed). The property Completed just indicated whether the process completed correctly or not. Previously we left to the user of the library to manage exceptions, but now we try to handle them ourselves.
Often the webpage containing the article will contain metadata about the language used in the article. Sometimes this is not true, so the library comes with a delegate LanguageIdentification in Article, that can be used to identify the language based on the text itself. This delegate will be called on the TextContent of the Article. The default method just returns the language in the metadata. This is the old, standard behavior.
You can use the delegate to implement your own method to identify the language. We also provide a decent implementation, that actually does something, using FastText. This implementation is distributed in a separate nuget package, SmartReader.NaturalLanguageProcessing.
To use the language identification feature you will need to call the Enable method.
NLP.Enable();
This will change the delegate LanguageIdentification, so it will automatically identify the language and set the property Language in every Article object created by the library.
To restore the default behavior, instead use RestoreDefaults.
NLP.RestoreDefaults();
There is also a delegate to create a summary of the article : CreateSummary. Also in this case the default implementation returns the summary provided by the metadata. In practice this is usually a short summary meant for social media sharing. At this moment we do not provide any better implementation.
The library could throw some exceptions, that should be caught and reported in the Errors property and set Completed to false.
If you set a value for MaxElemsToParse larger than 0, the library will throw a standard Exception if the threshold is passed.
If fetching an HTTP resource fails, the library will throw an HttpRequestException. This will happen both when trying to fetch the whole article and when trying to fetch an image.
This project has the following directory structure.
| Folder | Description |
|---|---|
| docfx_project/ | Contains the DocFx project that generates the documentation website |
| src/ | The main source folder |
| src/SmartReader | Source for the SmartReader library |
| src/SmartReader.NaturalLanguageProcessing | Source for an implementation for NLP features |
| src/SmartReaderTests | Source for the Tests |
| src/SmartReaderConsole | Source for example console project |
| src/SmartReader.WebDemo | Source for the demo web project |
You can see the demo web live. So you can test for yourself how effective the library can be for you.
There is also a Docker project for the web demo. You can build and run it with the usual docker commands.
docker build -t smartreader-webdemo .
docker run -it -p 5000:5000 smartreader-webdemo
The second command will forward traffic from port 5000 on your local host to the port 5000 of the docker container. This means that you will be able to access the web demo by visiting http://localhost:5000.
This README contains the info to get started in using the library. If you want to know more advanced options, API reference, etc. read the documentation on the main website.