Have you ever wondered how to convert your Pocket export files to a format that can be easily imported as bookmarks into Safari? Well, I've got just the solution for you, and it involves a pinch of Python and some HTML parsing magic. In this blog post, I will walk you through a Python script that accomplishes this task, step by step.
The Python script relies heavily on BeautifulSoup, a library that is fantastic for web scraping and parsing HTML files. The first part of the script contains a function named parse_pocket_export
. This function reads the Pocket export HTML file, parses the HTML content to extract all the link elements, and stores the title and URL of each link in a dictionary. The result is a list of dictionaries, where each dictionary represents a bookmark.
from bs4 import BeautifulSoup
def parse_pocket_export(file_path):
with open(file_path, 'r') as file:
data = file.read()
soup = BeautifulSoup(data, 'html.parser')
links = soup.find_all('a')
pocket_links = [{"title": link.text, "url": link['href']} for link in links]
return pocket_links
The second part of the script is the write_to_bookmark_file
function. This function takes as input the list of dictionaries generated by parse_pocket_export
, and writes them to a new HTML file in a format that can be imported as bookmarks into Safari.
def write_to_bookmark_file(pocket_links, output_file_path):
with open(output_file_path, 'w') as file:
file.write('<!DOCTYPE NETSCAPE-Bookmark-file-1>\n')
# ... (omitted for brevity)
for link in pocket_links:
file.write(f'<DT><A HREF="{link["url"]}" ADD_DATE="" LAST_VISIT="" LAST_MODIFIED="">{link["title"]}</A>\n')
file.write('</DL><p>\n')
To use this script, we need to manage its dependencies. For this, we will use Poetry, a dependency management and packaging tool. Poetry allows us to specify the exact versions of the packages we depend on, and it ensures that these packages are installed in a virtual environment. To use Poetry, first install it using the command curl -sSL
https://install.python-poetry.org
| python3 -
.
Next, to specify that our script depends on BeautifulSoup, create a new file called pyproject.toml
in the same directory as our script, and add the following content:
[tool.poetry]
name = "pocket-to-safari"
version = "0.1.0"
description = "Converts Pocket exports to Safari bookmarks."
[tool.poetry.dependencies]
python = "^3.7"
beautifulsoup4 = "^4.9.3"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
We also want to ensure that our code is working as expected. For this, we can write tests using Python's built-in unittest
library. Here's a simple test case for our parse_pocket_export
function:
import unittest
from pocket_to_safari import parse_pocket_export
class TestPocketToSafari(unittest.TestCase):
def test_parse_pocket_export(self):
pocket_links = parse_pocket_export('pocket_export.html')
self.assertIsInstance(pocket_links, list)
self.assertGreater(len(pocket_links), 0)
self.assertIsInstance(pocket_links[0], dict)
self.assertIn('title', pocket_links[0])
self.assertIn('url', pocket_links[0])
if __name__ == "__main__":
unittest.main()
This test checks that parse_pocket_export
returns a list of dictionaries and that each dictionary has a 'title' and 'url' key.
Finally, to import your new bookmarks file into Safari, follow these simple steps:
Open Safari.
Click on File in the menu.
Select Import From -> Bookmarks HTML File.
Navigate to your created HTML file and select it.
That's it! I hope this guide has been helpful in teaching you how to convert Pocket exports to Safari bookmarks using Python, BeautifulSoup, and Poetry. Happy coding!