Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
はてなブログのエントリ一覧を取得する雑なスクリプト
#
# AtomPub APIを使うバージョン
#
# 設定
# 面倒なのでBasic認証
$hatenaId = 'はてなID'
$blogId = 'ブログID'
$cred = Get-Credential -UserName $hatenaId
# エントリ取得(下書き含む)
$entries = &{
function GetXmlContent ([string]$Uri, [PSCredential]$Credential) {
$result = [PSCustomObject]@{
Content = $null;
NextUri = $null;
}
$ret = Invoke-WebRequest -Uri $Uri -Credential $Credential
if ($ret.StatusCode -ne 200) {
return $result
}
$xml = [xml]$ret.Content
$result.Content = $xml.feed.entry
$result.NextUri = $xml.feed.link | Where-Object rel -eq next | Select-Object -ExpandProperty href
return $result
}
$result = GetXmlContent -Uri "https://blog.hatena.ne.jp/$hatenaId/$blogId/atom/entry" -Credential $cred
Write-Output $result.Content
while (-not [string]::IsNullOrEmpty($result.NextUri)) {
$result = GetXmlContent -Uri $result.NextUri -Credential $cred
Write-Output $result.Content
}
}
# 適当に出力
$entries | ForEach-Object {
[PSCustomObject]@{
Title = $_.Title;
Url = $_.link | Where-Object rel -eq alternate | Select-Object -ExpandProperty href;
IsDraft = $_.control.draft -eq 'yes';
#Content = $_.content.'#text';
}
} | Format-List
#
# sitemap.xmlを辿るバージョン
# こっちの方が楽だった...
#
$url = 'https://blog.shibata.tech/'
$res = Invoke-WebRequest -Uri ([IO.Path]::Combine($url, 'sitemap.xml'))
if ($res.StatusCode -ne 200) {
return
}
# sitemapの場合
Write-Output ([xml]$res.Content).urlset.url.loc
# sitemap indexの場合
foreach ($loc in ([xml]$res.Content).sitemapindex.sitemap.loc) {
$res2 = Invoke-WebRequest -Uri $loc
if ($res2.StatusCode -ne 200) {
continue
}
Write-Output ([xml]$res2.Content).urlset.url.loc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment