Skip to content

#1 cache online results #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 59 additions & 14 deletions Convertto-TextASCIIArt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
.EXAMPLE
PS C:\>.\Convertto-TextASCIIArt -Text '# This !'

�� �� ������ �� �� �� ���� ��
���������� �� �� �� �� �� ��
�� �� �� ������ �� �� ��
���������� �� �� �� �� ��
�� �� �� �� �� �� ���� ��
�� �� ������ �� �� �� ���� ��
���������� �� �� �� �� �� ��
�� �� �� ������ �� �� ��
���������� �� �� �� �� ��
�� �� �� �� �� �� ���� ��

Shows local font on the script not internet required
#>
Expand Down Expand Up @@ -301,6 +301,35 @@ Begin {
<#0#> Write-Host " " -BackgroundColor $FontColor -NoNewline; Write-Host " " -NoNewline; Write-Host " " -BackgroundColor $FontColor -NoNewline; Write-Host " "
<#0#> Write-Host " " -NoNewline; Write-Host $(" " * 3) -BackgroundColor $FontColor -NoNewline; Write-Host " "}
}#if


# https://stackoverflow.com/a/45884020/1141876
function Get-FNVHash {

param(
[string]$InputString
)

# Initial prime and offset chosen for 32-bit output
# See https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
[uint32]$FNVPrime = 16777619
[uint32]$offset = 2166136261

# Convert string to byte array, may want to change based on input collation
$bytes = [System.Text.Encoding]::UTF8.GetBytes($InputString)

# Copy offset as initial hash value
[uint32]$hash = $offset

foreach($octet in $bytes)
{
# Apply XOR, multiply by prime and mod with max output size
$hash = $hash -bxor $octet
$hash = $hash * $FNVPrime % [System.Math]::Pow(2,32)
}
return $hash
}

}
Process {
switch ($PsCmdlet.ParameterSetName) {
Expand Down Expand Up @@ -419,17 +448,33 @@ Process {
if ($text -eq '# This is test !') {
$text = 'http://vcloud-lab.com'
}
$testEncode = [uri]::EscapeDataString($Text)
$url = "http://artii.herokuapp.com/make?text=$testEncode&font=$FontName"
Try {
$WebsiteApi = Invoke-WebRequest -Uri $url -ErrorAction Stop
Write-Host $WebsiteApi.Content -ForegroundColor $FontColor

$textHash = Get-FNVHash $Text
$cacheFolder = Join-Path $env:APPDATA "asciiart"
$cacheFile = Join-Path $cacheFolder "$textHash.txt"
New-Item -ItemType Directory -Force -Path $cacheFolder | Out-Null
$asciiArtText = ""
if (Test-Path $cacheFile)
{
$asciiArtText = [System.IO.File]::ReadAllText($cacheFile)
}
catch {
$errMessage = "Check your internet connection, Verify below url in browser`n"
$errMessage += $url
Write-Host $errMessage -BackgroundColor DarkRed
else {
$encodedText = [uri]::EscapeDataString($Text)
$url = "http://artii.herokuapp.com/make?text=$encodedText&font=$FontName"
Try {
$WebsiteApi = Invoke-WebRequest -Uri $url -ErrorAction Stop
$WebsiteApi.Content | Out-File $cacheFile
$asciiArtText = $WebsiteApi.Content
}
catch {
$ErrorMessage = $_.Exception.Message
$errMessage = "Check your internet connection, Verify below url in browser`n $ErrorMessage`n"
$errMessage += $url
Write-Host $errMessage -BackgroundColor DarkRed
}
}

Write-Host $asciiArtText -ForegroundColor $FontColor
} #Online
} #switch pscmdlet
}
Expand Down