Search code examples
powershellwindows-10

How do I get the copyright info from the Bing daily wallpaper JSON?


I have a powershell script that grabs the Bing daily image, and saves it on my pc as "bing.bmp":

irm "bing.com$((irm "bing.com/HPImageArchive.aspx?format=js&mkt=en-IN&n=1").images[1].url)" -OutFile bing.bmp

And that works fine. I would also like to pull the image description from the same file it is pulling the url from. The value is called "copyright", and I can't seem to get it. I tried this:

irm "bing.com$((irm "bing.com/HPImageArchive.aspx?format=js&mkt=en-IN&n=1").images[1].copyright)" -OutFile bing.txt

but it didn't work. Is there any way I can get the value "copyright" from the JSON file, and output it as a .txt file?

EDIT: This is the JSON I am trying to pull the value from:

https://bing.com/HPImageArchive.aspx?format=js&mkt=en-IN&n=1


Solution

  • You can try something like this, first query the API to get the details for today's wallpaper and store that response in a variable for future use. Then you could use the Title from the JSON object to create a new folder where you can save the wallpaper as well as the Copyright details.

    $json = Invoke-RestMethod "bing.com/HPImageArchive.aspx?format=js&mkt=en-IN&n=1"
    $title = $json.images.Title
    $folder = New-Item "Today's Title - $title" -ItemType Container
    $copyrightfile = Join-Path $folder -ChildPath "Copyright.txt"
    $wallpaperfile = Join-Path $folder -ChildPath "$title.bmp"
    $json.images.CopyRight | Out-File $copyrightfile
    Invoke-RestMethod "bing.com$($json.images.url)" -OutFile $wallpaperfile
    

    One thing to note, (...).images[1].url should be just (...).images.url from what I could see.