-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9640ec0
commit ee5ca8c
Showing
4 changed files
with
418 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Define global variables | ||
$global:COUNT = 0 | ||
$global:ROW = $null | ||
|
||
function Get-About { | ||
Write-Host "Random API Example" | ||
Write-Host "" | ||
Write-Host "Platform: $([System.Environment]::OSVersion.VersionString)" | ||
Write-Host "Path: $PSCommandPath" | ||
Write-Host "" | ||
|
||
$message = (Invoke-RestMethod -Uri 'https://random-data-api.com/api/hipster/random_hipster_stuff').sentence | ||
$shortMessage = $message.Substring(0, [Math]::Min($message.Length, 50)) | ||
Write-Host "Message: $shortMessage" | ||
Write-Host "" | ||
} | ||
|
||
function Get-UserCount { | ||
param ( | ||
[string]$Prompt = "Please enter a number of users greater than 1" | ||
) | ||
|
||
$count = Read-Host $Prompt | ||
if ($count -match '^[0-9]+$' -and [int]$count -gt 1) { | ||
$global:COUNT = [int]$count | ||
} else { | ||
Get-UserCount -Prompt "Invalid input `"$count`". Please enter a number of users greater than 1" # Prompt again recursively | ||
} | ||
} | ||
|
||
function Get-UserRow { | ||
$runspaces = @() | ||
$response = Invoke-RestMethod -Uri "https://random-data-api.com/api/v2/users?size=$global:COUNT" | ||
if ($response -eq $null -or $response -eq "") { | ||
Write-Host -NoNewline "`nCould not get server response.`n" | ||
exit 1 | ||
} | ||
|
||
foreach ($user in $response) { | ||
$runspace = [powershell]::Create().AddScript({ | ||
param($id, $name, $phone, $email, $avatar) | ||
$url = $avatar + "?size=32x32" | ||
$outputFile = "R:\$(Split-Path -Leaf $avatar)" | ||
[void](Invoke-WebRequest -Uri $url -OutFile $outputFile) | ||
[PSCustomObject]@{ | ||
Icon = $outputFile | ||
Id = $id | ||
Name = $name | ||
Phone = $phone | ||
Email = $email | ||
} | ||
}) | ||
[void]($runspace.AddArgument($user.id)) | ||
[void]($runspace.AddArgument("$($user.first_name) $($user.last_name)")) | ||
[void]($runspace.AddArgument($user.phone_number)) | ||
[void]($runspace.AddArgument($user.email)) | ||
[void]($runspace.AddArgument($user.avatar.Split('?')[0])) | ||
$runspaces += [PSCustomObject]@{ | ||
Pipe = $runspace; | ||
Status = $runspace.BeginInvoke() | ||
} | ||
} | ||
|
||
$completedRunspaces = 0 | ||
$totalRunspaces = $runspaces.Count | ||
|
||
# Wait for all runspaces to complete and collect results | ||
$userRows = foreach ($runspace in $runspaces) { | ||
$result = $runspace.Pipe.EndInvoke($runspace.Status) | ||
$runspace.Pipe.Dispose() | ||
$result | ||
$completedRunspaces++ | ||
Write-Host -NoNewline "`rLoading users: $completedRunspaces / $totalRunspaces" | ||
} | ||
|
||
$tableHeader = "{0,6} {1,-50} {2,-10} {3,-25}" -f "", "Icon", "Id", "Name" | ||
Write-Host -NoNewline "`n`n$tableHeader`n" | ||
|
||
$userRows | ForEach-Object { | ||
$index = [array]::IndexOf($userRows, $_) + 1 | ||
$tableRow = "{0,6} {1,-50} {2,-10} {3,-25}" -f $index, $_.Icon, $_.Id, $_.Name | ||
Write-Host $tableRow | ||
} | ||
Write-Host "" | ||
|
||
$prompt = "Please enter a row number between 1 and $global:COUNT" | ||
while ($true) { | ||
$row = Read-Host $prompt | ||
if ($row -match '^[0-9]+$' -and [int]$row -ge 1 -and [int]$row -le $global:COUNT) { | ||
$global:ROW = $userRows[[int]$row - 1] | ||
break | ||
} else { | ||
$prompt = "Invalid input `"$row`". Please enter a row number between 1 and $global:COUNT" | ||
} | ||
} | ||
} | ||
|
||
function Show-UserDetail { | ||
$avatarPath = $global:ROW.Icon | ||
[void](Invoke-WebRequest -Uri "https://robohash.org/$($avatarPath -split '/' | Select-Object -Last 1)?size=200x200" -OutFile $avatarPath) | ||
|
||
Write-Host " | ||
User Detail: | ||
------------ | ||
Icon: $($global:ROW.Icon) | ||
ID: $($global:ROW.Id) | ||
Name: $($global:ROW.Name) | ||
Email: $($global:ROW.Email) | ||
Phone: $($global:ROW.Phone) | ||
" | ||
} | ||
|
||
# Main script execution | ||
Get-About | ||
Get-UserCount | ||
Get-UserRow | ||
Show-UserDetail |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/bin/bash | ||
|
||
get_about() { | ||
echo -e "Random API Example" | ||
echo | ||
echo -e "Platform: $(uname -or)" | ||
echo -e "Path: $(readlink -f "${BASH_SOURCE[0]}")" | ||
echo | ||
echo "Message: $(curl -s https://random-data-api.com/api/hipster/random_hipster_stuff | jq -r '.sentence' | cut -c -50)" | ||
} | ||
|
||
get_user_count() { | ||
local prompt="$(echo -e "$1\n\nPlease enter a number of users greater than 1: ")" | ||
read -p "$prompt" count | ||
if [[ $count =~ ^[0-9]+$ ]] && (( count > 1 )); then | ||
echo "$count" | ||
else | ||
get_user_count "Invalid input \"$count\". Please enter a number of users greater than 1." # Prompt again recursively | ||
fi | ||
} | ||
|
||
get_user_row() { | ||
users=$(curl -s "https://random-data-api.com/api/v2/users?size=$COUNT" | | ||
jq -r '.[] | [.id, (.first_name + " " + .last_name), .phone_number, .email, .avatar] | @tsv' | | ||
awk -F '?' '{print $1}' | | ||
parallel -j8 --bar --colsep '\t' 'wget -q -O "/dev/shm/$(basename {5})" {5}?size=32x32 && echo -e "/dev/shm/$(basename {5})|{1}|{2}|{3}|{4}"') | ||
|
||
local table="$(echo -e "Icon|Id|Name\n$users" | awk -F '|' '{printf "%-50s %-10s %-25s\n", $1, $2, $3}' | nl -v0 -w6)" | ||
local prompt="$(echo -e "\n ${table:6}\n\nPlease enter a row number between 1 and $COUNT: ")" | ||
|
||
while true; do | ||
read -p "$prompt" row | ||
if [[ $row =~ ^[0-9]+$ ]] && ((row > 0 && row <= COUNT)); then | ||
echo $(awk "NR==$row" <<< "$users") | ||
break | ||
else | ||
prompt="Invalid input \"$row\". Please enter a row number between 1 and $COUNT: " | ||
fi | ||
done | ||
} | ||
|
||
show_user_detail() { | ||
IFS='|' read -r user_avatar user_id user_name user_phone user_email <<< "${ROW//\'/}" | ||
wget -q -O $user_avatar "https://robohash.org/$(basename "$user_avatar")?size=200x200" && | ||
echo " | ||
User Detail: | ||
------------ | ||
Icon: $user_avatar | ||
ID: $user_id | ||
Name: $user_name | ||
Email: $user_email | ||
Phone: $user_phone | ||
" | ||
} | ||
|
||
COUNT=$(get_user_count "$(get_about)") || exit 1 | ||
ROW=$(get_user_row) || exit 1 | ||
show_user_detail |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# Define global variables | ||
$global:COUNT = 0 | ||
$global:ROW = $null | ||
|
||
function Get-About { | ||
$path = $PSCommandPath.Replace("\", "/") | ||
$message = (Invoke-RestMethod -Uri 'https://random-data-api.com/api/hipster/random_hipster_stuff').sentence | ||
$shortMessage = $message.Substring(0, [Math]::Min($message.Length, 80)).PadRight(80) | ||
"Zenity: $(zenity --version) | ||
Platform: $([System.Environment]::OSVersion.VersionString) | ||
Path: $path | ||
Message: $shortMessage" | ||
} | ||
|
||
function Get-UserCount { | ||
$count = zenity --entry --title="Random API Example" --text="$(Get-About)`n`nPlease enter a number of users greater than 1:" | ||
if ($count -eq $null) { | ||
exit 1 | ||
} elseif ($count -match '^[0-9]+$' -and [int]$count -gt 1) { | ||
$global:COUNT = [int]$count | ||
} else { | ||
Write-Output $count | ||
zenity --error --title="Error" --text="Invalid input `"$count`". Please enter a number of users greater than 1." --width=400 --height=80 | ||
Get-UserCount # Prompt again recursively | ||
} | ||
} | ||
|
||
function Get-Progress-Dialog { | ||
$startInfo = New-Object System.Diagnostics.ProcessStartInfo | ||
$startInfo.FileName = "zenity" | ||
$startInfo.Arguments = "--progress --title=Initializing --percentage=0 --auto-close --width=500" | ||
$startInfo.RedirectStandardInput = $true | ||
$startInfo.UseShellExecute = $false | ||
|
||
$progress = New-Object System.Diagnostics.Process | ||
$progress.StartInfo = $startInfo | ||
$progress.Start() | Out-Null | ||
$progress | ||
} | ||
|
||
function Get-UserRow { | ||
$runspaces = @() | ||
$response = Invoke-RestMethod -Uri "https://random-data-api.com/api/v2/users?size=$global:COUNT" | ||
if ($response -eq $null -or $response -eq "") { | ||
zenity --error --title="Error" --text="Could not get server response." --width=400 --height=80 | ||
exit 1 | ||
} | ||
|
||
foreach ($user in $response) { | ||
$runspace = [powershell]::Create().AddScript({ | ||
param($id, $name, $phone, $email, $avatar) | ||
$url = $avatar + "?size=32x32" | ||
$outputFile = "R:\$(Split-Path -Leaf $avatar)" | ||
[void](Invoke-WebRequest -Uri $url -OutFile $outputFile) | ||
[PSCustomObject]@{ | ||
Icon = $outputFile | ||
Id = $id | ||
Name = $name | ||
Phone = $phone | ||
Email = $email | ||
} | ||
}) | ||
[void]($runspace.AddArgument($user.id)) | ||
[void]($runspace.AddArgument("$($user.first_name) $($user.last_name)")) | ||
[void]($runspace.AddArgument($user.phone_number)) | ||
[void]($runspace.AddArgument($user.email)) | ||
[void]($runspace.AddArgument($user.avatar.Split('?')[0])) | ||
$runspaces += [PSCustomObject]@{ | ||
Pipe = $runspace; | ||
Status = $runspace.BeginInvoke() | ||
} | ||
} | ||
|
||
$completed = 0 | ||
$total = $runspaces.Count | ||
|
||
$progress = Get-Progress-Dialog | ||
$progressInput = $progress.StandardInput | ||
|
||
# Wait for all runspaces to complete and collect results | ||
$user = $runspaces | ForEach-Object { | ||
$user = $_.Pipe.EndInvoke($_.Status) | ||
$_.Pipe.Dispose() | ||
Write-Output $user.Icon $user.Id $user.Name $user.Phone $user.Email | ||
$completed++ | ||
$fileName = Split-Path -Path $user.Icon -Leaf | ||
Write-Output "# $fileName $completed / $total" | ForEach-Object { | ||
$completionPercentage = ($completed / $total) * 100 | ||
$progressInput.WriteLine($_) | ||
$progressInput.WriteLine($completionPercentage) | ||
if ($completionPercentage -eq 100) { | ||
$progressInput.Close() | ||
} | ||
if ($progress.HasExited -and $process.ExitCode -ne 0) { | ||
exit 1 | ||
} | ||
} | ||
} | zenity --list --title="Users" ` | ||
--ok-label=OK ` | ||
--cancel-label=Close ` | ||
--column="Icon" ` | ||
--column="Id" ` | ||
--column="Name" ` | ||
--column="Phone" ` | ||
--column="Email" ` | ||
--hide-column=4,5 ` | ||
--imagelist ` | ||
--print-column="ALL" ` | ||
--mid-search ` | ||
--width=300 ` | ||
--height=400 | ||
|
||
$progressInput.Close() | ||
$progress.WaitForExit() | ||
|
||
if ($user -eq $null -or $user -eq "") { | ||
exit 1 | ||
} else { | ||
$properties = $user.Split('|') | ||
$global:ROW = [PSCustomObject]@{ | ||
Icon = $properties[0] | ||
Id = $properties[1] | ||
Name = $properties[2] | ||
Phone = $properties[3] | ||
Email = $properties[4] | ||
} | ||
} | ||
} | ||
|
||
function Show-UserDetail { | ||
$avatarPath = $global:ROW.Icon | ||
[void](Invoke-WebRequest -Uri "https://robohash.org/$($avatarPath -split '/' | Select-Object -Last 1)?size=200x200" -OutFile $avatarPath) | ||
|
||
$detail = Start-Job -ScriptBlock { | ||
param($id, $name, $email, $phone) | ||
"ID: $id | ||
Name: $name | ||
Email: $email | ||
Phone: $phone" | zenity --text-info --title="User Detail" ` | ||
--font="courier" ` | ||
--width=420 ` | ||
--height=160 | ||
} -ArgumentList $global:ROW.Id, $global:ROW.Name, $global:ROW.Email, $global:ROW.Phone | ||
|
||
$avatar = Start-Job -ScriptBlock { | ||
param($icon) | ||
$icon | zenity --list --title="Avatar" ` | ||
--ok-label=OK ` | ||
--cancel-label=Close ` | ||
--column="Icon" ` | ||
--hide-header ` | ||
--imagelist ` | ||
--print-column="ALL" ` | ||
--width=300 ` | ||
--height=300 | ||
} -ArgumentList $global:ROW.Icon | ||
|
||
Receive-Job -Job $detail -Wait | Out-Null | ||
Receive-Job -Job $avatar -Wait | Out-Null | ||
} | ||
|
||
# Main script execution | ||
Get-UserCount | ||
Get-UserRow | ||
Show-UserDetail |
Oops, something went wrong.