PowerShellでTwitterのTimelineを取得

WikipediaのWindows PowerShellの項にあったRSSフィードを取得するサンプルを見ながら作ってみた。

いろいろ面倒なので、Twitter API 1.0のXML版を使用。認証不要のAPIということで、指定したユーザのタイムラインを取得するAPI(/statuses/user_timeline.xml)を選択した。

実験環境はWindows XP上のPowerShell 2.0だが、PowerShell 3.0ではJSONをパースするコマンドレットが追加されているので、認証さえできればTwitter API 1.1にも対応できるかと。

コード

$Url = "https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=(ユーザ名)"
$tl = [XML.XmlDocument](New-Object Net.WebClient).DownloadString($Url)
$tl.statuses.status | ft -AutoSize @{Label="Date"; Expression={[DateTime]::ParseExact($_.created_at, "ddd MMM dd HH:mm:ss K yyyy", [Globalization.DateTimeFormatInfo]::InvariantInfo)}}, text

結果

Date                text
----                ----
2013/02/05 19:09:17 PowerShellがなかなか遊べそうな予感… http://t.co/QdsGXm2C
<<PowerShell PowerShellとEpgDataCap_Bonの連携>>