Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started doing the scraper. Unfinished.
- Loading branch information
Showing
8 changed files
with
733 additions
and
4 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
MobileVehicleInspection.Api.Tests/Library/DanishTransportAuthorityResponseParserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.IO; | ||
using MobileVehicleInspection.Api.Library; | ||
using MobileVehicleInspection.Contracts; | ||
using NUnit.Framework; | ||
|
||
namespace MobileVehicleInspection.Api.Tests.Library | ||
{ | ||
public class DanishTransportAuthorityResponseParserTests | ||
{ | ||
public const string ValidMarkup = ""; | ||
|
||
[Test] | ||
public void Parse_ValidInput_ReturnsVehicle() | ||
{ | ||
var sut = new DanishTransportAuthorityResponseParser(); | ||
string valid; | ||
using (var reader = new StreamReader(GetType().Assembly.GetManifestResourceStream(GetType(), "valid.html"))) | ||
valid = reader.ReadToEnd(); | ||
|
||
Vehicle result = sut.Parse(valid); | ||
Assert.Inconclusive(); | ||
Assert.AreEqual(new RegistrationNumber("XK95962"), result.RegistrationNumber); | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
MobileVehicleInspection.Api/Library/DanishTransportAuthorityResponseParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using MobileVehicleInspection.Contracts; | ||
|
||
namespace MobileVehicleInspection.Api.Library | ||
{ | ||
public class DanishTransportAuthorityResponseParser | ||
{ | ||
public Vehicle Parse(string markup) | ||
{ | ||
var reader = new Sgml.SgmlReader(); | ||
reader.InputStream = new StringReader(markup); | ||
XDocument doc = XDocument.Load(reader); | ||
//var keys = doc.Root.Descendants("div").Where(x => x.Attribute("class").) | ||
return new Vehicle(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
MobileVehicleInspection.Api/Library/DanishTransportAuthorityScraper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using MobileVehicleInspection.Contracts; | ||
|
||
namespace MobileVehicleInspection.Api.Library | ||
{ | ||
public class DanishTransportAuthorityScraper : IVehicleInspectionLookup | ||
{ | ||
private readonly ScraperSettings _settings; | ||
private readonly DanishTransportAuthorityResponseParser _parser; | ||
|
||
public DanishTransportAuthorityScraper(ScraperSettings settings, DanishTransportAuthorityResponseParser parser) | ||
{ | ||
_settings = settings; | ||
_parser = parser; | ||
} | ||
|
||
public async Task<Vehicle> ByRegistration(RegistrationNumber registration) | ||
{ | ||
var client = new WebClient(); | ||
var url = new Uri(String.Format(_settings.UrlTemplate, registration)); | ||
string result = null; | ||
try | ||
{ | ||
result = await client.DownloadStringTaskAsync(url); | ||
} | ||
catch (WebException wex) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format("Unable to retrieve data from {0}, {1}, HTTP {2}", url, wex.Status.ToString(), | ||
wex.Response == null ? -1 : (int) ((HttpWebResponse) wex.Response).StatusCode), wex); | ||
} | ||
|
||
return _parser.Parse(result); | ||
} | ||
} | ||
} |
11 changes: 8 additions & 3 deletions
11
MobileVehicleInspection.Api/Library/IVehicleInspectionLookup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
using MobileVehicleInspection.Contracts; | ||
using System.Threading.Tasks; | ||
using MobileVehicleInspection.Contracts; | ||
|
||
namespace MobileVehicleInspection.Api.Library | ||
{ | ||
public interface IVehicleInspectionLookup | ||
{ | ||
Vehicle ByRegistration(RegistrationNumber registration); | ||
Vehicle ByVin(VehicleIdentificationNumber vin); | ||
Task<Vehicle> ByRegistration(RegistrationNumber registration); | ||
} | ||
|
||
public class ScraperSettings | ||
{ | ||
public string UrlTemplate { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters