Skip to content

Instantly share code, notes, and snippets.

@frogamic
Created October 31, 2020 13:44
Show Gist options
  • Save frogamic/be85dec6b2be962449b2cf82b0f32d4c to your computer and use it in GitHub Desktop.
Save frogamic/be85dec6b2be962449b2cf82b0f32d4c to your computer and use it in GitHub Desktop.
For fetching the fantastic composite screengrabs from /u/iamonlyoneman on /r/killlakill
$user = 'iamonlyoneman'
$subreddit = 'KillLaKill'
$stateFile = 'fetch-last.txt'
function Get-Page {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string] $Uri,
[Parameter()]
[string] $After,
[Parameter()]
[string] $Before
)
if (![String]::IsNullOrEmpty($Before)) {
$Uri += "?before=$Before"
}
if (![String]::IsNullOrEmpty($After)) {
if ([String]::IsNullOrEmpty($Before)) {
$Uri += "?"
} else {
$Uri += "&"
}
$Uri += "after=$After"
}
Invoke-RestMethod -Method Get -Uri $Uri
}
$lastFetched = $null
if (Test-Path -Path $stateFile -PathType Leaf) {
$lastFetched = Get-Content -Path $stateFile
}
$skipped = @()
$posts = @()
$page = @{data = @{after = $null}}
$done = $false;
Write-Host "Fetching posts from $user in $subreddit"
do {
$page = Get-Page -Uri "https://www.reddit.com/user/$user/submitted.json" -After $page.data.after
$posts += $page.data.children | Where-Object {
if ($done) {
$false
} else {
$done = $_.data.name -eq $lastFetched
!$done -and $_.data.subreddit -eq $subreddit
}
}
Write-Host '.' -NoNewline
} while ($page.data.after -and !$done)
Write-Host "`n$($posts.count) posts found"
for ($i = $posts.count - 1; $i -ge 0; $i -= 1) {
Write-Progress -Activity "Downloading Images" -Status "$($posts.count - $i) / $($posts.count)" -PercentComplete (($posts.count - $i) / $posts.count * 100)
$extension = (Split-Path -Path $posts[$i].data.url -leaf).split('.')[1].ToLower()
$filename = ("$($posts[$i].data.title)_$($posts[$i].data.name).$extension" -replace "[$([RegEx]::Escape([string][IO.Path]::GetInvalidFileNameChars()))]+","_")
$escapedFileName = ([Management.Automation.WildcardPattern]::Escape($filename))
if ($extension -in 'jpeg','jpg','png') {
Invoke-WebRequest -Uri $posts[$i].data.url -OutFile $escapedFileName
Rename-Item -LiteralPath $escapedFileName $filename
} else {
$skipped += $posts[$i]
}
Set-Content -Path $stateFile -Force -Value $posts[$i].data.name
}
Write-Progress -Activity "Downloading Images" -Status "$($posts.count) / $($posts.count)" -Completed
$skipped.data | Select-Object -Property 'name','title','url'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment