ID for ArticleImage; implement ReadException
This commit is contained in:
@@ -59,7 +59,7 @@ namespace ReadSharp.Tests
|
||||
[Fact]
|
||||
public async Task ReadArticleWithInvalidUriTest()
|
||||
{
|
||||
await ThrowsAsync<Exception>(async () =>
|
||||
await ThrowsAsync<ReadException>(async () =>
|
||||
{
|
||||
await reader.Read(new Uri("http://frontendplayyyyy.com"));
|
||||
});
|
||||
|
||||
@@ -7,6 +7,14 @@ namespace ReadSharp
|
||||
/// </summary>
|
||||
public class ArticleImage
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the identifier.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The identifier.
|
||||
/// </value>
|
||||
public string ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the URI.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
|
||||
namespace ReadSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// ReadSharp exception
|
||||
/// </summary>
|
||||
public class ReadException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReadException"/> class.
|
||||
/// </summary>
|
||||
public ReadException()
|
||||
: base() { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReadException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The message that describes the error.</param>
|
||||
public ReadException(string message)
|
||||
: base(message) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReadException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The error message that explains the reason for the exception.</param>
|
||||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
|
||||
public ReadException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
}
|
||||
}
|
||||
+28
-22
@@ -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<ArticleImage> images = transcodingResult.Images.Select<XElement, ArticleImage>(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
|
||||
|
||||
Reference in New Issue
Block a user