Skip to content

Instantly share code, notes, and snippets.

@elazar
Created October 17, 2023 01:33
Show Gist options
  • Save elazar/d2c519736c79d68ee7da966c54c984c2 to your computer and use it in GitHub Desktop.
Save elazar/d2c519736c79d68ee7da966c54c984c2 to your computer and use it in GitHub Desktop.
Convert Refind export to Postmarks SQL
<?php
$date = fn(int $timestamp): string => date('Y-m-d H:i:s', $timestamp);
$doc = XMLReader::open('refind collection links.html', null, LIBXML_NOERROR | LIBXML_NOWARNING);
$links = [];
$currentTag = null;
while ($doc->read()) {
if ($doc->nodeType == XMLReader::ELEMENT) {
if ($doc->localName === 'h3') {
$currentTag = $doc->readString();
if ($currentTag === 'Dungeons and Dragons') {
$currentTag = 'dnd';
}
} elseif ($doc->localName === 'a') {
$url = $doc->getAttribute('href');
if (isset($links[$url])) {
$links[$url]['tags'][] = $currentTag;
} else {
$links[$url] = [
'title' => html_entity_decode(trim($doc->readString())),
'createdAt' => $date($doc->getAttribute('add_date')),
'updatedAt' => $date($doc->getAttribute('last_visit')),
'tags' => [$currentTag],
];
}
}
}
}
foreach ($links as $url => $attributes) {
$attributes['tags'] = array_filter(
$attributes['tags'],
fn(string $tag): bool => preg_match('/[A-Z]/', $tag) === 0
);
$attributes['tags'] = array_map(
fn(string $tag): string => '#' . preg_replace('/\s+/', '-', $tag),
$attributes['tags']
);
$attributes['tags'] = implode(' ', $attributes['tags']);
$attributes['url'] = $url;
$attributes = array_map(
fn(string $value): string => "'" . str_replace("'", "''", $value) . "'",
$attributes
);
echo 'INSERT INTO bookmarks (title, url, tags, created_at, updated_at) VALUES ('
. $attributes['title'] . ', '
. $attributes['url'] . ', '
. $attributes['tags'] . ', '
. $attributes['createdAt'] . ', '
. $attributes['updatedAt']
. ');' . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment