From 4e57b0f6ad1de78aeac2c8139a9eea7ceaaf4406 Mon Sep 17 00:00:00 2001 From: neutmute Date: Thu, 11 Oct 2018 17:05:49 +1100 Subject: [PATCH 1/2] #1 cache online results --- Convertto-TextASCIIArt.ps1 | 71 ++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/Convertto-TextASCIIArt.ps1 b/Convertto-TextASCIIArt.ps1 index 866a03e..f09e0e8 100644 --- a/Convertto-TextASCIIArt.ps1 +++ b/Convertto-TextASCIIArt.ps1 @@ -41,11 +41,11 @@ .EXAMPLE PS C:\>.\Convertto-TextASCIIArt -Text '# This !' - �� �� ������ �� �� �� ���� �� - ���������� �� �� �� �� �� �� - �� �� �� ������ �� �� �� - ���������� �� �� �� �� �� - �� �� �� �� �� �� ���� �� + �� �� ������ �� �� �� ���� �� + ���������� �� �� �� �� �� �� + �� �� �� ������ �� �� �� + ���������� �� �� �� �� �� + �� �� �� �� �� �� ���� �� Shows local font on the script not internet required #> @@ -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) { @@ -419,16 +448,30 @@ 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 + + if (Test-Path $cacheFile) + { + $testEncode = Get-Content $cacheFile } - catch { - $errMessage = "Check your internet connection, Verify below url in browser`n" - $errMessage += $url - Write-Host $errMessage -BackgroundColor DarkRed + else { + $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 + $WebsiteApi.Content | Out-File $cacheFile + } + catch { + $ErrorMessage = $_.Exception.Message + $errMessage = "Check your internet connection, Verify below url in browser`n $ErrorMessage`n" + $errMessage += $url + Write-Host $errMessage -BackgroundColor DarkRed + } } } #Online } #switch pscmdlet From a1db728238cf34902e3f5d3e5d406806428fc891 Mon Sep 17 00:00:00 2001 From: neutmute Date: Fri, 12 Oct 2018 21:08:57 +1100 Subject: [PATCH 2/2] fix caching bug --- Convertto-TextASCIIArt.ps1 | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Convertto-TextASCIIArt.ps1 b/Convertto-TextASCIIArt.ps1 index f09e0e8..4e0a212 100644 --- a/Convertto-TextASCIIArt.ps1 +++ b/Convertto-TextASCIIArt.ps1 @@ -453,18 +453,18 @@ Process { $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) { - $testEncode = Get-Content $cacheFile + $asciiArtText = [System.IO.File]::ReadAllText($cacheFile) } else { - $testEncode = [uri]::EscapeDataString($Text) - $url = "http://artii.herokuapp.com/make?text=$testEncode&font=$FontName" + $encodedText = [uri]::EscapeDataString($Text) + $url = "http://artii.herokuapp.com/make?text=$encodedText&font=$FontName" Try { $WebsiteApi = Invoke-WebRequest -Uri $url -ErrorAction Stop - Write-Host $WebsiteApi.Content -ForegroundColor $FontColor $WebsiteApi.Content | Out-File $cacheFile + $asciiArtText = $WebsiteApi.Content } catch { $ErrorMessage = $_.Exception.Message @@ -473,6 +473,8 @@ Process { Write-Host $errMessage -BackgroundColor DarkRed } } + + Write-Host $asciiArtText -ForegroundColor $FontColor } #Online } #switch pscmdlet }