Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airreg.ps

(view source code of airreg.ps as plain text)

  1. param (
  2. 	[parameter( Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "Enter a 5 or six character airplane registration code:" )]
  3. 	[string]$reg,
  4. 	[switch]$OpenWebPage,
  5. 	[parameter( ValueFromRemainingArguments = $true, ValueFromPipeline = $false )]
  6. 	[object]$invalidArgs = ""
  7. )
  8.  
  9. $OldProgressPreference = $ProgressPreference
  10.  
  11. function Show-Help {
  12. 	param ( [string]$error = "" )
  13. 	if ( $error -ne "" ) {
  14. 		Write-Host
  15. 		Write-Host "Error:`t" -ForegroundColor Red -NoNewline
  16. 		Write-Host $error
  17. 	}
  18.  
  19. 	Write-Host
  20. 	Write-Host "AirReg.ps1,  Version 1.01"
  21. 	Write-Host "Search an airplane's type and model by its registration"
  22. 	Write-Host
  23. 	Write-Host "Usage:  AirReg.ps1  reg  [ -OpenWebPage ]"
  24. 	Write-Host
  25. 	Write-Host "Where:  reg            is the airplane registration `"number`", e.g. `"G-FLUG`""
  26. 	Write-Host "        -OpenWebPage   opens Airport-data.com's web page for the airplane,"
  27. 	Write-Host "                       if " -ForegroundColor White -NoNewline
  28. 	Write-Host "it exists"
  29. 	Write-Host
  30. 	Write-Host "Notes:  The script uses Airport-data.com's API to find the airplane's data."
  31. 	Write-Host "        The author of this script is not affiliated in any way with"
  32. 	Write-Host "        Airport-data.com."
  33. 	Write-Host "        The script accepts piped input for the registration `"number`", but"
  34. 	Write-Host "        not for the -OpenWebPage switch."
  35. 	Write-Host "        If multiple registrations are passed on the command line, the script"
  36. 	Write-Host "        will abort with an error message. If multiple registrations are piped,"
  37. 	Write-Host "        however, the script will handle only the last one and ignore the rest."
  38. 	Write-Host
  39. 	Write-Host "Written by Rob van der Woude"
  40. 	Write-Host "http://www.robvanderwoude.com"
  41.  
  42. 	Exit 1
  43. }
  44.  
  45.  
  46. # Check if the mandatory registration "number" isn't empty
  47. if ( $reg.Trim( ) -eq "" ) {
  48. 	Show-Help
  49. }
  50.  
  51.  
  52. # Check if too many arguments were passed on the command line or piped
  53. if ( $invalidArgs -ne "" ) {
  54. 	if ( $invalidArgs -eq "/?" ) {
  55. 		Show-Help
  56. 	} else {
  57. 		Show-Help "Invalid or too many command line argument(s)"
  58. 	}
  59. }
  60.  
  61.  
  62. function Open-URL {
  63. 	param( [string]$url )
  64. 	if ( $HOME[0] -eq "/" ) {
  65. 		# Linux
  66. 		$browser = ( ( xdg-settings get default-web-browser ) -Split "\." )[0]
  67. 		# Redirect standard output to hide "Vector smash protection is enabled" message
  68. 		Start-Process -FilePath $browser -ArgumentList $url -RedirectStandardOutput NUL
  69. 	} else {
  70. 		# Windows
  71. 		Start-Process $url
  72. 	}   
  73. }
  74.  
  75.  
  76. function Get-Model {
  77. 	param ( [string]$reg = "", [string]$url = "" )
  78. 	$reg   = $reg.ToUpper( ).Trim( )
  79. 	$url   = $url.Trim( )
  80. 	$model = ""
  81. 	if ( ( $reg -ne "" ) -and ( $url -ne "" ) ) {
  82. 		$ProgressPreference = "SilentlyContinue"
  83. 		$html  = ( Invoke-WebRequest -URI $url ).Content
  84. 		$ProgressPreference = $OldProgressPreference
  85. 		$regex = [regex]"[\?\&]field=model[\&\`"][^>]*>([^<]+)<"
  86. 		$match = $regex.Match( $html )
  87. 		$model = $match.Groups[1].ToString( )
  88. 	}
  89. 	$model
  90. }
  91.  
  92.  
  93. function Search-PlaneByReg {
  94. 	param ( [string]$reg )
  95. 	$model  = ""
  96. 	$ProgressPreference = "SilentlyContinue"
  97. 	$result = ( Invoke-webrequest -URI "http://www.airport-data.com/api/ac_thumb.json?r=$reg" ).Content | ConvertFrom-Json -ErrorAction Stop
  98. 	$ProgressPreference = $OldProgressPreference
  99. 	if ( $result.status -eq 200 ) {
  100. 		$url = $result.data.link
  101. 		$model = Get-Model -reg $reg -url $url
  102. 		if ( $OpenWebPage ) {
  103. 			Open-URL $url
  104. 		}
  105. 	}
  106. 	$model
  107. }
  108.  
  109.  
  110. $reg = $reg.ToUpper( ).Trim( )
  111. if ( $reg -eq "/?" ) {
  112. 	Show-Help
  113. }
  114. if ( ( $reg.Replace( "-", "" ).Length -lt 5 ) -or ( $reg.Replace( "-", "" ).Length -gt 6 ) ) {
  115. 	Show-Help "Invalid registration code `"$reg`""
  116. }
  117.  
  118. try {
  119. 	$model = Search-PlaneByReg -reg $reg
  120. 	if ( $model -eq "" ) {
  121. 		$reg   = $reg.Replace( "-", "" )
  122. 		$reg   = $reg.Substring( 0, 1 ) + "-" + $reg.Substring( 1 )
  123. 		$model = Search-PlaneByReg -reg $reg
  124. 	} else {
  125. 		Write-Host "$reg`t" -NoNewline
  126. 	}
  127. 	if ( $model -eq "" ) {
  128. 		$reg   = $reg.Replace( "-", "" )
  129. 		$reg   = $reg.Substring( 0, 2 ) + "-" + $reg.Substring( 2 )
  130. 		$model = Search-PlaneByReg -reg $reg
  131. 	} else {
  132. 		Write-Host "$reg`t" -NoNewline
  133. 	}
  134. 	if ( $model -eq "" ) {
  135. 		Show-Help
  136. 	} else {
  137. 		Write-Host "$reg`t" -NoNewline
  138. 	}
  139. }
  140. catch {
  141. 	Show-Help $_.Exception.Message
  142. }
  143.  
  144. Write-Host $model
  145.  

page last modified: 2024-04-16; loaded in 0.0105 seconds