Skip to content

Commit

Permalink
Started doing the scraper. Unfinished.
Browse files Browse the repository at this point in the history
  • Loading branch information
driis committed Dec 8, 2012
1 parent 6a285ef commit 60d8e17
Show file tree
Hide file tree
Showing 8 changed files with 733 additions and 4 deletions.
@@ -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);
}
}
}
629 changes: 629 additions & 0 deletions MobileVehicleInspection.Api.Tests/Library/valid.html

Large diffs are not rendered by default.

Expand Up @@ -77,18 +77,25 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Library\DanishTransportAuthorityResponseParserTests.cs" />
<Compile Include="Library\RegistrationNumberTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<EmbeddedResource Include="Library\valid.html" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MobileVehicleInspection.Api\MobileVehicleInspection.Api.csproj">
<Project>{27D3B211-901F-47CC-BD18-DA61FA6D900D}</Project>
<Name>MobileVehicleInspection.Api</Name>
</ProjectReference>
<ProjectReference Include="..\MobileVehicleInspection.Contracts\MobileVehicleInspection.Contracts.csproj">
<Project>{BF6263C1-7790-449C-8803-FAE5CE6AB42A}</Project>
<Name>MobileVehicleInspection.Contracts</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
Expand Down
@@ -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();
}
}
}
@@ -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 MobileVehicleInspection.Api/Library/IVehicleInspectionLookup.cs
@@ -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; }
}
}
Expand Up @@ -65,6 +65,9 @@
<Reference Include="ServiceStack.Text">
<HintPath>..\packages\ServiceStack.Text.3.9.29\lib\net35\ServiceStack.Text.dll</HintPath>
</Reference>
<Reference Include="SgmlReaderDll">
<HintPath>..\packages\SgmlReader.1.8.8\lib\4.0\SgmlReaderDll.dll</HintPath>
</Reference>
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
Expand All @@ -86,6 +89,8 @@
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Library\DanishTransportAuthorityResponseParser.cs" />
<Compile Include="Library\DanishTransportAuthorityScraper.cs" />
<Compile Include="Library\Guard.cs" />
<Compile Include="Library\IVehicleInspectionLookup.cs" />
<Compile Include="Library\RegistrationNumber.cs" />
Expand Down
1 change: 1 addition & 0 deletions MobileVehicleInspection.Api/packages.config
Expand Up @@ -5,4 +5,5 @@
<package id="ServiceStack.OrmLite.SqlServer" version="3.9.28" targetFramework="net45" />
<package id="ServiceStack.Redis" version="3.9.29" targetFramework="net45" />
<package id="ServiceStack.Text" version="3.9.29" targetFramework="net45" />
<package id="SgmlReader" version="1.8.8" targetFramework="net45" />
</packages>

0 comments on commit 60d8e17

Please sign in to comment.