Skip to content

Instantly share code, notes, and snippets.

@yunruse
Last active January 1, 2022 17:15
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 yunruse/aa9c311e15cd032118f1647391d06efb to your computer and use it in GitHub Desktop.
Save yunruse/aa9c311e15cd032118f1647391d06efb to your computer and use it in GitHub Desktop.
Extract Safari history
import sqlite3
from typing import NamedTuple
from datetime import datetime
HISTORY_EPOCH = 978307200
class Visit(NamedTuple):
date: datetime
title: str
url: str
def history(path):
'''
Extract visit times from Safari browser history.
This is typically stored at ~/Library/Safari/History.db
'''
cnx = sqlite3.connect(path)
cur = cnx.cursor()
cur.execute('''
SELECT visit_time, title, url
FROM history_items INNER JOIN history_visits
ON history_visits.history_item = history_items.id;
''')
data = cur.fetchall()
cur.close()
cnx.close()
for date, title, url in data:
date = datetime.fromtimestamp(HISTORY_EPOCH + date)
yield Visit(date, title, url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment