Instantly share code, notes, and snippets.

Embed
What would you like to do?

The PowerShell script shown below can be used to archive a Hacker News post as an Emacs Org mode file.

Example invocation:

The post-to-org function is passed the story ID.

Resulting org file:

Script:

function hacker-news-item ($id)
{
    Invoke-RestMethod -Uri "https://hacker-news.firebaseio.com/v0/item/$id.json?print=pretty"
}

function render-text ($text)
{
    [System.Web.HttpUtility]::HtmlDecode(
        ((($text -replace '<p>', "`r`n`r`n") -replace "`r`n\* ", "`r`n- ") -replace '<a href="(.+?)".*>(.+)</a>', '[[$1][$2]]')
    )
}

$counter = 0

function show-comment ($id, $file, $indent=2)
{
    # Write-Host -NoNewline '-'

    # $counter++; Write-Host "$counter "

    $Global:counter++

    Write-Host -NoNewline "$counter "

    $result_comment = hacker-news-item $id

    ('*' * $indent)                  | Out-File -Append -NoNewline $file
    ' '                              | Out-File -Append -NoNewline $file
    $result_comment.by               | Out-File -Append -NoNewline $file
    ' '                              | Out-File -Append -NoNewline $file
                                     
    ''                               | Out-File -Append            $file
    render-text $result_comment.text | Out-File -Append            $file
    ''                               | Out-File -Append            $file

    foreach ($id in $result_comment.kids)
    {
        show-comment $id $file ($indent + 1)
    }

}

function post-to-org ($id)
{
    $result_story = hacker-news-item $id

    $file = "c:\temp\hn-$id.org" 

    "* $($result_story.title)" | Out-File $file

$properties = @"

:PROPERTIES:
:url:       $($result_story.url)
:id:        $($result_story.id)
:score:     $($result_story.score)
:by:        $($result_story.by)
:END:

"@

    $properties                    | Out-File -Append $file
    render-text $result_story.text | Out-File -Append $file


    Write-Host "Number of comments: $($result_story.descendants)"

    # $counter = 0

    $Global:counter = 0
    
    foreach ($id in $result_story.kids)
    {
        $result_comment = hacker-news-item $id

        show-comment $id $file
    }
}

# The Early History Of Smalltalk (1993)
# post-to-org 17913668
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment