はてなブックマークを削除、編集、取得するスクリプト
ブックマークの削除
指定したIDのブックマークを削除する。
Remove-Bookmark.ps1
param([string]$userName, [string]$password, [string]$id) if($userName.Length -eq 0 -or $password.Length -eq 0 -or $id.Length -eq 0 -or $args[0] -eq "-?") { $commandName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) Write-Host -foregroundColor Yellow @" Name: $commandName Description: はてなブックマークから指定したIDのブックマークを削除します。 Usage: $commandName [[-userName] <string>] [[-password] <string>] [[-id] <string>] -userName <string> ユーザ名 -password <string> パスワード -id <string> ブックマークのID "@ exit 1 } $webReq = [Net.HttpWebRequest]::Create("http://b.hatena.ne.jp/atom/edit/$id") $webReq.Headers.Add("X-WSSE", (Get-WsseHeader $userName $password)) $webReq.Method = "DELETE" $webRes = $webReq.GetResponse() Write-Debug $webRes.StatusCode $webRes.Close()
使い方
PS > Remove-Bookmark ユーザ名 パスワード id
ブックマークの編集
指定したIDのブックマークの情報を編集する。タイトルとコメントを変更可、指定するのはどちらか一方だけでもいい。
Set-Bookmark.ps1
param([string]$userName, [string]$password, [string]$id, [string]$title, [string]$summary) if($userName.Length -eq 0 -or $password.Length -eq 0 -or $id.Length -eq 0 -or $args[0] -eq "-?") { $commandName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) Write-Host -foregroundColor Yellow @" Name: $commandName Description: 指定したIDのはてなブックマークの情報を設定します。 Usage: $commandName [[-userName] <string>] [[-password] <string>] [[-id] <string>] [[-title] <string>] [[-summary] <string>] -userName <string> ユーザ名 -password <string> パスワード -id <string> ブックマークのID -title <string> ブックマークのタイトル -summary <string> コメントとかタグ "@ exit 1 } $reqUrl = "http://b.hatena.ne.jp/atom/edit/$id" $webReq = [Net.HttpWebRequest]::Create($reqUrl) $webReq.Headers.Add("X-WSSE", (Get-WsseHeader $userName $password)) $webReq.ContentType = "application/xatom+xml, application/xml, text/xml, */*" $webReq.Method = "PUT" # PUTと同時に認証情報を送信するので、100-Continueを待機する必要はない。 $curSp = [Net.ServicePointManager]::FindServicePoint($reqUrl) if($curSp -ne $null) { $curSp.Expect100Continue = $false } $content = [string](&{@" <entry xmlns="http://purl.org/atom/ns#"> "@ if($title.Length -gt 0) {@" <title>$title</title> "@} if($summary.Length -gt 0) {@" <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 $webRes.Close()
使い方
PS > Set-Bookmark ユーザ名 パスワード id タイトル コメント
ブックマークの取得
指定したIDのブックマークの情報を取得する。特定のブックマークの情報だけを取得したい時に使用する。
Get-Bookmark.ps1
param([string]$userName, [string]$password, [string]$id) if($userName.Length -eq 0 -or $password.Length -eq 0 -or $id.Length -eq 0 -or $args[0] -eq "-?") { $commandName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name) Write-Host -foregroundColor Yellow @" Name: $commandName Description: はてなブックマークから指定したIDのブックマークの情報を取得します。 Usage: $commandName [[-userName] <string>] [[-password] <string>] [[-id] <string>] -userName <string> ユーザ名 -password <string> パスワード -id <string> ブックマークのID "@ exit 1 } $webReq = [Net.HttpWebRequest]::Create("http://b.hatena.ne.jp/atom/edit/$id") $webReq.Headers.Add("X-WSSE", (Get-WsseHeader $userName $password)) $webRes = $webReq.GetResponse() Write-Debug $webRes.StatusCode $sr = New-Object IO.StreamReader($webRes.GetResponseStream()) $result = [xml]$sr.ReadToEnd() $sr.Close() $webRes.Close() $result.entry
使い方
PS > Get-Bookmark ユーザ名 パスワード id generator : generator issued : 2008-03-13T08:12:24+09:00 title : InfoQ: Add-In FrameworkにPipline Builderを導入 id : tag:hatena.ne.jp,2005:bookmark-coma2n-7851087 dc : http://purl.org/dc/elements/1.1/ summary : summary link : {link, link, link} xmlns : http://purl.org/atom/ns# subject : .NET author : author
以上、終わり。
なるほど、これがRESTful Webサービスというやつか、勉強になった。