Excite翻訳で英語を日本語に翻訳するスクリプト

Excite翻訳で英語を日本語に翻訳するスクリプトを作ってみた。

Eng-ToJapan.ps1
param([string]$word)

if($word.Length -eq 0 -or $args[0] -eq "-?") {
    $commandName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
	Write-Host @"

名前
    $commandName

概要
    指定した英語をExcite翻訳を使って日本語に翻訳します。

構文
    $commandName [-word] <string>

注意
    Import-Assembly.ps1が必要です。

"@ -foregroundColor Yellow
    exit 1
}
[void](Import-Assembly System.Web)

$postData = [Text.Encoding]::ASCII.GetBytes(
    ("before={0}" -f [Web.HttpUtility]::UrlEncode($word))
)
$webReq = [Net.HttpWebRequest]::Create("http://excite.co.jp/world/english")
$webReq.Method = "POST"
$webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"
$webReq.ContentType = "application/x-www-form-urlencoded"
$webReq.ContentLength = $postData.Length

$reqStream = $webReq.GetRequestStream()
$reqStream.Write($postData, 0, $postData.Length)
$reqStream.Close()

$webRes = $webReq.GetResponse()

$sr = New-Object IO.StreamReader(
    $webRes.GetResponseStream(), [Text.Encoding]::GetEncoding("Shift-JIS")
)
if($sr.ReadToEnd().Replace([Environment]::NewLine, "") -match '<textarea.*name=\"after\".*>(?<value>.*)</textarea>') { $matches["value"] }

$sr.Close()

使い方

PS > Eng-ToJapan Hello

こんにちは