Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Prompt for how many cat pictures we're going to retrieve
- $cats = Read-Host -Prompt 'Input how many cats you want'
- #start the loop
- For ($i=1; $i -le $cats; $i++)
- {
- #set the filename. This will be useful
- $Filename = "Cat $i"
- #store the file with a temporary name
- Invoke-WebRequest "https://api.thecatapi.com/api/images/get?format=src&results_per_page=1" -OutFile "$Filename"
- #open the file as binary so we can check the header to determine the file type
- $a = [System.IO.File]::ReadAllBytes("$Filename");
- #read the relevant bits for GIF
- $header = "{0:X2}" -f $a[0]
- $header += "{0:X2}" -f $a[1]
- $header += "{0:X2}" -f $a[2]
- #check if the header says it's a GIF
- If ($header -eq "474946") {
- #If it is, tell the user the filename and move the temporary file to the correct name
- Write-Host "$Filename.gif"
- Move-Item -Path $Filename -Destination "$Filename.gif"
- }
- #read the relevant bits for JPG (JFIF)
- $header = "{0:X2}" -f $a[6]
- $header += "{0:X2}" -f $a[7]
- $header += "{0:X2}" -f $a[8]
- $header += "{0:X2}" -f $a[9]
- #check if the header says "JFIF"
- If ($header -eq "4A464946") {
- #If it is, tell the user the filename and move the temporary file to the correct name
- Write-Host "$Filename.jpg"
- Move-Item -Path $Filename -Destination "$Filename.jpg"
- }
- #read the relevant bits for PNG
- $header = "{0:X2}" -f $a[1]
- $header += "{0:X2}" -f $a[2]
- $header += "{0:X2}" -f $a[3]
- #check if the header says "PNG"
- If ($header -eq "504E47") {
- #If it is, tell the user the filename and move the temporary file to the correct name
- Write-Host "$Filename.png"
- Move-Item -Path $Filename -Destination "$Filename.png"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement