forked from Gerenios/AADInternals
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OneNote.ps1
140 lines (121 loc) · 4.54 KB
/
OneNote.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Speaks out the given text.
# Feb 22nd 2022
function Start-Speech
{
<#
.SYNOPSIS
Gets mp3 stream of the given text and plays it with Media player.
.DESCRIPTION
Gets mp3 stream of the given text using learning tools API and plays it with Media player.
.Parameter AccessToken
The access token used to get the speech.
.Parameter Language
The language code. Defaults to "en-US"
.Parameter PreferredVoice
Male or Female voice, defaults to Female.
.Parameter Text
The text to speak.
.Example
PS C:\>Get-AADIntAccessTokenForOneNote -SaveToCache
PS C:\>Start-AADIntSpeech -Text "Three Swedish switched witches watch three Swiss Swatch watch switches. Which Swedish switched witch watch which Swiss Swatch watch switch?" -Language "en-GB" -PreferredVoice Male
.Example
PS C:\>Get-AADIntAccessTokenForOneNote -SaveToCache
PS C:\>Start-AADIntSpeech -Text "Mustan kissan paksut posket" -PreferredVoice Female -Language fi-FI
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$AccessToken,
[Parameter(Mandatory=$True)]
[String]$Text,
[Parameter(Mandatory=$False)]
[String]$Language = "en-US",
[Parameter(Mandatory=$False)]
[ValidateSet("Female","Male")]
[String]$PreferredVoice = "Female"
)
Process
{
# Get from cache if not provided
$AccessToken = Get-AccessTokenFromCache -AccessToken $AccessToken -Resource "https://onenote.com" -ClientId "1fec8e78-bce4-4aaf-ab1b-5451cc387264"
# Construct the body
$body = @{
"data" = [ordered]@{
"title" = "The King's Speech"
"chunks" = @(
[ordered]@{
"content" = $Text
"mimeType" = "text/plain"
}
)
"startingChunkIndex" = 0
"startingCharIndex" = 0
}
}
# Set the headers
$headers=@{
"Authorization" = "Bearer $AccessToken"
"Accept-Language" = $Language
"MS-Int-AppId" = "Teams"
}
# Invoke the command
$contentModel = Invoke-RestMethod -UseBasicParsing -Method Post -Uri "https://learningtools.onenote.com/learningtoolsapi/v2.0/getcontentmodelforreader" -Headers $headers -Body ($body | ConvertTo-Json -Depth 5) -ContentType "application/json; charset=utf-8"
# Construct the body
$body = [ordered]@{
"data" = [ordered]@{
"sentenceModels" = @(
[ordered]@{
"t" = $Text
"lang" = $Language
"se" = $contentModel.data[0].formatting.b.r[0].i[0]
"wo" = @()
}
)
}
"options" = [ordered]@{
"preferredVoice" = $PreferredVoice
"extractWordMarkers" = $True
"encoding" = "Wav"
"clientLabel" = "ReadAloudFirstPrefetch"
"useBrowserSpecifiedDialect" = $True
}
}
# Set the headers
$headers=@{
"Authorization" = "MS-SessionToken $($contentModel.meta.sessionToken)"
"X-UserSessionId" = $contentModel.meta.sessionId
"Accept-Language" = $Language
"MS-Int-AppId" = "Teams"
}
# Invoke the command
$response = Invoke-RestMethod -UseBasicParsing -Method Post -Uri "https://learningtools.onenote.com/learningtoolsapi/v2.0/GetSpeech" -Headers $headers -Body ($body | ConvertTo-Json -Depth 5) -ContentType "application/json"
$mp3B64 = $response.data.sb[0].ad.Split(",")[1]
# Create a temporary file
$tmp = New-TemporaryFile
Rename-Item -Path $tmp.FullName -NewName ($tmp.Name+".mp3")
$mp3 = ($tmp.FullName+".mp3")
try
{
Set-BinaryContent -Path $mp3 -Value [byte[]](Convert-B64ToByteArray -B64 $mp3B64)
$player = [System.Windows.Media.MediaPlayer]::new()
$player.Open($mp3)
# Pause for a while to populate the duration
Start-Sleep -Milliseconds 100
$player.Play()
# Wait till completed
while($player.Position -lt $player.NaturalDuration.TimeSpan)
{
Start-Sleep -Milliseconds 10
}
$player.Close()
}
catch
{
}
finally
{
# Remove the temp file
Remove-Item $mp3
}
}
}