Skip to content

Instantly share code, notes, and snippets.

@woshichuanqilz
Created July 3, 2024 14:08
Show Gist options
  • Save woshichuanqilz/936f9c8890f0a3693cc18314cd08a149 to your computer and use it in GitHub Desktop.
Save woshichuanqilz/936f9c8890f0a3693cc18314cd08a149 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sqlite3
import shutil
from datetime import datetime, timedelta
import os
# Function to convert a Python datetime to Chrome timestamp format
def datetime_to_chrome_timestamp(dt):
chrome_epoch = datetime(1601, 1, 1)
return int((dt - chrome_epoch).total_seconds() * 1000000)
# Get the current datetime
current_datetime = datetime.now()
# Subtract 24 hours from the current datetime
previous_datetime = current_datetime - timedelta(hours=24)
# Convert the previous datetime to Chrome timestamp format
chrome_timestamp = datetime_to_chrome_timestamp(previous_datetime)
# Copy the Chrome history database file to a temporary location
shutil.copyfile('/home/lizhe/.config/google-chrome/Default/History', 'History_copy.db')
# Connect to the copied database
conn = sqlite3.connect('History_copy.db')
cursor = conn.cursor()
# Query the database to get the count of sites visited today
cursor.execute('SELECT COUNT(DISTINCT url) FROM visits WHERE visit_time >= ?', (chrome_timestamp,))
site_count = cursor.fetchone()[0]
print(f"You have visited {site_count} sites in 24 hours.")
# Close the database connection
conn.close()
# Delete the copied database file
os.remove('History_copy.db')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment