Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Last active September 26, 2021 16:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharmatech/2ef271907cb73298318fdb50a4ee7d54 to your computer and use it in GitHub Desktop.
Save dharmatech/2ef271907cb73298318fdb50a4ee7d54 to your computer and use it in GitHub Desktop.

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
@NightMachinery
Copy link

Has "https://hacker-news.firebaseio.com/v0/item/%s.json?print=pretty" changed? I get TLS errors on it.

@NightMachinery
Copy link

@NightMachinary commented on Sep 26, 2021, 7:55 PM GMT+3:30:

Has "https://hacker-news.firebaseio.com/v0/item/%s.json?print=pretty" changed? I get TLS errors on it.

The problem was with my own network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment