-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-RedditWallpapers.ps1
100 lines (88 loc) · 3.82 KB
/
Get-RedditWallpapers.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
<###############################################################################
## Get-RedditWallpapers.ps1
## .Contributors: Pandages, MrAusnadian, SpaceDeerEdith
## Version: 3.1
##
## .SYNOPSIS
## Downloads wallpaper images from a user-specified subreddit.
##
## .DEPENDENCIES
## Background Intelligent Transfer Service (BITS)
## Reddit / JSON
###############################################################################>
[CmdletBinding()]
[Alias()]
[OutputType([int])]
param (
[string]$wallpaperRoot = "C:\Wallpapers",
[string[]]$subReddits = @("EarthPorn", "Wallpapers", "spaceporn", "Art"),
[int]$minWidth = 1920,
[int]$minHeight = 1080,
[ValidateSet("new", "top", "hot", "rising")]
[String]$sort = "new",
[bool]$ignorePortrait = $true
)
function Get-Wallpapers {
param (
[string]$destination,
[string]$subReddit,
[int]$minWidth,
[int]$minHeight,
[string]$sort,
[bool]$ignorePortrait,
[int]$start = 100
)
Start-Sleep 5
$images = Invoke-RestMethod "https://www.reddit.com/r/$subReddit/$sort/.json?start=$start" -Method Get -Body @{ limit = "100" }
$total = $images.data.dist
Write-Output "Downloading images from /r/$subReddit sorted by $sort to $destination..."
foreach ($child in $images.data.children) {
$url = $child.data.url
$title = Remove-InvalidFileNameChars $child.data.title
[int]$height = $child.data.preview.images[0].source.height
[int]$width = $child.data.preview.images[0].source.width
if ($url -match "\.(jpe?)|(pn)g$" -and
($height -ge $minHeight) -and
($width -ge $minWidth) -and
(-not ($ignorePortrait -and $child.data.preview.images[0].source.height -gt $child.data.preview.images[0].source.width))
) {
$fileName = "$title$($url.Substring($url.LastIndexOf('.')))"
$fullPath = Join-Path -Path $destination -ChildPath $fileName
if (Test-Path $fullPath) {
Write-Output " * Skipping $fileName - File already exists!"
} else {
$percent = ++$current / $total * 100
Write-Progress -Activity "Downloading images..." -Status "Downloading $fileName from $url..." -PercentComplete $percent
try {
Start-BitsTransfer -Source $url -Destination $fullPath
} catch {
Write-Error "Failed to download $url"
}
}
} else {
Write-Output "$title skipped - Resolution ($width x $height) less than minimum resolution ($minWidth x $minHeight)"
}
}
}
Function Remove-InvalidFileNameChars {
param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[String]$Name
)
$invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
$re = "[{0}]" -f [RegEx]::Escape($invalidChars)
return $Name -replace $re
}
## BEGIN SCRIPT EXECUTION ##
foreach ($subReddit in $subReddits) {
$destination = Join-Path -Path $wallpaperRoot -ChildPath $subReddit
if (-not (Test-Path $destination)) { New-Item -Path $destination -ItemType Directory | Out-Null }
try {
Get-Wallpapers -destination $destination -subReddit $subReddit -minWidth $minWidth -minHeight $minHeight -sort $sort -ignorePortrait $ignorePortrait
Get-Wallpapers -destination $destination -subReddit $subReddit -minWidth $minWidth -minHeight $minHeight -sort $sort -ignorePortrait $ignorePortrait -start 100
} catch {
Write-Error "An error occurred attempting to download images from /r/$subReddit!"
} finally {
Write-Progress -Activity "Downloading images..." -Completed
}
}