Old-Lost

Get-DesktopBackgrounds

Jul 21st, 2017
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param (
  2.     [string]$BackgroundsFolder = (Join-Path -Path  ([System.Environment]::GetFolderPath('Personal')) -ChildPath 'DesktopBackgrounds'),
  3.     [int]$Limit = 200
  4. )
  5. function Get-Image {
  6.     param ([string]$Path)
  7.     if (Test-Path $Path) {
  8.         $img = [System.Drawing.Image]::FromFile($Path)
  9.         $img.Clone()
  10.         [void]$img.Dispose()
  11.     } else {
  12.         Write-Log "Get-Image: File not found: $_"
  13.     }
  14. }
  15. function Write-Log {
  16.     param ([string]$Message)
  17.     Write-Output ('{0:yyyy-MM-dd hh:mm:ss} {1}' -f (Get-Date), $Message) | Out-File $LogFile -Append
  18.     Write-Host $Message -ForegroundColor Yellow
  19. }
  20.  
  21. Set-StrictMode -Version Latest
  22. if (-not (Test-Path $BackgroundsFolder)) {
  23.     New-Item $BackgroundsFolder -ItemType Directory
  24. } elseif (-not (Get-Item -Path $BackgroundsFolder).Attributes.ToString().Contains('Directory')) {
  25.     throw "$BackgroundsFolder is not a directory"
  26. }
  27. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  28. $LogFolder = ([System.Environment]::GetFolderPath('Personal'))
  29. [string]$LogFile = Join-Path -Path $LogFolder -ChildPath 'DesktopBackgrounds.log'
  30. [string]$DTBGListFile = Join-Path -Path $LogFolder -ChildPath 'DesktopBackgrounds.txt'
  31. if (-not (Test-Path $DTBGListFile)) { New-Item $DTBGListFile }
  32. $ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
  33. $ImageUrlRE = '.*?="(https?://(?:orig..\.deviantart\.net|blogspot\.com|hdqwalls.com|i\.imgur\.com|i\.redd\.it)/[^"]*?)">'
  34. $ImageExtensionRE = '\.(jpg|png)\z'
  35. $RSSFeedUrl = 'https://www.reddit.com/r/WQHD_Wallpaper.rss'
  36. #
  37. # Copy images from the WQHD_Wallpaper subreddit to a folder
  38. #
  39. Get-ChildItem -Path $BackgroundsFolder\* -File -Include *.jpg, *.png |
  40.     Sort-Object LastWriteTime -Descending |
  41.     Select-Object -Skip $Limit |
  42.     Remove-Item -Confirm:$false -Verbose
  43. # Get the RSS feed from the subreddit
  44. # look through post entries looking for links to imgur.com or redd.it
  45. # download any jpg links to our backgrounds folder
  46. ([xml](Invoke-WebRequest -Uri $RSSFeedUrl)).feed.entry | % { $_.content.'#text' } | ? { $_ -match $ImageUrlRE } | % {
  47.     [string]$Uri = $Matches[1]
  48.     # Write-Log $Uri
  49.     if ($Uri -match $ImageExtensionRE) {
  50.         [string]$OutFile = Join-Path -Path $BackgroundsFolder -ChildPath ($Uri -replace '.*?/([^/]+)\z', '$1')
  51.         # $LogFile contains the names of all files we've downloaded before
  52.         # Skip the file if we've copied it before
  53.         if ((-not (Select-String -Path $DTBGListFile -SimpleMatch -Pattern $Uri)) -and (-not (Test-Path $OutFile))) {
  54.             Write-Log "Copying $Uri"
  55.             try {
  56.                 Invoke-WebRequest -Uri $Uri -OutFile $OutFile
  57.                 # Add the name to our log file so if we delete it manually it won't be recopied in the future
  58.                 Add-Content -Path $DTBGListFile -Value $Uri
  59.                 # Check image to see if it's only some low-res crap
  60.                 [System.Drawing.Bitmap]$Image = Get-Image $OutFile
  61.                 if ($Image -and (($Image.Width -lt 1920) -or ($Image.Height -lt 1040))) {
  62.                     Write-Log "$OutFile is low-res. Deleting..."
  63.                     Remove-Item $OutFile
  64.                 } elseif (-not $Image) {
  65.                     Write-Log "Could not get dimensions of $OutFile"
  66.                 }
  67.             } catch {
  68.                 Write-Log "Copying file '$Uri' failed"
  69.             }
  70.         }
  71.     }
  72. }
Add Comment
Please, Sign In to add comment