Skip to content

Instantly share code, notes, and snippets.

@outloudvi
Created December 18, 2019 13:25
Show Gist options
  • Save outloudvi/5adb16f1958482fe7fe29c270fbe2b90 to your computer and use it in GitHub Desktop.
Save outloudvi/5adb16f1958482fe7fe29c270fbe2b90 to your computer and use it in GitHub Desktop.
This file briefly analyze your places.sqlite and give you the most-frequently-visited site list.

analyze_places.py

This file briefly analyze your places.sqlite and give you the most-frequently-visited site list.

To find your places.sqlite, visit about:support in your Firefox browser, click "Open Directory" on the line of "Profile Directory", and you will see the file.

#!/usr/bin/env python3
SHOW_FIRST_X_SITES=30
def strfix(st):
if st == ".":
return "(files:///)"
if st[0] == ".":
return st[1:]
return st
import sqlite3
db = sqlite3.connect("places.sqlite")
cur = db.cursor()
cur.execute("SELECT * FROM moz_places")
ret = []
sites = {}
while True:
try:
one = cur.fetchone()
except sqlite3.DatabaseError:
# DatabaseError: database disk image is malformed
break
if one is not None:
ret.append(one)
if one[3] not in sites:
sites[one[3]] = int(one[4])
else:
sites[one[3]] += int(one[4])
else:
break
print("Total entries: " + str(len(ret)))
siteRank = []
for i in sites.keys():
siteRank.append((strfix(i[::-1]), sites[i]))
siteRank.sort(key=lambda s:s[1], reverse=True)
rankTill = min(SHOW_FIRST_X_SITES, len(siteRank) - 1)
ts1 = len(str(rankTill))
ts2 = 0
ts3 = 0
for i in range(0, rankTill):
ts2 = max(ts2, len(siteRank[i][0]))
ts3 = max(ts3, len(str(siteRank[i][1])))
for i in range(0, rankTill):
print("Rank #%{}d: %{}s (%{}d times)".format(ts1, ts2, ts3) % (i+1, siteRank[i][0], siteRank[i][1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment