Skip to content

Instantly share code, notes, and snippets.

@ymkim92
Created July 12, 2021 05:38
Show Gist options
  • Save ymkim92/a379d700a00c3841933f9a111f315364 to your computer and use it in GitHub Desktop.
Save ymkim92/a379d700a00c3841933f9a111f315364 to your computer and use it in GitHub Desktop.
Read some columns from a table of firefox history data
import sqlite3
import click
from datetime import datetime
DB = 'places.sqlite'
@click.command()
@click.option('-s', '--start-time', type=click.DateTime(formats=["%Y-%m-%dT%H:%M"]))
@click.option('-e', '--end-time', type=click.DateTime(formats=["%Y-%m-%dT%H:%M"]))
def firefox_history(start_time, end_time):
""" e.g., $ python read.py -s 2021-07-10T09:30 -e 2021-07-10T10:40
"""
conn = sqlite3.connect(DB)
cur = conn.cursor()
sql = "SELECT url, visit_count, last_visit_date FROM moz_places "
if start_time:
start_time = start_time.timestamp()*1e6
if end_time:
end_time = end_time.timestamp()*1e6
if start_time and end_time:
sql += f"WHERE last_visit_date>{start_time} and last_visit_date < {end_time};"
elif start_time:
sql += f"WHERE last_visit_date>{start_time};"
elif end_time:
sql += f"WHERE last_visit_date<{end_time};"
else:
sql += ";"
cur.execute(sql)
rows = cur.fetchall()
for row in rows:
try:
t_sec = int(row[2])/1e6
dt = datetime.fromtimestamp(t_sec)
print(f"{row[0]}, {row[1]}, {dt}")
except TypeError:
print("Skip..")
if __name__ == '__main__':
firefox_history()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment