Skip to content

Instantly share code, notes, and snippets.

@drguildo
Last active November 16, 2022 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drguildo/45eb3e2f0ecb4357c1ea7ed43c235801 to your computer and use it in GitHub Desktop.
Save drguildo/45eb3e2f0ecb4357c1ea7ed43c235801 to your computer and use it in GitHub Desktop.
A PowerShell script for downloading all of the images posted in a subreddit.
param (
[Parameter(Mandatory = $true)] [string]$Subreddit
)
$ErrorActionPreference = 'Continue'
$UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FSL 7.0.6.01001)"
$StartingDirectory = Get-Location
function getFilename([string]$url) {
$uri = New-Object -TypeName Uri -ArgumentList $url
return $uri.Segments[$uri.Segments.Length - 1]
}
function deleteIfJunk([string]$filename) {
$hash = (Get-FileHash $filename -Algorithm MD5).Hash
if ($hash -eq "D835884373F4D6C8F24742CEABE74946") {
Write-Host "Deleting junk file $filename."
Remove-Item $filename
}
}
function download([string]$url) {
Write-Host "Downloading $url..."
$filename = getFilename($url)
if (Test-Path $filename) {
Write-Host "$filename already exists. Skipping."
return
}
try {
Invoke-WebRequest -Uri $url -OutFile $filename -UserAgent $UserAgent -ErrorAction Continue
deleteIfJunk($filename)
}
catch {
Write-Host $_.Exception.Message
}
}
function getJson([string]$jsonUrl) {
Write-Host "Fetching JSON from $jsonUrl"
$json = ""
$success = $false
do {
try {
$json = Invoke-WebRequest $jsonUrl | ConvertFrom-Json
$success = $true
}
catch {
Write-Error $_.Exception.Message
}
} while (-not $success)
return $json
}
function downloadImages($json) {
$json.data.children.data | Where-Object { $_.post_hint -eq "image" } | ForEach-Object { download $_.url }
$after = $json.data.after
if (-Not [string]::IsNullOrWhiteSpace($after)) {
$json = getJson("https://www.reddit.com/r/$Subreddit/.json?after=$after")
downloadImages($json)
}
}
function getVideoDownloaderExecutableName() {
$videoDownloaders = @('yt-dlp','youtube-dl')
foreach ($videoDownloader in $videoDownloaders) {
if (Get-Command $videoDownloader -ErrorAction SilentlyContinue) {
return $videoDownloader
}
}
return $null
}
function downloadVideos($json) {
$videoDownloader = getVideoDownloaderExecutableName
if ([string]::IsNullOrWhiteSpace($videoDownloader)) {
Write-Error "Could not find video downloader. Video downloading disabled."
return
}
$json.data.children.data | Where-Object { $_.post_hint -eq "hosted:video" } | ForEach-Object { & $videoDownloader $_.url }
$after = $json.data.after
if (-Not [string]::IsNullOrWhiteSpace($after)) {
$json = getJson("https://www.reddit.com/r/$Subreddit/.json?after=$after")
downloadVideos($json)
}
}
try {
$Subreddit = $Subreddit -replace "[\/\\\.]",""
if (Test-Path $Subreddit) {
$response = Read-Host -Prompt ('Directory {0} already exists. Continue?' -f $Subreddit)
$response = $response.ToLower()
if (-not ($response.Equals("y") -or $response.Equals("yes"))) {
exit
}
} else {
mkdir $Subreddit | out-null
}
Set-Location -Path $Subreddit
$json = getJson("https://www.reddit.com/r/$Subreddit/.json")
downloadImages($json)
downloadVideos($json)
Write-Host -BackgroundColor Green -ForegroundColor White "We processed all the JSON!"
}
finally {
Set-Location $StartingDirectory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment