This repository has been archived by the owner on Sep 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallpaper-carousel.ps1
138 lines (106 loc) · 5.46 KB
/
wallpaper-carousel.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
param (
[Parameter(Position = 0, Mandatory = $true)]
[string] $Dir,
[Parameter(Position = 1, Mandatory = $false)]
[array] $Subs = (
"Amoledbackgrounds",
"wallpaperdump",
"VillagePorn",
"EarthPorn",
"wallpaper",
"BeachPorn",
"WaterPorn",
"CityPorn",
"LakePorn",
"SkyPorn"
),
[Parameter(Position = 2, Mandatory = $false)]
[int] $Mode = 0, # 0: Schedule, 1: Fetch, 2: Sweep
[TimeSpan] $SweepInterval = (New-TimeSpan -Days 7),
[TimeSpan] $Interval = (New-TimeSpan -Days 1),
[TimeSpan] $TTL = (New-TimeSpan -Days 7),
[string] $Timeframe = 'month',
[string] $Listing = 'top',
[int] $Limit = 10,
[Switch] $NSFW
)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$ProgressPreference = "SilentlyContinue"
$ListingOptions = ('controversial', 'best', 'hot', 'new', 'random', 'rising', 'top')
$TimeframeOptions = ('hour', 'day', 'week', 'month', 'year', 'all')
if ( -not $TimeframeOptions.Contains($Timeframe)) { throw "Unknown option $Timeframe for Timeframe. Available timeframe types: $($TimeframeOptions -join ', ')" }
if ( -not $ListingOptions.Contains($Listing)) { throw "Unknown option $Listing for Listing. Available listing types: $($ListingOptions -join ', ')" }
$ResPattern = '[[({]\s*(?<Width>\d+)\s*x\s*(?<Height>\d+)\s*[\])}]'
$ScreenRes = ([System.Windows.Forms.Screen]::PrimaryScreen).Bounds
$FileTypes = ('.jpg', '.jpeg', '.webp', '.png', '.bmp')
$MinH = if ($ScreenRes.Height -gt 720) { $ScreenRes.Height - ($ScreenRes.Height * 0.15) } else { $ScreenRes.Height }
$MinW = if ($ScreenRes.Width -gt 1280) { $ScreenRes.Width - ($ScreenRes.Width * 0.10) } else { $ScreenRes.Width }
function ValidateImage(
[Parameter(Position = 0)] [string] $Filename,
[int] $H,
[int] $W
) {
if (($W -lt $MinW) -or ($H -lt $MinH)) { Write-Debug "$filename's image resolution is too small for this screen. Skipping..."; return $false }
elseif ($H -gt $W) { Write-Debug "$Filename seems to be a mobile wallpaper. Skipping..."; return $false }
return $true
}
if ($Mode -eq 0) {
Unregister-ScheduledTask WallpaperCarouselSweeper -Confirm:$false -ErrorAction Ignore
Unregister-ScheduledTask WallpaperCarousel -Confirm:$false -ErrorAction Ignore
$Settings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -Priority 6 -RunOnlyIfNetworkAvailable
$Arguments = (
"-WindowStyle Hidden",
"-Command .\$(Split-Path $PSCommandPath -Leaf)",
$Dir, ($Subs -join ','), 1,
"-Timeframe $Timeframe",
"-Listing $Listing"
)
$SweeperArguments = $Arguments[0..1] + ($Dir, "-Mode 2", "-TTL $TTL", "-SweepInterval $SweepInterval")
Register-ScheduledTask WallpaperCarousel `
-Action (New-ScheduledTaskAction "pwsh.exe" -Argument ($Arguments -join ' ') -WorkingDirectory $PSScriptRoot) `
-Trigger (New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval $Interval) `
-Settings $Settings -Force | Out-Null
Register-ScheduledTask WallpaperCarouselSweeper `
-Action (New-ScheduledTaskAction "pwsh.exe" -Argument ($SweeperArguments -join ' ') -WorkingDirectory $PSScriptRoot) `
-Trigger (New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval $SweepInterval) `
-Settings $Settings -Force | Out-Null
Write-Host "Added Task Schedule. Remove it by running `"Unregister-ScheduledTask WallpaperCarousel,WallpaperCarouselSweeper -Confirm:`$false`""
}
elseif ($Mode -eq 1) {
$SubredditData = Invoke-WebRequest -Uri "https://www.reddit.com/r/$($Subs | Get-Random).json?listing=$Listing&t=$Timeframe&limit=$Limit"
$SubredditPosts = (ConvertFrom-Json $SubredditData.content).data.children | ForEach-Object { $_.data } | Where-Object {
-not $_.is_video -and -not $_.spoiler -and $_.url `
-and -not $_.banned_by -and -not $_.removed_by `
-and $FileTypes.Contains('.' + $_.url.Split('.')[-1])
}
New-Item $Dir -ItemType Directory -Force | Out-Null
$Existing = Get-ChildItem $Dir -Name
foreach ($Post in $SubredditPosts) {
if (-not $NSFW -and $Post.over_18) {
continue
}
$Filename = Split-Path $Post.url -Leaf
if ($Existing -and $Existing.Contains($Filename)) {
Write-Debug "$Dir\$filename already exists. Skipping..."
continue
}
if ($Post.title -imatch $ResPattern -and -not (ValidateImage $Filename -H $Matches.Height -W $Matches.Width)) { continue }
try { $Image = New-Object System.Drawing.Bitmap ((Invoke-WebRequest $Post.url).RawContentStream) }
catch { Write-Error "An error occurred while processing $filename from $($Post.url)"; continue }
if (-not (ValidateImage $Filename -H $Image.Height -W $Image.Width)) { continue }
$Image.Save("$Dir\$Filename")
Write-Debug "Saved $Dir\$filename ($($Post.subreddit))"
}
}
elseif ($Mode -eq 2) {
if (-not (Test-Path -Path $Dir)) {
exit
}
$DateTTL = (Get-Date).Add(-$TTL)
$Files = Get-ChildItem $Dir
| Where-Object { ($FileTypes.Contains($_.Extension)) -and ($_.CreationTime -lt $DateTTL) }
| ForEach-Object { $_.FullName }
Remove-Item $Files -Confirm:$false
Write-Debug "Deleted $($Files.Length) file$(if ($Files.Length -cne 1) { 's' })"
}