Skip to content

Instantly share code, notes, and snippets.

@dbieber
Created January 5, 2021 02:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbieber/681597cdee886d479446babcb242f512 to your computer and use it in GitHub Desktop.
Save dbieber/681597cdee886d479446babcb242f512 to your computer and use it in GitHub Desktop.
Automatic distraction detection. Detects visits to distraction websites on your phone from your computer.
"""Automatic distraction detection. Work in progress.
Usage:
python detect.py main
"""
import fire
import subprocess
import plyvel
import re
import os
import time
# You may need to replace "Profile 1" with e.g. "Default" for this to work for you.
DB_PATH = os.path.expanduser("~/Library/Application Support/Google/Chrome/Profile 1/Sync Data/LevelDB/")
BACKUP_PATH = os.path.expanduser("~/Profile1/Sync Data/LevelDB/")
def copy(src, dest):
assert 'Library' not in dest
subprocess.call(['rm', '-r', dest])
subprocess.call(['cp', '-r', src, dest])
def backup():
return copy(DB_PATH, BACKUP_PATH)
def parse_urls(path):
db = plyvel.DB(path)
all_data = []
for key, value in db.iterator():
if b'sessions-dt' in key:
data = re.findall(b'https?://[\x20-\x7F]+', value)
if data:
all_data.extend(data)
return all_data
def main():
distraction_domains = [
'twitter.com',
'facebook.com',
'fb.com',
'ycombinator',
'news.google',
'reddit',
'youtube',
]
while True:
copy_path = copy(DB_PATH, BACKUP_PATH)
urls = parse_urls(BACKUP_PATH)
active_distractions = []
for url in urls:
for domain in distraction_domains:
if domain.encode('utf-8') in url:
active_distractions.append(domain)
print(active_distractions)
time.sleep(10)
if __name__ == '__main__':
fire.Fire()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment