はてなブックマークを投稿するスクリプト
お次は、はてなブックマークにブックマークを投稿するスクリプト。
New-Bookmark.ps1
param([string]$userName, [string]$password, [string]$url, [string]$summary) if($userName.Length -eq 0 -or $password.Length -eq 0 -or $url.Length -eq 0 -or $args[0] -eq "-?") { $commandName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) Write-Host -foregroundColor Yellow @" Name: $commandName Description: 指定したURLをはてなブックマークでブクマします。 Usage: $commandName [[-userName] <string>] [[-password] <string>] [[-url] <string>] [[-summary] <string>] -userName <string> ユーザ名 -password <string> パスワード -url <string> ブックマークするURL -summary <string> コメントとかタグ Output: 投稿したブックマークのID "@ exit 1 } $postURL = "http://b.hatena.ne.jp/atom/post" $webReq = [Net.HttpWebRequest]::Create($postURL) $webReq.Headers.Add("X-WSSE", (Get-WsseHeader $userName $password)) $webReq.ContentType = "application/xatom+xml, application/xml, text/xml, */*" $webReq.Method = "POST" # POSTと同時に認証情報を送信するので、100-Continueを待機する必要はない。 $curSp = [Net.ServicePointManager]::FindServicePoint($postURL) if($curSp -ne $null) { $curSp.Expect100Continue = $false } # titleは入れても意味ないので省く $content = @" <entry xmlns="http://purl.org/atom/ns#"> <title></title> <link rel="related" type="text/html" href="$url" /> <summary type="text/plain">$summary</summary> </entry> "@ Write-Debug $content $content = [Text.Encoding]::UTF8.GetBytes($content) $webReq.ContentLength = $content.Length $req = $webReq.GetRequestStream() $req.Write($content, 0, $content.Length) $req.Close() $webRes = $webReq.GetResponse() Write-Debug $webRes.StatusCode $sr = New-Object IO.StreamReader($webRes.GetResponseStream()) $result = [xml]$sr.ReadToEnd() $sr.Close() $webRes.Close() $id = $result.entry.id # tag:hatena.ne.jp,2005:bookmark-ユーザ名-xxxxxxx ←最後のこの部分だけがID $id.Substring($id.LastIndexOf("-") + 1)
使い方
PS > New-Bookmark ユーザ名 パスワード http://b.hatena.ne.jp/coma2n [.NET]hoge xxxxxxx
成功するとブックマークのIDが返ってくる。このIDは削除とか編集するのに必要になる。
ブクマのタイトルは指定しても効かなかったので、このスクリプトでは指定できないようにしてある。