Skip to content

Instantly share code, notes, and snippets.

@intellectronica
Created January 8, 2022 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intellectronica/4e5f3363d891a64c42566d104e881977 to your computer and use it in GitHub Desktop.
Save intellectronica/4e5f3363d891a64c42566d104e881977 to your computer and use it in GitHub Desktop.
# fill auth token here. see https://github.com/nicolevanderhoeven/shortform-to-readwise#usage for how to get it
$authToken = '...'
$booksApiUrl = 'https://www.shortform.com/api/books/'
$bookSlug = $args[0]
$response = Invoke-WebRequest -Uri "$booksApiUrl$bookSlug" -Headers @{
"Authorization" = "Basic $authToken";
"X-Sf-Client" = "11.8.0";
}
$data = ($response.Content | Out-String | ConvertFrom-Json).data
$title = $data.title + " (Summary)"
$author = $data.author + " (Shortform)"
$cover = "https:" + $data.cover_image
$created = $data.created.ToString("yyyy-MM-dd")
$tags = $data.tags
$chapters = $data.content | Where-Object -Property content_type -EQ 'chapter' |
ForEach-Object { @{ "title" = $_.title; "text" = $_.text } }
$chaptersString = ($chapters | ForEach-Object { "`n<h2>" + $_.title + "</h2>`n" + $_.text })
$htmlString = @"
<html>
<head><title>$title- $author</title></head>
<body>
$chaptersString
</body>
</html>
"@
Invoke-WebRequest -Uri $cover -OutFile ".\$bookSlug.png"
Set-Content -Path ".\$bookSlug.html" -Value $htmlString
pandoc --toc -M title=$title -M author=$author -M date=$created -M publisher=Shortform -M cover-image=".\$bookSlug.png" -o ".\$bookSlug-summary.epub" ".\$bookSlug.html"
Remove-Item ".\$bookSlug.html"
Remove-Item ".\$bookSlug.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment