Open1

Powershell Html Client

Otogawa Katsutoshi(oto)Otogawa Katsutoshi(oto)

HtmlAgilityPackを使う。

function Search-MavenPlugin {

    # if you not Add-Type HtmlGility, Add-Type.`
    if ($null -eq ( [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.Location -match "HtmlAgilityPack/1.11.62/lib/netstandard2.0/HtmlAgilityPack.dll" } )) {

        $packageId = "HtmlAgilityPack"
        $version = "1.11.62"
        Add-Type -Path ".nupkg/$packageId/$version/lib/netstandard2.0/HtmlAgilityPack.dll"
    }

    $Uri = "https://maven.apache.org/plugins/"

    # PluginName

    $UseBasicParsing = ""
    if ($PSVersion.Major -eq 5 -and $PSVersion.Minor -ge 1) {
        $UseBasicParsing = "-UseBasicParsing"
    } elseif ($PSVersion.Major -ge 7) {
        # Default parsing

    } else {
        # $PSVersion 5.0 before version is not supported.
        Write-Error 
        # throw Error
    }


    $response = Invoke-Expression "Invoke-WebRequest -Uri $Uri $UseBasicParsing"

    # HtmlAgilityPackのHtmlDocumentオブジェクトを作成V
    $doc = [HtmlAgilityPack.HtmlDocument]::new()

    # HTMLをロード
    $doc.LoadHtml($response.Content)

    ## tableを取得。
    $tables = $doc.DocumentNode.SelectNode("//table")

    $xml = [xml]($tables[3].OuterHtml)

    # 
    $xml.table.tr | ForEach-Object { $_.th}


    # 検索結果のリストが出る。
    $requestBody = @{

        q="quickstart";
        wt="json"
    }
# g:org.apache.maven.plugins
# -join +AND+で検索追加`。
# rowsのデフォルトは20
    $requestBody = @{
        q="tags:plugin";
        wt="json";
        fq="tags:maven";
        rows=100;
    }

    # maven plugin
    $requestBody = @{
        q="p:maven-plugin";
        wt="json";
        rows=100;
    }

    # maven-archetype
    # 
    $requestBody = @{
        q="p:maven-archetype";
        wt="json";
        rows=100;
    }

    # -Packaging Pom, Jar, War, MavenPlugin, MavenArchetype
    $requestBody = @{
        q="p:pom";
        wt="json";
        rows=100;
    }

    $requestBody = @{
        q="tags:plugin";
        wt="json";
        fq="tags:gradle";
        rows=100;
    }



    # 下のようにできない。
    # https://maven.pkg.github.com
    # $requestBody = @{
    #     q="repositoryId:com.github";
    #     wt="json";
    #     rows=100;
    # }
    #repositoryId:central
    $response = Invoke-WebRequest -Body $requestBody -Uri "https://search.maven.org/solrsearch/select"

    # 200
    $response.StatusCode

    $responseJson = $response.Content | ConvertFrom-Json
    
    # json出力。
    $responseJson.response.docs
    # # タイトルを取得
    # $title = $doc.DocumentNode.SelectSingleNode("//title").InnerText
    # Write-Output "Title: $title"

    # # ヘッダーを取得
    # $header = $doc.DocumentNode.SelectSingleNode("//h1").InnerText
    # Write-Output "Header: $header"

    # $xml = [xml]::new()
    # Mavenでしか触っていないxmlのパースを正しくできないので、Xpathが正しく動作しない。

    $xml = [xml] ($response.Content -split "`n" | Select-Object -Skip 1)

    #bodyColumn > section > section:nth-child(4) > table > tbody
    $xml.html.body.div.div.main.section.section.table

    # supported by Maven Plugin table show.
    $MavenPluginTable = $xml.html.body.div.div.main.section.section[0].table
    
    # header
    $MavenPluginTable.tr.th

    $MavenPluginTable 

    $MavenPluginTable.tr.td
}