Last active
February 19, 2022 10:07
-
-
Save satob/a3acad49b61b4cbba54c6126f6b5a4cb to your computer and use it in GitHub Desktop.
howmのファイル(1ファイル1トピック)をOneNoteへ移行
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See https://developer.microsoft.com/ja-jp/graph/graph-explorer?request=me/onenote/notebooks&version=v1.0 , https://docs.microsoft.com/ja-jp/graph/onenote-create-page | |
$AuthorizationToken = "get.jwt.token"; | |
$JsonHeaders = @{ "Content-type" = "application/json"; Accept = "application/json"; Authorization = "Bearer " + $AuthorizationToken; } | |
$XhtmlHeaders = @{ "Content-type" = "application/xhtml+xml"; Accept = "application/json"; Authorization = "Bearer " + $AuthorizationToken; } | |
$ErrorActionPreference = "Stop" | |
$ErrorActionPreference = "Ignore" | |
$CreateNotebookUrl = "https://graph.microsoft.com/v1.0/me/onenote/notebooks" | |
$Body = [System.Text.Encoding]::UTF8.GetBytes('{ "displayName": "howm" }') | |
$Notebook = (Invoke-RestMethod -Method Post -Headers $JsonHeaders -Body $Body -Uri $CreateNotebookUrl) | |
$ErrorActionPreference = "Stop" | |
$GetNotebooksUrl = "https://graph.microsoft.com/v1.0/me/onenote/notebooks" | |
$Notebooks = (Invoke-RestMethod -Method Get -Headers $JsonHeaders -Uri $GetNotebooksUrl) | |
$NotebookId = ($Notebooks.value | Where-Object { $_.displayName -eq "howm" }).id | |
$ErrorActionPreference = "Ignore" | |
$CreateSectionUrl = "https://graph.microsoft.com/v1.0/me/onenote/notebooks/${NotebookId}/sections" | |
$Body = [System.Text.Encoding]::UTF8.GetBytes('{ "displayName": "howm" }') | |
$Section = (Invoke-RestMethod -Method Post -Headers $JsonHeaders -Body $Body -Uri $CreateSectionUrl) | |
$ErrorActionPreference = "Stop" | |
$GetSectionUrl = "https://graph.microsoft.com/v1.0/me/onenote/sections" | |
$Sections = (Invoke-RestMethod -Method Get -Headers $JsonHeaders -Uri $GetSectionUrl) | |
$SectionId = ($Sections.value | Where-Object { $_.displayName -eq "howm" }).id | |
# 最初に以下を実行してタイトルのないファイルを避けておく | |
# Get-ChildItem C:\path\to\howm -Recurse | Where-Object { $_.Name -match "^20.*txt$" } | ForEach-Object { $FileName = $_.FullName; $Content = (Get-Content $FileName -Encoding UTF8); try { ($Content | Select-Object -First 1).Substring(2) > $null } catch { $FileName } } | |
$HtmlTemplate = @' | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>{0}</title> | |
<meta name="created" content="{1}" /> | |
</head> | |
<body> | |
<p>{2}</p> | |
</body> | |
</html> | |
'@; | |
$CreatePageUrl = "https://graph.microsoft.com/v1.0/me/onenote/sections/${SectionId}/pages" | |
# $MaxConcurrentJobs = 10 | |
# $Runspace = [runspacefactory]::CreateRunspacePool(1,$maxConcurrentJobs) | |
# $Runspace.Open() | |
Get-ChildItem C:\path\to\howm -Recurse | Sort-Object ` | |
| Where-Object { $_.Name -match "^20.*txt$" } ` | |
| ForEach-Object { | |
# | Select-Object -First 3 | ForEach-Object { | |
# There is no out-of-the-box way to slice the input of Foreach-Object | |
# https://stackoverflow.com/a/59289500/3902663 | |
# $ps = [powershell]::Create() | |
# $ps.RunspacePool = $Runspace | |
$File = $_; | |
$FileName = $_.FullName; | |
$CreationDateTime = [DateTime]::ParseExact($File.Name.Substring(0,17), "yyyy-MM-dd-HHmmss", $null).ToString("yyyy-MM-ddTHH:mm:ss.0000000"); | |
# Is there way to set update datetime of a note? | |
$UpdateDateTime = $_.LastWriteTime.ToString("yyyy-MM-ddTHH:mm:ss.0000000"); | |
$Content = (Get-Content -Encoding UTF8 -Path $FileName); | |
$HtmlTitle = ($Content | Select-Object -First 1).Substring(2); | |
$HtmlBody = ($Content | Select-Object -Skip 2); | |
$Html = $HtmlTemplate -F $HtmlTitle, $CreationDateTime, ($HtmlBody -join '<br>') | |
Write-Host $HtmlTitle | |
# Stop when the authorization token is invalidated | |
$Result = Invoke-RestMethod -Method Post -Headers $XhtmlHeaders -Body ([System.Text.Encoding]::UTF8.GetBytes($Html)) -Uri $CreatePageUrl -ErrorAction Stop | |
Remove-Item $FileName | |
# [void]$ps.AddCommand("Invoke-WebRequest").AddParameter("Method","Post").AddParameter("Headers", $XhtmlHeaders).AddParameter("Body", [System.Text.Encoding]::UTF8.GetBytes($Html)).AddParameter("Uri", $CreatePageUrl) | |
# [void]$ps.BeginInvoke() | |
Start-Sleep -m 300 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment