forked from timothymctim/Bing-wallpapers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-BingImageOfTheDay.ps1
More file actions
248 lines (218 loc) · 10.7 KB
/
Copy pathGet-BingImageOfTheDay.ps1
File metadata and controls
248 lines (218 loc) · 10.7 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Get the Bing "Image of the Day"
#
# Forked from <https://github.com/timothymctim/Bing-wallpapers>
#
# Copyright (c) 2015 Tim van de Kamp, 2025-6 Richard Burte
# License: MIT license
<#
.SYNOPSIS
Fetch the Bing image of the day, or multiple from the archive.
.DESCRIPTION
Fetch the Bing image of the day. Can download previous day images as well. Skips downloading existing images.
.PARAMETER Locale
Specifies the locale of the image to download. By using the value `'auto'`, Bing will attempt to determine an applicable locale based on your IP address.
Default value: "auto".
.PARAMETER Count
The number of wallpapers to download. 1 is the most recent.
Default value: 3.
.PARAMETER Delete
Whether the script will delete all wallpapers not in the last $Count number when sorted.
.PARAMETER Resolution
Specify the size of the file. Select from the set, or use "auto" to detect.
Default is "auto".
.PARAMETER Path
Specify the location where wallpapers will be downloaded.
Default: Wallpaper folder in your Pictures folder.
.PARAMETER FromAlternateSource
Use an alternative, non-official endpoint enumerating Bing wallpaper information. Contains many more image files than the official endpoint.
Default: False, use Bing endpoint
.INPUTS
None. You can't pipe objects to Get-BingImageOfTheDay.
.OUTPUTS
Writes images to specified path.
.LINK
https://github.com/arebee/Bing-wallpapers-x-plat
#>
function Get-BingImageOfTheDay {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
# Get the Bing image of this country
[ValidateSet(
'auto', 'ar-XA', 'bg-BG', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH',
'de-DE', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-ID', 'en-IE',
'en-IN', 'en-MY', 'en-NZ', 'en-PH', 'en-SG', 'en-US', 'en-XA',
'en-ZA', 'es-AR', 'es-CL', 'es-ES', 'es-MX', 'es-US', 'es-XL',
'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-CH', 'fr-FR', 'he-IL',
'hr-HR', 'hu-HU', 'it-IT', 'ja-JP', 'ko-KR', 'lt-LT', 'lv-LV',
'nb-NO', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO',
'ru-RU', 'sk-SK', 'sl-SL', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA',
'zh-CN', 'zh-HK', 'zh-TW'
)][string]$Locale = 'auto',
# Download the latest $Count wallpapers.
[ValidateRange("Positive")]
[int]$Count = 3,
# Delete files in the folder that were not downloaded in this execution of the command.
[switch]$Delete,
# Resolution of the image to download.
[ValidateSet(
'auto', '800x600', '1024x768', '1280x720', '1280x768', '1366x768',
'1920x1080', '1920x1200', '720x1280', '768x1024', '768x1280',
'768x1366', '1080x1920', 'UHD'
)][string]$Resolution = 'auto',
# Destination folder to download the wallpapers to.
[string]$Path = $(Join-Path $([Environment]::GetFolderPath("MyPictures")) "Wallpapers"),
# Get wallpaper information from an unofficial alternative API with > 500 images
[switch]$FromAlternateSource
)
process {
# Contstants
$Const_Url_Image_Host = 'https://www.bing.com'
$Const_Url_Feed_Source = 'https://www.bing.com/HPImageArchive.aspx?format=js&pid=hp'
$Const_Url_Feed_AltSource = 'https://api45gabs.azurewebsites.net/api/sample/bingphotos'
# Check if download folder exists and otherwise create it
if (!(Test-Path $Path)) {
If ($WhatIfPreference -eq $true) {
Write-Output "Create Directory $Path"
}
else {
New-Item -ItemType Directory $Path
}
}
# Max item count: the number of images we'll query for
# [int]$maxItemCount = [System.Math]::max(1, [System.Math]::max($Count, 8))
# URI to fetch the image locations from
if ($Locale -eq 'auto') {
$market = ''
}
else {
$market = "&mkt=$Locale"
}
# Get the appropiate screen resolution, using OS specific probing
if ($Resolution -eq 'auto') {
if ($PSVersionTable.OS.Contains('Windows')) {
Write-Verbose 'On Windows'
Add-Type -AssemblyName System.Windows.Forms
$primaryScreen = [System.Windows.Forms.Screen]::AllScreens | Where-Object { $_.Primary -eq 'True' }
$width = $primaryScreen.Bounds.Width
$height = $primaryScreen.Bounds.Height
}
elseif ($PSVersionTable.OS.Contains('macOS') -or $PSVersionTable.OS.Contains('Darwin')) {
Write-Verbose 'On MacOS'
$null = system_profiler SPDisplaysDataType -json | ConvertFrom-Json -OutVariable displayInfo
foreach ($display in $displayInfo[0].SPDisplaysDataType[0].spdisplays_ndrvs[0]) {
if ($display.spdisplays_main -eq 'spdisplays_yes') {
[int]$width = $display._spdisplays_pixels.split('x')[0].Trim()
[int]$height = $display._spdisplays_pixels.split('x')[1].Trim()
}
}
}
else {
# Default for Linux HD
$width = 1920
$height = 1080
}
# Determine the resolution to download based on width and height
if ($width -le 1024) {
$Resolution = '1024x768'
}
elseif ($width -le 1280) {
$Resolution = '1280x720'
}
elseif ($width -le 1366) {
$Resolution = '1366x768'
}
elseif ($height -le 1080) {
$Resolution = '1920x1080'
}
elseif ($width -le 1920) {
$Resolution = '1920x1080'
}
else {
$Resolution = 'UHD'
}
}
Write-Verbose "Resolution: $resolution"
# $items = New-Object System.Collections.ArrayList
if ($FromAlternateSource) {
# Uses a JSON format, all items returned. No "desc" field.
$jsonImgs = ConvertFrom-Json -InputObject $(Invoke-WebRequest -UseBasicParsing -Uri $Const_Url_Feed_AltSource).Content
}
else {
# Using the JSON Format, which puts images in an images array.
# Add paging support for when the count of images requested > 8
# The most recent 12 images are available through the Bing API.
# It's a zero index page. Page 0 returns items 1-page size
# They have a bug on paging though so :shrug:
# &idx=0&n=$maxItemCount
$pageSize = 8 # Maximum page size
<# Action when all if and elseif conditions are false #>
if ($Count -gt 15) { $Count = 15; Write-Verbose "Bing API supports a maximum of 15 images" }
$pageAndRemainder = [System.Math]::DivRem($Count, $pageSize)
# To support remainder logic vs 0 offset pages
if (($pageAndRemainder.item2 -eq 0) -and ($pagesAndRemainder.item1 -ne 0)) {
$pages = $pageAndRemainder.Item1 - 1
}
else {
$pages = $pageAndRemainder.item1
}
for ($i = 0; $i -le $pages; $i++) {
if ($pages -eq $i) { $pageItems = $pageAndRemainder.Item2 } else { $pageItems = $pageSize }
[string]$pageUri = "$Const_Url_Feed_Source$market&idx=$($i * $pageSize)&n=$pageItems"
Write-Verbose "Fetching Page $($i + 1)"
$request = Invoke-WebRequest -Uri $pageUri -DisableKeepAlive -UserAgent 'parp-1.0' -UseBasicParsing
$jsonImgs += $(ConvertFrom-Json -InputObject $request.Content).Images
}
}
# Add utility properties
foreach ($item in $jsonImgs) {
[string]$imageDate = [datetime]::ParseExact($item.startdate, 'yyyyMMdd', $null).ToString("yyyy-MM-dd")
[string]$imageUrl = "$Const_Url_Image_Host$($item.urlBase)_$Resolution.jpg"
Add-Member -Type NoteProperty -Name imagedate -Value $imageDate -InputObject $item -Force
Add-Member -Type NoteProperty -Name imageurl -Value $imageUrl -InputObject $item -Force
}
# Download items
$jsonImgs = $($jsonImgs | Sort-Object -Property startdate -Unique -Descending)
Write-Verbose "Most recent $Count images to check from $($jsonImgs.length)"
$i = 0
$client = New-Object System.Net.WebClient
foreach ($item in $jsonImgs) {
$destination = Join-Path $Path "$($item.imagedate).jpg"
$destinationWithMetadata = Join-Path $Path $($item.imagedate + "_meta.jpg")
# Download the enclosure if we haven't done so already
if (!(Test-Path $destination) -and !(Test-Path $destinationWithMetadata)) {
Write-Verbose $('image: ' + $item.startdate + ' -- ' + $item.title)
Write-Verbose "Test-Path $destination $(Test-Path $destination)"
Write-Verbose "Test-Path $destinationWithMetadata $(Test-Path $destinationWithMetadata)"
if ($WhatIfPreference -eq $true) {
Write-Output "WhatIf: Downloading image from: $($item.imageurl) to: $destination"
}
else {
Write-Verbose "Downloading image from: $($item.imageurl) to: $destination"
$client.DownloadFile($item.imageurl, $destination)
}
}
$i++
if ($i -eq $Count) { break }
}
# Delete switch logic
if ($Delete -and ($Count -gt 0)) {
# We do not want to keep every file; remove the old ones
Write-Host "Cleaning the directory..."
$i = 1
Get-ChildItem -Filter "*.jpg" "$Path\*" -Include "????-??-??.jpg", "????-??-??_meta.jpg" | Sort-Object -Descending FullName | ForEach-Object {
if ($i -gt $Count) {
# We have more files than we want, delete the extra files
$fileName = $_.FullName
if ($WhatIfPreference -eq $true) {
Write-Output "Deleting file $fileName"
}
else {
Write-Verbose "Deleting file $fileName"
Remove-Item "$fileName"
}
}
$i++
}
}
}
}