Console app to generate progress report almost there, needs to generate Markdown and deal with missing Lastfm documentation

See #28
This commit is contained in:
Rikki Tooley 2014-10-21 00:18:50 +01:00
parent 46ce11c52c
commit a47d601bfb
8 changed files with 285 additions and 2 deletions

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30501.0
VisualStudioVersion = 12.0.30723.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IF.Lastfm.Core.Tests", "src\IF.Lastfm.Core.Tests\IF.Lastfm.Core.Tests.csproj", "{EC1526C6-AE22-4F12-8640-F9613DFCCE1A}"
EndProject
@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IF.Lastfm.Demo.Apollo", "sr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IF.Lastfm.Core", "src\IF.Lastfm.Core\IF.Lastfm.Core.csproj", "{3CF4B78F-8B48-49CB-942F-83DB13474A4F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IF.Lastfm.ProgressReport", "src\IF.Lastfm.ProgressReport\IF.Lastfm.ProgressReport.csproj", "{96DE7C3C-F439-484B-A5B5-39A729119BD6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -61,6 +63,14 @@ Global
{3CF4B78F-8B48-49CB-942F-83DB13474A4F}.Release|Any CPU.Build.0 = Release|Any CPU
{3CF4B78F-8B48-49CB-942F-83DB13474A4F}.Release|ARM.ActiveCfg = Release|Any CPU
{3CF4B78F-8B48-49CB-942F-83DB13474A4F}.Release|x86.ActiveCfg = Release|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Debug|ARM.ActiveCfg = Debug|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Debug|x86.ActiveCfg = Debug|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Release|Any CPU.Build.0 = Release|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Release|ARM.ActiveCfg = Release|Any CPU
{96DE7C3C-F439-484B-A5B5-39A729119BD6}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -6,10 +6,16 @@
namespace IF.Lastfm.Core.Api.Commands
{
internal abstract class LastAsyncCommandBase<T> : IAsyncCommand<T> where T : LastResponse, new()
/// <summary>
/// Having this type makes reflection easier - there probably isn't any other need for it
/// </summary>
internal abstract class LastAsyncCommandBase
{
public string Method { get; protected set; }
}
internal abstract class LastAsyncCommandBase<T> : LastAsyncCommandBase, IAsyncCommand<T> where T : LastResponse, new()
{
public Uri Url { get; protected set; }
public ILastAuth Auth { get; protected set; }

View File

@ -9,6 +9,7 @@
using System.Text;
[assembly: InternalsVisibleTo("IF.Lastfm.Core.Tests")]
[assembly: InternalsVisibleTo("IF.Lastfm.ProgressReport")]
namespace IF.Lastfm.Core
{
public class LastFm

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{96DE7C3C-F439-484B-A5B5-39A729119BD6}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>IF.Lastfm.ProgressReport</RootNamespace>
<AssemblyName>IF.Lastfm.ProgressReport</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack">
<HintPath>..\..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\packages\Newtonsoft.Json.6.0.5\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IF.Lastfm.Core\IF.Lastfm.Core.csproj">
<Project>{3cf4b78f-8b48-49cb-942f-83db13474a4f}</Project>
<Name>IF.Lastfm.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,138 @@
using HtmlAgilityPack;
using IF.Lastfm.Core.Api.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
namespace IF.Lastfm.ProgressReport
{
/// <summary>
/// Scrapes Last.fm/api for available methods, compares to what commands are in IF.Lastfm.Core and generates some Markdown.
/// Maybe some images if I'm feeling adventurous
/// </summary>
public class Program
{
public const string API_INTRO_PAGE = "http://www.last.fm/api/intro";
public static void Main(string[] args)
{
// scrape Last.fm API documentation
var apiGroup = GetApiMethods();
if (apiGroup == null)
{
return;
}
// reflect on Last.fm assembly to find all implemented commands
var allImplemented = GetImplementedCommands().ToList();
foreach (var group in apiGroup)
{
var apiGroupName = group.Key;
var implemented = allImplemented.Where(m => m.StartsWith(apiGroupName.ToLowerInvariant(), StringComparison.Ordinal)).ToList();
var matches = group.Value.Intersect(implemented);
var notImplemented = group.Value.Except(implemented);
var secret = implemented.Except(group.Value);
// TODO got all the information, now need to generate something!
}
}
#region Scrape
/// <summary>
/// Scrape the API documentation for all the method names
/// </summary>
private static Dictionary<string, IEnumerable<string>> GetApiMethods()
{
var client = new HttpClient();
var response = client.GetAsync(API_INTRO_PAGE);
response.Wait();
if (!response.Result.IsSuccessStatusCode)
{
Console.WriteLine("Server returned {0} fetching {1}\n{2}",
response.Result.StatusCode, API_INTRO_PAGE, response.Result.ReasonPhrase);
Console.ReadLine();
return null;
}
var html = response.Result.Content.ReadAsStringAsync();
html.Wait();
var doc = new HtmlDocument();
doc.LoadHtml(html.Result);
var wspanel = doc.DocumentNode.SelectNodes("//ul[@class='wspanel']").LastOrDefault();
if (wspanel == null)
{
Console.WriteLine("Couldn't parse HTML");
Console.ReadLine();
return null;
}
// each package is a section of the API
var packages = wspanel.Descendants().Where(li => HasClass(li, "package"));
var allMethods = new Dictionary<string, IEnumerable<string>>();
foreach (var package in packages)
{
var h3 = package.Element("h3");
var ul = package.Element("ul");
var methodLinks = ul.SelectNodes("child::li");
var methods = methodLinks.Select(a => a.InnerText);
allMethods.Add(h3.InnerText, methods);
}
return allMethods;
}
private static bool HasClass(HtmlNode stay, string classy)
{
return stay.Attributes.Contains("class") && stay.Attributes["class"].Value.Contains(classy);
}
#endregion
#region Reflect
/// <summary>
/// With thanks to Tim Murphy
/// http://stackoverflow.com/a/4529684/268555
/// </summary>
public static IEnumerable<Type> FindSubClassesOf(Type type)
{
var assembly = type.Assembly;
return assembly.GetTypes().Where(t => t.IsSubclassOf(type));
}
/// <summary>
/// Reflect on implemented commands
/// </summary>
private static IEnumerable<string> GetImplementedCommands()
{
var types = FindSubClassesOf(typeof(LastAsyncCommandBase)).Where(t => t.IsClass && !t.IsAbstract);
var methods = types.Select(GetApiMethodFromCommandType);
return methods;
}
private static string GetApiMethodFromCommandType(Type type)
{
var constructor = type.GetConstructors().First();
var parameters = constructor.GetParameters();
var arguments = new object[parameters.Count()]; // to keep reflection happy
var instance = (LastAsyncCommandBase)Activator.CreateInstance(type, arguments);
return instance.Method;
}
#endregion
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("IF.Lastfm.ProgressReport")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("IF.Lastfm.ProgressReport")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a3216c3d-c470-425d-a680-1ec006773152")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />
</packages>