1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| Param( [ValidateSet('en-US', 'de-DE', 'en-AU', 'en-CA', 'en-NZ', 'en-UK', 'ja-JP', 'zh-CN')][string]$locale = 'zh-CN', [int]$files = 0, [ValidateSet('auto', '1024x768', '1280x720', '1366x768', '1920x1080')][string]$resolution = 'auto', [string]$downloadFolder = "$([Environment]::GetFolderPath("MyPictures"))\Bing-Wallpapers" )
[int]$maxItemCount = [System.Math]::max(1, [System.Math]::max($files, 8))
[string]$hostname = "https://www.bing.com" [string]$uri = "$hostname/HPImageArchive.aspx?format=xml&idx=0&n=$maxItemCount&mkt=$locale"
if ($resolution -eq 'auto') { Add-Type -AssemblyName System.Windows.Forms $primaryScreen = [System.Windows.Forms.Screen]::AllScreens Where-Object {$_.Primary -eq 'True'} if ($primaryScreen.Bounds.Width -le 1024) { $resolution = '1024x768' } elseif ($primaryScreen.Bounds.Width -le 1280) { $resolution = '1280x720' } elseif ($primaryScreen.Bounds.Width -le 1366) { $resolution = '1366x768' } else { $resolution = '1920x1080' } }
if (!(Test-Path $downloadFolder)) { New-Item -ItemType Directory $downloadFolder } $request = Invoke-WebRequest -Uri $uri [xml]$content = $request.Content $items = New-Object System.Collections.ArrayList foreach ($xmlImage in $content.images.image) { [datetime]$imageDate = [datetime]::ParseExact($xmlImage.startdate, 'yyyyMMdd', $null) [string]$imageUrl = "$hostname$($xmlImage.urlBase)_$resolution.webp" $item = New-Object System.Object $item Add-Member -Type NoteProperty -Name date -Value $imageDate $item Add-Member -Type NoteProperty -Name url -Value $imageUrl $null = $items.Add($item) }
if (!($files -eq 0) -and ($items.Count -gt $files)) { $items = $itemsSort-Object date while ($items.Count -gt $files) { $null, $items = $items } } Write-Host "Downloading images..." $client = New-Object System.Net.WebClient $imgsArr = @() foreach ($item in $items) { $baseName = $item.date.ToString("yyyy-MM-dd") $destination = "$downloadFolder\$baseName.webp" $url = $item.url $imgsArr = $imgsArr + $destination if (!(Test-Path $destination)) { Write-Debug "Downloading image to $destination" $client.DownloadFile($url, "$destination") } } if ($files -gt 0) { Write-Host "Cleaning the directory..." $i = 1 Get-ChildItem -Filter "????-??-??.webp" $downloadFolder Sort-Object -Descending FullName ForEach-Object { if ($i -gt $files) { $fileName = $_.FullName Write-Debug "Removing file $fileName" Remove-Item "$fileName" } $i++ } }
Add-Type @" using System; using System.Runtime.InteropServices; using Microsoft.Win32; namespace Wallpaper { public enum Style : int { Tile, Center, Stretch, NoChange } public class Setter { public const int SetDesktopWallpaper = 20; public const int UpdateIniFile = 0x01; public const int SendWinIniChange = 0x02; [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); public static void SetWallpaper ( string path, Wallpaper.Style style ) { SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile SendWinIniChange ); RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true); switch( style ) { case Style.Stretch : key.SetValue(@"WallpaperStyle", "2") ; key.SetValue(@"TileWallpaper", "0") ; break; case Style.Center : key.SetValue(@"WallpaperStyle", "1") ; key.SetValue(@"TileWallpaper", "0") ; break; case Style.Tile : key.SetValue(@"WallpaperStyle", "1") ; key.SetValue(@"TileWallpaper", "1") ; break; case Style.NoChange : break; } key.Close(); } } } "@ $today = Get-Date (Get-Date).AddDays(-1) -UFormat "%Y-%m-%d" $lastPicDate = Get-Date $items[0].date -UFormat "%Y-%m-%d"
switch ($lastPicDate) { $today { $Wallpaper = "$downloadFolder\" + $today + '.webp' break } Default { $imgsArr = $imgsArr Sort-Object {Get-Random} $Wallpaper = $imgsArr[0] break } }
[Wallpaper.Setter]::SetWallpaper( $Wallpaper, 1 )
|