Created
September 25, 2025 14:07
-
-
Save lzap/77f7a777dd0cccbd21c9690760697d03 to your computer and use it in GitHub Desktop.
EEEEH - what I was working on today and yesterday? (Chrome on MacOS version)
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| import sqlite3 | |
| import os | |
| import re | |
| import shutil | |
| import tempfile | |
| from datetime import datetime, timedelta, timezone | |
| def get_chrome_history_path(): | |
| """ | |
| Finds the path to the Chrome history file on macOS. | |
| """ | |
| home = os.path.expanduser("~") | |
| history_path = os.path.join( | |
| home, "Library/Application Support/Google/Chrome/Default/History" | |
| ) | |
| return history_path | |
| def main(): | |
| """ | |
| Main function to query Chrome history for unique GitHub PRs and Issues from the last 24 hours. | |
| """ | |
| history_db = get_chrome_history_path() | |
| if not os.path.exists(history_db): | |
| print("Error: Chrome history database not found.") | |
| print(f"Looked for: {history_db}") | |
| return | |
| # Chrome locks its database file while running. We must copy it to a temporary | |
| # location to avoid the lock and to prevent unnecessary SSD wear. | |
| temp_dir = tempfile.gettempdir() | |
| temp_db = os.path.join(temp_dir, f"chrome_history_copy_{os.getpid()}.db") | |
| try: | |
| shutil.copyfile(history_db, temp_db) | |
| # Connect to the copied database | |
| con = sqlite3.connect(temp_db) | |
| cursor = con.cursor() | |
| # Calculate the timestamp for 24 hours ago | |
| # Chrome stores timestamps in microseconds since Jan 1, 1601 (UTC) | |
| # Use timezone-aware datetimes to avoid deprecation warnings | |
| time_24_hours_ago = datetime.now(timezone.utc) - timedelta(days=1) | |
| epoch_start = datetime(1601, 1, 1, tzinfo=timezone.utc) | |
| chrome_epoch_offset = (time_24_hours_ago - epoch_start).total_seconds() * 1000000 | |
| # SQL query to select URLs from the 'urls' table from the last day | |
| query = f""" | |
| SELECT url, title | |
| FROM urls | |
| WHERE last_visit_time > {int(chrome_epoch_offset)} | |
| ORDER BY last_visit_time DESC | |
| """ | |
| cursor.execute(query) | |
| # Use a dictionary to store unique items. In Python 3.7+, dicts preserve | |
| # insertion order. Since we query with DESC, we'll get items in reverse | |
| # chronological order of their last visit. | |
| unique_items = {} | |
| # Regex to match GitHub PRs/Issues and Red Hat Jira issues | |
| url_pattern = re.compile( | |
| r"(https://github\.com/.+?/.+?/(pull|issues)/\d+)|" | |
| r"(https://issues\.redhat\.com/browse/[A-Za-z]+-\d+)" | |
| ) | |
| for url, title in cursor.fetchall(): | |
| match = url_pattern.match(url) | |
| if match: | |
| # To get only the base URL without query params or fragments | |
| base_url = match.group(0) | |
| if base_url not in unique_items: | |
| unique_items[base_url] = title.strip() | |
| print("--- Activity from the Last 24 Hours (in chronological order) ---") | |
| if not unique_items: | |
| print("No unique GitHub or Jira URLs found in the last 24 hours.") | |
| else: | |
| # ANSI escape codes for terminal formatting | |
| BOLD = '\033[1m' | |
| RESET = '\033[0m' | |
| # Reverse the dictionary items to print in chronological order | |
| for url, title in reversed(list(unique_items.items())): | |
| if "by lzap" in title: | |
| print(f"{BOLD}- {title}{RESET}") | |
| print(f"{BOLD} {url}{RESET}\n") | |
| else: | |
| print(f"- {title}") | |
| print(f" {url}\n") | |
| except (sqlite3.Error, IOError) as e: | |
| print(f"An error occurred: {e}") | |
| finally: | |
| if 'con' in locals() and con: | |
| con.close() | |
| # Clean up the temporary database file | |
| if os.path.exists(temp_db): | |
| os.remove(temp_db) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment