-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBingWallpaperPs.ps1
208 lines (173 loc) · 6.34 KB
/
BingWallpaperPs.ps1
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
# Check whether the user has internet
# connection or not.
# Give a URL which would stay online
# everytime. For this case, I'll
# use Bing website as I'll download
# Bing wallpaper of the day.
$URL = "www.bing.com"
# Trap the process inside an infinity
# loop. For this case, I'll use
# while loop.
while (1) {
# If the user has internet connection and
# the website is online then the next
# line would return "True", if not then
# it would return "False".
$hasInternetConnection = Test-Connection -TargetName $URL -Ping -Count 1 -Quiet
# If it returns "True" then tell the user
# something and proceed to the next tasks
if ($hasInternetConnection -eq "True") {
Write-Output "Your internet connection is OK."
Write-output "Proceeding to the next steps."
# Break the loop.
break
}
# If it returns "False" then tell the user
# something and go the next loop after a
# certain amount of time. For this case,
# I'll go the next loop after 1 minute.
else {
Write-Output "Check your internet connection"
# Enter the wait time in seconds. I'll give
# 60 as 60 seconds mean 1 minute.
Start-Sleep -Seconds 60
}
}
# BING API
# ($uri is created)
$uri = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-WW"
# Get the image metadata and temporarily store it
# ($response is created)
$response = Invoke-WebRequest -Method Get -Uri $uri
# Extrect the image metadata
# ($body and $FHD_fileurl is created)
$body = ConvertFrom-Json -InputObject $response.Content
$FHD_fileurl = "https://www.bing.com" + $body.images[0].url
# Try to get the UHD version if available
# This basically replaces "1920x1080" with UHD
# from the $FHD_fileurl
# ($UHD_fileurl is created)
$UHD_fileurl = $FHD_fileurl.Replace("1920x1080", "UHD")
# echo $body.images
# Determine filename for both HD & UHD images
# I had to edit to resolve the file access denied problem in the Linux OS.
$FHD_filename = $body.images[0].startdate + " - " + $body.images[0].copyright.Split('(', 2)[-2].Replace(" ", "-").Replace("?", "").Replace("/", ", ").Replace("-", " ").TrimEnd(' ') + "_FHD.jpg"
$UHD_filename = $body.images[0].startdate + " - " + $body.images[0].copyright.Split('(', 2)[-2].Replace(" ", "-").Replace("?", "").Replace("/", ", ").Replace("-", " ").TrimEnd(' ') + "_UHD.jpg"
$LNK_filename = $body.images[0].startdate + "_LNK.txt"
# Get the current directory
$curDir = Get-Location
# Check if there is a folder named "Bing Wallpapers".
# If the folder doesn't exist, create the folder.
# Also, if the folder exist, check whether FHD and UHD.
# folder exitst or not. if not, create 2 folders named FHD and UHD.
$folderName = "Bing wallpaper"
if (Test-Path $curDir/$folderName) {
# Write-Host "The folder already exist."
Set-Location $curDir/$folderName
# FHD
if (Test-Path "FHD") {
Write-Host "The FHD folder already exist."
}
else {
New-Item "FHD" -ItemType Directory
}
# UHD
if (Test-Path "FHD") {
Write-Host "The UHD folder already exist."
}
else {
New-Item "UHD" -ItemType Directory
}
# LINKSN
if (Test-Path "LNK") {
Write-Host "The LNK folder already exist."
}
else {
New-Item "LNK" -ItemType Directory
}
}
else {
New-Item $folderName -ItemType Directory
# Write-Host "The folder is created."
Set-Location $curDir/$folderName
New-Item "FHD" -ItemType Directory
New-Item "UHD" -ItemType Directory
New-Item "LNK" -ItemType Directory
}
# Set-Location $curDir
# Set-Location ".\Bing wallpaper\FHD\"
# echo $PWD
# echo "Before the invoke request"
# echo "After the invoke request"
$FHD_filepath = "$curDir/Bing wallpaper/FHD/" + $FHD_filename
$UHD_filepath = "$curDir/Bing wallpaper/UHD/" + $UHD_filename
$LNK_filepath = "$curDir/Bing wallpaper/LNK/" + $LNK_filename
# Get back to the working directory from where the
# script was launched
Set-Location -Path ..
# Creating three boolean variables to determine which files
# have been downloaded. These will be used to notify users
# which files have been downloaded. By default, the values
# false as they have not been downloaded yet.
$FHD = "F"
$UHD = "F"
$LNK = "F"
# Check whether the user already have the wallpaper
# or not. This can be verified by checking whether
# the file or wallpaper is present or not.
if (Test-Path $FHD_filepath) {
Write-Host "The FHD file exist. So, the FHD file will not be downloaded."
}
else {
# Downlaod the FHD file.
Invoke-WebRequest -Method Get -Uri "$FHD_fileurl" -OutFile "$FHD_filepath"
$FHD = "T" # Means, the FHD file has been downloaded.
}
if (Test-Path $UHD_filepath) {
Write-Host "The UHD file exist. So, the UHD file will not be downloaded."
}
else {
# Download the UHD file.
Invoke-WebRequest -Method Get -Uri "$UHD_fileurl" -OutFile "$UHD_filepath"
$UHD = "T" # Means, the FHD file has been downloaded.
}
if (Test-Path $LNK_filepath) {
Write-Host "The LNK file exist. So, the wallpaper not be downloaded and applied."
}
else {
# Save the informations in the LNK file.
Add-Content -Path $LNK_filepath -Encoding utf8 -Value "Title of the image:
$($body.images[0].copyright)
More information of the image:
$($body.images[0].copyrightlink)
Date of the image(YYYYMMDD):
$($body.images[0].startdate)
The base URL of the image:
$($body.images[0].urlbase)
Download link of the FHD file:
$($FHD_fileurl)
Download link of the UHD file:
$($UHD_fileurl)
Powershell command to download the FHD file:
Invoke-WebRequest -Method Get -Uri `"$FHD_fileurl`" -OutFile `"$FHD_filename`"
Powershell command to download the UHD file:
Invoke-WebRequest -Method Get -Uri `"$UHD_fileurl`" -OutFile `"$UHD_filename`"
"
$LNK = "T" # Means, the FHD file has been saved.
}
# Now, apply the wallpaper
# Go to the setWallpaper directory
Set-Location .\setWallpaper
# Run the command
pwsh .\chooseOS.ps1 $LNK $UHD $FHD $FHD_filepath $body.images[0].copyright $body.images[0].copyrightlink
# Return to the current working directory
Set-location ..
# Return to the previous working directory.
# # Set the picture as desktop background image.
# if ($IsLinux) {
# # Go to the linux directory
# Set-Location .\setWallpaper\linux
# pwsh neofetch.ps1 $FHD_filepath $FHD $UHD $LNK $FHD_filepath
# # Get back to the working directory
# Set-Location ..\..
# }