-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelegraphDownloader.ps1
105 lines (88 loc) · 3.9 KB
/
TelegraphDownloader.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
# Specifies a path to one or more locations.
param (
[Parameter(Mandatory = $False)]
[String] $PaserFile
)
function DownTelegraph {
if ($PaserFile) {
Select-String -Path $PaserFile -Pattern "https://telegra.ph/" -AllMatches |
Select-Object Line -Unique |
Format-Table -HideTableHeaders |
Out-String |
Set-Content .\TEMP
}
else {
<# Action when all if and elseif conditions are false #>
Write-Host "Please enter the named `Result` json file path..." -ForegroundColor DarkCyan
}
if (Test-Path .\TEMP) {
$URLS = (Get-Content -Path .\TEMP).Split("\n").Trim()
$Target = @()
$EndPrefix = "?return_content=true"
$URLS | ForEach-Object {
if ($_.contains('"text":')) {
$Target += $_.replace('"text":', "").replace('telegra.ph', "api.telegra.ph/getPage").replace('"', "") + $EndPrefix
}
if ($_.contains('"href":')) {
$Target += $_.replace('"href":', "").replace('telegra.ph', "api.telegra.ph/getPage").replace('"', "") + $EndPrefix
}
}
# Download images in parallel using -AsJob
$Target | ForEach-Object -Parallel {
$url = $_
# Suppress error messages
$ErrorActionPreference = 'SilentlyContinue'
# Download JSON data from URL
$WebJSONData = Invoke-WebRequest -Uri $url -ErrorAction SilentlyContinue -UseBasicParsing
# Extract title and image URLs from JSON data
$Title = (ConvertFrom-Json $WebJSONData.Content).result.title
$Images = (ConvertFrom-Json $WebJSONData.Content).result.content | Where-Object { $_.children[0].tag -eq "img" }
$Count = $Images | Measure-Object | Select-Object -ExpandProperty Count
Write-Host "Downloading: title:"$Title "total:"$Count
# Create directory for images
$StorePath = ".\Images\$Title"
$StratPrefix = "https://telegra.ph"
if (Test-Path $StorePath) {
return
}
else {
New-Item -ItemType Directory -Path $StorePath > $null
}
# Download images
$Images | ForEach-Object {
$ImgObject = $_.children | Where-Object { $_.tag -eq "img" }
if ($null -eq $ImgObject) {
Write-Host "No image found..." -ForegroundColor Red
}
else {
$ImageUrl = $ImgObject.attrs.src
if ($ImageUrl) {
$ImageName = $ImageUrl.replace("/file/", "")
$OUTPUT = Join-Path $StorePath $ImageName
# Get current image index and total count
$Index = $Images.IndexOf($_) + 1
$Progress = "$Index/$Count"
Write-Host "Downloading: [$Progress] $Title $StratPrefix$ImageUrl" -ForegroundColor Magenta
# Download image using Invoke-WebRequest and -OutBuffer
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $StratPrefix$ImageUrl -OutFile "$OUTPUT" -ErrorAction Stop -UseBasicParsing -OutBuffer 65536
}
catch {
Write-Host "Error downloading image: $_" -ForegroundColor Red
}
}
else {
Write-Host "Null file..." -ForegroundColor Red
}
}
}
} -ThrottleLimit 10
# Wait for all jobs to complete and receive results
Get-Job | Wait-Job | Receive-Job
}
else {
Write-Host "Please check named `TEMP` file is available..."
}
}
DownTelegraph