diff --git a/ReadSharp.Tests/ReadTests.cs b/ReadSharp.Tests/ReadTests.cs index c0806d1..5576b1c 100644 --- a/ReadSharp.Tests/ReadTests.cs +++ b/ReadSharp.Tests/ReadTests.cs @@ -59,7 +59,7 @@ namespace ReadSharp.Tests [Fact] public async Task ReadArticleWithInvalidUriTest() { - await ThrowsAsync(async () => + await ThrowsAsync(async () => { await reader.Read(new Uri("http://frontendplayyyyy.com")); }); diff --git a/ReadSharp/Models/ArticleImage.cs b/ReadSharp/Models/ArticleImage.cs index d8377cd..1389958 100644 --- a/ReadSharp/Models/ArticleImage.cs +++ b/ReadSharp/Models/ArticleImage.cs @@ -7,6 +7,14 @@ namespace ReadSharp /// public class ArticleImage { + /// + /// Gets or sets the identifier. + /// + /// + /// The identifier. + /// + public string ID { get; set; } + /// /// Gets or sets the URI. /// diff --git a/ReadSharp/ReadException.cs b/ReadSharp/ReadException.cs new file mode 100644 index 0000000..6d92b47 --- /dev/null +++ b/ReadSharp/ReadException.cs @@ -0,0 +1,33 @@ +using System; + +namespace ReadSharp +{ + /// + /// ReadSharp exception + /// + public class ReadException : Exception + { + /// + /// Initializes a new instance of the class. + /// + public ReadException() + : base() { } + + + /// + /// Initializes a new instance of the class. + /// + /// The message that describes the error. + public ReadException(string message) + : base(message) { } + + + /// + /// Initializes a new instance of the class. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified. + public ReadException(string message, Exception innerException) + : base(message, innerException) { } + } +} diff --git a/ReadSharp/Reader.cs b/ReadSharp/Reader.cs index 3c4afa7..50f5d14 100644 --- a/ReadSharp/Reader.cs +++ b/ReadSharp/Reader.cs @@ -111,25 +111,21 @@ namespace ReadSharp bool noHeadline = false, bool useDeepLinks = false) { - HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri); - HttpResponseMessage respo = null; + Response response; + TranscodingResult transcodingResult; + Encoding encoding; // make async request try { - respo = await _httpClient.SendAsync(request); + // get HTML string from URI + response = await Request(uri); } catch (HttpRequestException exc) { - throw new Exception(exc.Message, exc); + throw new ReadException(exc.Message); } - // read response - string resp = await respo.Content.ReadAsStringAsync(); - - // get HTML string from URI - Response response = await Request(uri); - // handle deep links if (useDeepLinks) { @@ -141,20 +137,29 @@ namespace ReadSharp } // readability - TranscodingResult transcodingResult = ExtractReadableInformation(uri, response.Stream, bodyOnly, noHeadline); - - Encoding encoding = _encoder.GetEncodingFromString(transcodingResult.Charset); - - // extract again if encoding didn't match or failed to retrieve - if (encoding != null && ( - String.IsNullOrEmpty(response.Charset) - || - !String.Equals(response.Charset, transcodingResult.Charset, StringComparison.OrdinalIgnoreCase))) + try { - transcodingResult = ExtractReadableInformation(uri, response.Stream, bodyOnly, noHeadline, encoding); + transcodingResult = ExtractReadableInformation(uri, response.Stream, bodyOnly, noHeadline); + + encoding = _encoder.GetEncodingFromString(transcodingResult.Charset); + + // extract again if encoding didn't match or failed to retrieve + if (encoding != null && ( + String.IsNullOrEmpty(response.Charset) + || + !String.Equals(response.Charset, transcodingResult.Charset, StringComparison.OrdinalIgnoreCase))) + { + transcodingResult = ExtractReadableInformation(uri, response.Stream, bodyOnly, noHeadline, encoding); + } + } + catch (Exception exc) + { + throw new ReadException(exc.Message); } // get images from article + int id = 1; + List images = transcodingResult.Images.Select(image => { Uri imageUri; @@ -162,6 +167,7 @@ namespace ReadSharp return new ArticleImage() { + ID = (id++).ToString(), Uri = imageUri, Title = image.GetAttributeValue("title", null), AlternativeText = image.GetAttributeValue("alt", null) @@ -276,7 +282,7 @@ namespace ReadSharp } catch (HttpRequestException exc) { - throw new Exception(exc.Message, exc); + throw new ReadException(exc.Message, exc); } // validate HTTP response @@ -284,7 +290,7 @@ namespace ReadSharp { string exceptionString = String.Format("Request error: {0} ({1})", response.ReasonPhrase, (int)response.StatusCode); - throw new Exception(exceptionString); + throw new ReadException(exceptionString); } // read response