Last active
January 1, 2022 17:15
-
-
Save yunruse/aa9c311e15cd032118f1647391d06efb to your computer and use it in GitHub Desktop.
Extract Safari history
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
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