Skip to content

Instantly share code, notes, and snippets.

@y-ack
Created November 10, 2017 02:46
Show Gist options
  • Save y-ack/223cbbeb0d67aeacbdb70745ced7d7a8 to your computer and use it in GitHub Desktop.
Save y-ack/223cbbeb0d67aeacbdb70745ced7d7a8 to your computer and use it in GitHub Desktop.
Very dirty script used to generate JSON archives of miiverse
#$MiiverseList = Invoke-RestMethod "https://pastebin.com/raw/4kxfQWSH"
#$MiiverseList = $MiiverseList -split "`r`n"
$MiiverseList = dir .\chat_links | ? name -match "^AY" | select name
#$MiiverseList = @("https://miiverse.nintendo.net/replies/AYMHAAADAAB2V0e0LQKOBA")
#$SaveDirectory = "$home/miiverse"
$SaveDirectory = "$home/snail_miiverse"
mkdir $SaveDirectory
mkdir "$SaveDirectory/images/"
#$URLRoot = "https://miiverse.nintendo.net"
$URLRoot = "http://oboy.smilebasicsource.com:49000/random/chatlinks"
#$URLRoot = "http://localhost:8000"
$RepliesDangler = '/replies?page_param={"per_page"%3A120%2C"order"%3A"asc"}&selected=all'
#$BrowserFrame = New-Object -ComObject InternetExplorer.Application #Local file hack
function Get-MiiverseThreadBackLink {
param([Parameter(ValueFromPipeline=$True)][String]$URL)
return $URLRoot+((Invoke-WebRequest $URL).Links | ? {$_.href -match "/posts/" }).href
}
function Ungarble-Text {
param([Parameter(ValueFromPipeline=$True)][String]$String)
return [System.Text.Encoding]::utf8.GetString([System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($string))
}
function Archive-MiiversePage {
param([Parameter(ValueFromPipeline=$True)][String]$URL,
[switch]$local,
[switch]$shortreplies)
$filename = $URL[($URL.LastIndexOf("/")+1)..($URL.length-6)] -join ''
try {
if ($local) {
$BrowserFrame.Navigate("$URL")
while ($BrowserFrame.busy -eq $true) {Start-Sleep -Milliseconds 100}
#Start-Sleep -Milliseconds 600
$page = $BrowserFrame.Document
}
else { $page = Invoke-WebRequest $URL
$page = $page.parsedHtml}
} catch { Write-Host "$URL failed:"
Write-Host $_
Write-Host "skipping deleted $filename";return}
if ($page.getElementsByClassName("user-content").length -eq 0) {Write-Host "skipping deleted $filename";return}
$ThreadObject = New-Object Object
Write-Host "$SaveDirectory/images/$filename/"
if (Test-Path "$SaveDirectory/images/$filename/"){Write-Host "skipping duplicate $filename";break}
mkdir "$SaveDirectory/images/$filename/"
# Root Post
$postHeader = $page.getElementsByClassName("user-content")[0]
$postContent = $page.getElementsByClassName("post-list-outline")[0].getElementsByClassName("body")[0]
$ThreadObject | Add-Member -NotePropertyName url -NotePropertyValue $URL
$Community = $page.getElementsByClassName("community-container-heading")[0].innerText
$ThreadObject | Add-Member -NotePropertyName community -NotePropertyValue $Community
$Post = New-Object Object
$Post | Add-Member -NotePropertyName datetime -NotePropertyValue ([DateTime]::Parse($postHeader.getElementsByClassName("timestamp")[0].innerText))
$Post | Add-Member -NotePropertyName user -NotePropertyValue @{
username = $postHeader.querySelector(".user-name a").innerText | Ungarble-Text
NNID = $postHeader.querySelector(".user-name a").nameProp | Ungarble-Text
}
if ($postContent.getElementsByClassName("screenshot-container").length -ne 0) {
$Screenshot = New-Object Object
$Screenshot | Add-Member -NotePropertyName origin -NotePropertyValue $postContent.querySelector(".screenshot-container img").src
$imagename = $postContent.querySelector(".screenshot-container img").src
$imagename = $imagename.substring($imagename.lastIndexOf("/")+1)
Invoke-WebRequest $postContent.querySelector(".screenshot-container img").src -OutFile "$SaveDirectory/images/$filename/$imagename"
$Screenshot | Add-Member -NotePropertyName file -NotePropertyValue "images/$filename/$imagename"
$Post | ADd-Member -NotePropertyName screenshot -NotePropertyValue $Screenshot
}
if ($postContent.getElementsByClassName("post-content-memo").length -ne 0) {
$Memo = New-Object Object
$Memo | Add-Member -NotePropertyName origin -NotePropertyValue $postContent.querySelector("img.post-memo").src
$imagename = $postContent.querySelector("img.post-memo").src
$imagename = $imagename.substring($imagename.lastIndexOf("/")+1)
Invoke-WebRequest $postContent.querySelector("img.post-memo").src -OutFile "$SaveDirectory/images/$filename/$imagename"
$Memo | Add-Member -NotePropertyName file -NotePropertyValue "images/$filename/$imagename"
$Post | Add-Member -NotePropertyName memo -NotePropertyValue $Memo
}
if ($postContent.getElementsByClassName("post-content-text").length -ne 0) {
$Post | Add-Member -NotePropertyName text -NotePropertyValue ($postContent.getElementsByClassName("post-content-text")[0].innerText | Ungarble-Text)
}
$Post | Add-Member -NotePropertyName yeahs -NotePropertyValue $postContent.getElementsByClassName("empathy-count")[0].innerText
$Post | Add-Member -NotePropertyName replycount -NotePropertyValue $postContent.getElementsByClassName("reply-count")[0].innerText
$ThreadObject | Add-Member -NotePropertyName post -NotePropertyValue $Post
# Replies
if ($shortreplies) {
$repliesHtml = $Page.querySelector(".reply-list")
} else {
$repliesHtml = Invoke-WebRequest "$URL$RepliesDangler"
}
$replyArray = @()
$repliesHtml.getElementsByTagName("li") | ForEach-Object {
$reply = New-Object Object
$reply | Add-Member -NotePropertyName datetime -NotePropertyValue ([DateTime]::Parse($_.getElementsByClassName("timestamp")[0].innerText))
$user = New-Object Object
$user | Add-Member -NotePropertyName username -NotePropertyValue ($_.getElementsByTagName("a")[1].innerText | Ungarble-Text)
$user | Add-Member -NotePropertyName NNID -NotePropertyValue $_.getElementsByTagName("a")[1].nameProp
$reply | Add-Member -NotePropertyName user -NotePropertyValue $user
if ($_.getElementsByClassName("screenshot-container").length -ne 0) {
$Screenshot = New-Object Object
$Screenshot | Add-Member -NotePropertyName origin -NotePropertyValue $_.getElementsByClassName("screenshot-container")[0].childNodes[0].src
$imagename = $_.getElementsByClassName("screenshot-container")[0].childNodes[0].src
$imagename = $imagename.substring($imagename.lastIndexOf("/")+1)
Invoke-WebRequest $_.getElementsByClassName("screenshot-container")[0].childNodes[0].src -OutFile "$SaveDirectory/images/$filename/$imagename"
$Screenshot | Add-Member -NotePropertyName file -NotePropertyValue "images/$filename/$imagename"
$reply | Add-Member -NotePropertyName screenshot -NotePropertyValue $Screenshot
}
if ($_.getElementsByClassName("reply-content-memo").length -ne 0) {
$Memo = New-Object Object
$Memo | Add-Member -NotePropertyName origin -NotePropertyValue $_.getElementsByClassName("reply-content-memo")[0].childNodes[0].src
$imagename = $_.getElementsByClassName("reply-content-memo")[0].childNodes[0].src
$imagename = $imagename.substring($imagename.lastIndexOf("/")+1)
Invoke-WebRequest $_.getElementsByClassName("reply-content-memo")[0].childNodes[0].src -OutFile "$SaveDirectory/images/$filename/$imagename"
$Memo | Add-Member -NotePropertyName file -NotePropertyValue "images/$filename/$imagename"
$reply | Add-Member -NotePropertyName memo -NotePropertyValue $Memo
}
if ($_.getElementsByClassName("reply-content-text").length -ne 0) {
$reply | Add-Member -NotePropertyName text -NotePropertyValue ($_.getElementsByClassName("reply-content-text")[0].innerText | Ungarble-Text)
}
$reply | Add-Member -NotePropertyName yeahs -NotePropertyValue $_.getElementsByClassName("empathy-count")[0].innerText
$replyArray += $reply
}
$ThreadObject | Add-Member -NotePropertyName replies -NotePropertyValue $replyArray
$ThreadObject | ConvertTo-Json | Out-File "$SaveDirectory/$filename.json" -Encoding utf8
return $ThreadObject
}
$MiiverseList | ForEach-Object {
$a = New-Object Object
$file = $_.name
Archive-MiiversePage "$URLRoot/$file" -shortreplies
#
switch -regex ($_) {
"/posts/" { $a = Archive-MiiversePage $_}
"/replies/" {$a = Get-MiiverseThreadBackLink $_ | Archive-MiiversePage}
}
}
#$BrowserFrame.Quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment