Skip to content

Commit

Permalink
feat: import Pocket bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
fedragon committed Jul 5, 2024
1 parent 63f02f9 commit 46f74e9
Show file tree
Hide file tree
Showing 7 changed files with 850 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ To install the bookmarklet in your browser:
- select "Add Bookmark", then
- paste the contents of [bookmarklet/bookmarklet.js](bookmarklet/bookmarklet.js) in the "URL" field

## Bonus: Import Pocket saves

The repository contains an optional importer for [Pocket](https://getpocket.com/) saves in the `pocket-importer` directory. See its README for details.

## Credits

Inspired by [downmark](https://github.com/alessandro-fazzi/downmark).
6 changes: 6 additions & 0 deletions pocket-importer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
*.html
39 changes: 39 additions & 0 deletions pocket-importer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Pocket importer

Imports Pocket saves into Bookmd.

**Disclaimer:** Use at your own risk, no guarantees whatsoever. I've used it to migrate my own Pocket saves and it worked fine except for a few websites.

## Usage

This currently only works with Chrome. Before running, make sure you have Chrome installed and that it is in your PATH.

### Export from Pocket

Open the [Pocket Export](https://getpocket.com/export) page and click on "Export HTML file" link. Copy the file to the `pocket-importer` directory as `pocket-export.html`.

### Run server

From the root of the `bookmd` project, run

```shell
make
bin/server
```

### Run Chrome in debug mode

```shell
/path/to/chrome --remote-debugging-port=1243
```

Playwright [cannot currently deal with 'open in app' dialogs](https://github.com/microsoft/playwright/issues/11014), so we have to ensure they are dismissed: to do so, type `obsidian://` in the address bar, tick the checkbox of the dialog so that it won't show up again, and select `Open in Obsidian`.

### Run the importer

From the `pocket-importer` directory, run

```shell
npm install
npx tsx ./importer.ts
```
46 changes: 46 additions & 0 deletions pocket-importer/importer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {chromium} from 'playwright';
import * as nhp from 'node-html-parser';
import * as fs from "node:fs";

(async () => {
const browser = await chromium.connectOverCDP('http://localhost:1243');
const defaultContext = browser.contexts()[0];
const page = defaultContext.pages()[0];

const serverAddress = 'http://localhost:3333';
const vault = 'my-vault';
const folder = 'Clippings';
const doc = fs.readFileSync('./pocket-export.html', 'utf8');
const root: nhp.HTMLElement = nhp.parse(doc);

const links = root.querySelectorAll('a').map(
(a: nhp.HTMLElement): { url: string, link: string } => {
const tags = a.getAttribute('tags')
.split(",")
.map(tag => 'tag=' + encodeURIComponent(tag.trim()))
.join("&");

const params = [
`url=${encodeURIComponent(a.getAttribute('href'))}`,
`vault=${encodeURIComponent(vault)}`,
`folder=${encodeURIComponent(folder)}`,
`epoch=${a.getAttribute('time_added')}`,
'silent=true',
tags
].join('&')

return {url: a.getAttribute('href'), link: `${serverAddress}/api/bookmarks?${params}`}
}
);

for (const {url, link} of links) {
try {
await page.goto(link);
} catch (err) {
if (err instanceof Error && !err.message.startsWith('page.goto: net::ERR_ABORTED')) {
console.log('failed to open', url, err.name);
}
}
}
await browser.close();
})();
Loading

0 comments on commit 46f74e9

Please sign in to comment.