-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTry-Catch.ps1
89 lines (75 loc) · 3.07 KB
/
Try-Catch.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
<# $key = 'trnsl.1.1.20191009T073247Z.a7caab90b20cb300.074ab5d0767aeb7a647af56ccb8f50be100d1ae4' #API-ключ
$bin = "$([Environment]::GetFolderPath('UserProfile'))\yatrans.bin"
Add-Type -AssemblyName System.Security
[IO.File]::WriteAllBytes(
$bin,
[Security.Cryptography.ProtectedData]::Protect(
[Text.Encoding]::Unicode.GetBytes($key),
$null,
[Security.Cryptography.DataProtectionScope]::CurrentUser
))
attrib +h $bin
#>
function Get-Translation {
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$Data,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$Language,
[int]$attempts=3,
[int]$sleepInSeconds=5
)
begin {
Add-Type -AssemblyName System.Security
# декодирование ключа
if (Test-Path ($key = "$([Environment]::GetFolderPath('UserProfile'))\yatrans.bin")) {
$key = [Text.Encoding]::Unicode.GetString(
[Security.Cryptography.ProtectedData]::Unprotect(
[IO.File]::ReadAllBytes($key),
$null,
[Security.Cryptography.DataProtectionScope]::CurrentUser
)
)
}
else {
throw 'API-ключ Яндекс.Перевод не найден.'
}
# url-root
$url = 'https://translate.yandex.net/api/v1.5/tr/'
# определение языка оригинала
$detect = "$($url)detect?key=$key&text="
# перевод
$transl = "$($url)translate?key=$key&text=%t&lang=%l-$Language&format=plain"
# user agent
$usr = 'Mozilla/5.0 (Windows NT 6.3; rv:37.0.1) Gecko/20100101 Firefox/37.0.1'
$foregroundcolor = "Yellow", "Red", "Black","White", "Green", "Cyan", "Blue"
}
process {
# для перевода текстовых файлов
$res = [xml](wget "$detect$Data" -DisableKeepAlive -UseBasicParsing -UserAgent $usr).Content
$transl = $transl -replace '%t', $Data
$transl = $transl -replace '%l', $res.DetectedLang.lang
do
{
try{
$res = [xml](wget $transl -DisableKeepAlive -UseBasicParsing -UserAgent $usr).Content
}
catch {Write-host "Sorry.... Something wrong with proccess :(
\\\\\\\\ //|''''|
| - - | \ /
( a a ) / \
| L || |
\ == / | |
_.\____/._\ |
|| || /
© Tonya Uhlianets
" -foregroundcolor $(get-random $foregroundcolor)}
$attempts--
if ($attempts -gt 0) { sleep $sleepInSeconds }
} while ($attempts -gt 0)
$res.Translation.text
}
}
Get-Translation