Created
October 17, 2023 01:33
-
-
Save elazar/d2c519736c79d68ee7da966c54c984c2 to your computer and use it in GitHub Desktop.
Convert Refind export to Postmarks SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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