Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active June 14, 2018 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/b121eb1c1400d961aae426bb0c974e59 to your computer and use it in GitHub Desktop.
Save stknohg/b121eb1c1400d961aae426bb0c974e59 to your computer and use it in GitHub Desktop.
はてなブログのエントリ一覧を取得する雑なスクリプト
#
# 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