WPF私的ライブラリ一覧

Code Library

スクレピング
<#
.SYNOPSIS
meaywork.netに掲載されているライブラリへのアクセスツール
.EXAMPLE

#>

$uri = "https://maywork.net/wpf-maywork-wpf-library/"
$html = Invoke-WebRequest -Uri $uri


$items = [regex]::Matches(
    $html.Content,
     '<li[^>]*>.*?</li>',
    'Singleline'
)

$snippets = foreach ($item in $items) {
    $name = [regex]::Match($item.Value, 'data-name="([^"]+)"').Groups[1].Value
    $desc = [regex]::Match($item.Value, 'class="desc"\>([^\<]+)').Groups[1].Value
    $url  = [regex]::Match($item.Value, 'href="([^"]+)"').Groups[1].Value
    if ($name -eq "") { continue }

    [PSCustomObject]@{
        Name = $name
        Desc = $desc
        Url  = $url
    }
}

# GUIで選択
$selected = $snippets | Out-GridView -Title "サイトを選択" -PassThru

if ($selected) {

    $Url = $selected.Url
    $Name = $selected.Name
    $OutputDir = "."

    # HTML取得
    $response = Invoke-WebRequest -Uri $Url -UseBasicParsing
    $html = $response.Content

    # HTMLエンコード解除用
    Add-Type -AssemblyName System.Web


    # すべての code ブロック取得
    $codeBlocks = [regex]::Matches(
        $html,
        '<pre.*?<code.*?>(.*?)</code>.*?</pre>',
        [System.Text.RegularExpressions.RegexOptions]::Singleline
    )

    if ($codeBlocks.Count -eq 0) {
        Write-Host "No code blocks found."
        return
    }

    foreach ($block in $codeBlocks) {

        $rawCode = $block.Groups[1].Value
        $decoded = [System.Web.HttpUtility]::HtmlDecode($rawCode)


        # C#クラス名検出
        if ($decoded -match 'class\s+([A-Za-z_][A-Za-z0-9_]*)') {

            $className = $matches[1]
            $fileName  = "$className.cs"

            if ($className.ToLower() -eq $Name.ToLower()) {
                $path = Join-Path $OutputDir $fileName

                Set-Content -Path $path -Value $decoded -Encoding UTF8

                Write-Host "Saved: $fileName"
            }
        }
    }
}

スクリーンショット