-
Notifications
You must be signed in to change notification settings - Fork 7
/
hackspace-downloader.ps1
executable file
·61 lines (55 loc) · 2.04 KB
/
hackspace-downloader.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
<#
.SYNOPSIS
Downloader for all Hackspage issues
.DESCRIPTION
The Hackspage issues are downloadable for free under https://hackspace.raspberrypi.org/issues
or you can buy the paper issues under: https://store.rpipress.cc/collections/all/hackspace-magazine?sort_by=created-descending
.NOTES
This script is under GNU GENERAL PUBLIC LICENSE
Orignal author: Rubemlrm - https://github.com/Rubemlrm
The new rewritten code is now programmed by [Author](https://github.com/Jaykul)
Script is part of https://github.com/joergi/HackspaceDownloader
#>
[CmdletBinding()]
param(
[string]$FirstIssue = "1",
[string]$LastIssue
)
[uri]$baseUrl = "https://hackspace.raspberrypi.org/issues/"
# control variables
$baseDir = $PWD.Path
if (!$LastIssue) {
$LastIssue = Get-Content "$baseDir\issues.txt" -First 1
}
$downloadDir = Join-Path $baseDir "issues"
# Check if directory doesn't exist and try to create it
if (!(Test-Path -Path $downloadDir)) {
$null = New-Item -ItemType Directory -Path $downloadDir -ErrorAction Stop
}
$errorCount = 0
foreach ($issue in $FirstIssue..$LastIssue) {
$uri = [uri]::new($baseUrl, "{0:00}/pdf/download" -f $issue)
Write-Verbose -Message "Downloading $uri"
if (($link = (Invoke-WebRequest -UseBasicParsing $uri).Links.Where{ $_.class -eq "c-link" }.href)) {
$uri = [uri]::new($baseUrl, $link)
try {
# Replace this line to specify the output file name
$fileName = "HS_{0:00}.pdf" -f $issue
$filePath = Join-Path $downloadDir $fileName
Write-Information "Downloading $uri"
Write-Output "Downloading $uri"
Invoke-WebRequest $uri -OutFile $filePath -ErrorAction Stop
Write-Verbose -Message "Downloaded $uri"
} catch {
Write-Warning "Failed downloading $uri"
Get-Error | Out-String | Write-Verbose -Verbose
$errorCount++
}
} else {
Write-Warning "Failed to find link for issue $issue"
$errorCount++
}
}
if ($errorCount -gt 0) {
exit 1
}