diff --git a/PocketSharp.Tests/ReadTests.cs b/PocketSharp.Tests/ReadTests.cs index 417d653..ed891d3 100644 --- a/PocketSharp.Tests/ReadTests.cs +++ b/PocketSharp.Tests/ReadTests.cs @@ -8,17 +8,39 @@ namespace PocketSharp.Tests { public class ReadTests : TestsBase { - public ReadTests() : base() { } + private PocketReader reader; + + + public ReadTests() : base() + { + reader = new PocketReader(); + } [Fact] - public async Task TemporaryReadTest() + public async Task ReadArticleTest() { - PocketReader reader = new PocketReader(); + PocketArticle result = await reader.Read(new PocketItem() + { + ID = 99, + Uri = new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation") + }); - string result = await reader.Read(new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation")); + Assert.True(result.Content.Length > 15000); + } - Assert.True(result.Length > 15000); + + [Fact] + public async Task ReadArticleWithInvalidUriTest() + { + await ThrowsAsync(async () => + { + await reader.Read(new PocketItem() + { + ID = 99, + Uri = new Uri("http://frontendplayyyyy.com") + }); + }); } } } \ No newline at end of file diff --git a/PocketSharp/Models/PocketArticle.cs b/PocketSharp/Models/PocketArticle.cs new file mode 100644 index 0000000..89ce37a --- /dev/null +++ b/PocketSharp/Models/PocketArticle.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace PocketSharp.Models +{ + /// + /// Readable article + /// + public class PocketArticle + { + /// + /// Gets or sets the pocket item ID. + /// + /// + /// The pocket item ID. + /// + public int PocketItemID { get; set; } + + /// + /// Gets or sets the content. + /// + /// + /// The content. + /// + public string Content { get; set; } + + /// + /// Gets or sets the title. + /// + /// + /// The title. + /// + public string Title { get; set; } + + /// + /// Gets or sets the next page URL. + /// + /// + /// The next page URL. + /// + public string NextPageUrl { get; set; } + } +} diff --git a/PocketSharp/PocketReader.cs b/PocketSharp/PocketReader.cs index 6def6bc..fdf6df0 100644 --- a/PocketSharp/PocketReader.cs +++ b/PocketSharp/PocketReader.cs @@ -15,7 +15,7 @@ namespace PocketSharp /// /// REST client used for HTML retrieval /// - protected readonly HttpClient _restClient; + protected readonly HttpClient _httpClient; /// @@ -23,13 +23,22 @@ namespace PocketSharp /// public PocketReader() { - // initialize REST client - _restClient = new HttpClient(new HttpClientHandler() + // initialize HTTP client + _httpClient = new HttpClient(new HttpClientHandler() { - AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip + AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, + AllowAutoRedirect = true, + MaxAutomaticRedirections = 10 }); - _restClient.DefaultRequestHeaders.Add("Accept", "text/html"); + // add accept types + _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); + + // add accepted encodings + _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip,deflate"); + + // add user agent (default for Opera with PocketSharp identifier appended) + _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.4 Safari/537.36 OPR/18.0.1284.2 PocketSharp/2.0"); } @@ -39,19 +48,8 @@ namespace PocketSharp /// /// The pocket item. /// - public async Task Read(PocketItem item) - { - return await Read(item.Uri); - } - - - - /// - /// Reads content from the given URI. - /// - /// The URI. - /// - public async Task Read(Uri uri) + /// + public async Task Read(PocketItem item) { // initialize transcoder NReadabilityTranscoder transcoder = new NReadabilityTranscoder( @@ -64,12 +62,12 @@ namespace PocketSharp ); // get HTML string from URI - string htmlResponse = await Request(uri); + string htmlResponse = await Request(item.Uri); // set properties for processing TranscodingInput transcodingInput = new TranscodingInput(htmlResponse) { - Url = uri.ToString(), + Url = item.Uri.ToString(), DomSerializationParams = new DomSerializationParams() { BodyOnly = true, @@ -84,7 +82,13 @@ namespace PocketSharp // process/transcode HTML TranscodingResult transcodingResult = transcoder.Transcode(transcodingInput); - return transcodingResult.ExtractedContent; + return new PocketArticle() + { + Content = transcodingResult.ExtractedContent, + Title = transcodingResult.ExtractedTitle, + NextPageUrl = transcodingResult.NextPageUrl, + PocketItemID = item.ID + }; } @@ -92,42 +96,27 @@ namespace PocketSharp /// /// Fetches a resource /// + /// The URI. + /// protected async Task Request(Uri uri) { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri); // make async request - HttpResponseMessage response = await _restClient.SendAsync(request); + HttpResponseMessage response = await _httpClient.SendAsync(request); // validate HTTP response - ValidateResponse(response); + if (response.StatusCode != HttpStatusCode.OK) + { + string exceptionString = String.Format("Request error: {0} ({1})", response.ReasonPhrase, (int)response.StatusCode); + + throw new PocketException(exceptionString); + } // read response var responseString = await response.Content.ReadAsStringAsync(); return responseString; } - - - /// - /// Validates the response. - /// - /// The response. - /// - /// - /// Error retrieving response - /// - protected void ValidateResponse(HttpResponseMessage response) - { - // no error found - if (response.StatusCode == HttpStatusCode.OK) - { - return; - } - - string exceptionString = String.Format("Request error: {0} ({1})", response.ReasonPhrase, (int)response.StatusCode); - - throw new PocketException(exceptionString); - } } } \ No newline at end of file diff --git a/PocketSharp/PocketSharp.csproj b/PocketSharp/PocketSharp.csproj index 0855366..aa37969 100644 --- a/PocketSharp/PocketSharp.csproj +++ b/PocketSharp/PocketSharp.csproj @@ -41,6 +41,7 @@ +