Skip to content

Instantly share code, notes, and snippets.

@jancbeck
Created August 30, 2023 16:04
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 jancbeck/f749031aa4f65b5dead744620e650a4e to your computer and use it in GitHub Desktop.
Save jancbeck/f749031aa4f65b5dead744620e650a4e to your computer and use it in GitHub Desktop.
Takes a Safari exported reading list JSON and converts to Pocket's export format
from datetime import datetime
from bs4 import BeautifulSoup
# Create a BeautifulSoup object with the basic HTML structure
html_structure = '''
<!DOCTYPE html>
<html>
<head>
<title>Reading List</title>
</head>
<body>
<h1>Unread</h1>
<ul id="unread_list">
</ul>
<h1>Read</h1>
<ul id="read_list">
</ul>
</body>
</html>
'''
soup = BeautifulSoup(html_structure, 'html.parser')
# Locate the ul elements for read and unread articles
unread_ul = soup.find(id='unread_list')
read_ul = soup.find(id='read_list')
# Convert JSON data to HTML
# Here, I'll assume that all articles are unread as there is no information in the JSON data to distinguish read from unread articles.
for article in reading_list_data:
# Extract necessary information
title = article.get('title', 'Unknown Title')
url = article.get('URLString', '#')
date_added = article.get('DateAdded', 'Unknown Date')
# Convert date to timestamp (assuming date is in 'YYYY-MM-DD HH:MM:SS' format)
try:
time_added = int(datetime.strptime(date_added, '%Y-%m-%d %H:%M:%S').timestamp())
except Exception as e:
time_added = 0
# Create a new list item with an embedded link
new_li = soup.new_tag('li')
new_a = soup.new_tag('a', href=url, time_added=str(time_added), tags='')
new_a.string = title
new_li.append(new_a)
# Append to the unread list (since we assumed all are unread)
unread_ul.append(new_li)
# Save the BeautifulSoup object as an HTML file
html_output_path = '/mnt/data/reading_list.html'
with open(html_output_path, 'w') as file:
file.write(str(soup))
html_output_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment