Last active
October 26, 2017 09:58
-
-
Save pkillnine/05edd311cc54c2d58477906f4c0783f5 to your computer and use it in GitHub Desktop.
qutebrowser startpage config v0.1 - requires `sqlite3` in your shell's path.
This file contains 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
N_OF_URLS = 10 | |
WITHIN_LAST_DAYS = 31 | |
HISTORYFILE = '{}/history.sqlite'.format(config.datadir) | |
STARTPAGE_DIR = str(config.datadir) | |
STARTPAGE_TITLE = "There's no place like ~" | |
TEMPLATE_FILE = "template.html" # Note: this path is relative to the path specified in STARTPAGE_DIR | |
def get_top_urls(n=N_OF_URLS, within_last_days=WITHIN_LAST_DAYS, historyfile=HISTORYFILE): | |
"""Read the history.sqlite file to get a list of the most accessed pages""" | |
import subprocess | |
import datetime | |
import time | |
datetime_cutoff = datetime.datetime.now() - datetime.timedelta(days = within_last_days) | |
atime_cutoff = time.mktime(datetime_cutoff.timetuple()) | |
SQL_QUERY = """ | |
select url from | |
(SELECT url, title, atime FROM History WHERE atime > {} | |
GROUP BY url ORDER BY count(*) DESC) | |
""".format(atime_cutoff) | |
top_urls = subprocess.check_output(['sqlite3', historyfile, SQL_QUERY]) | |
top_urls = top_urls.decode() | |
top_urls_unfiltered = top_urls.split('\n') | |
top_urls = [] | |
for url in top_urls_unfiltered: | |
#--blacklist not working atm-- | |
#for b in TOP_URLS_BLACKLIST: | |
# if b in url: continue | |
if 'duckduckgo' in url: continue | |
top_urls.append(url) | |
return top_urls[:n] | |
def generate_startpage_html(urls, title=STARTPAGE_TITLE, template_file=TEMPLATE_FILE, startpage_dir=STARTPAGE_DIR): | |
import jinja2 | |
template_loader = jinja2.FileSystemLoader(searchpath=startpage_dir) | |
template_env = jinja2.Environment(loader=template_loader) | |
template = template_env.get_template(template_file) | |
template_vars = {'top_urls' : urls, 'title' : title} | |
startpage_html = template.render(template_vars) | |
#Maybe set to something else if it fails. Maybe show error on front page. | |
return startpage_html | |
top_urls = get_top_urls() | |
startpage_html = generate_startpage_html(top_urls) | |
with open('{}/startpage.html'.format(STARTPAGE_DIR), 'w') as f: | |
f.write(startpage_html) | |
c.url.start_pages = '{}/startpage.html'.format(STARTPAGE_DIR) | |
c.url.default_page = '{}/startpage.html'.format(STARTPAGE_DIR) |
This file contains 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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>{{ title }}</title> | |
<meta name="description" content="{{ description }}" /> | |
<style type="text/css"> | |
#content { | |
margin: auto; | |
padding: 10px; | |
width: 50%; | |
border: 3px solid blue; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="content"> | |
<p>Commonly accessed pages:</p> | |
<ul> | |
{% for item in top_urls %} | |
<li><a href='{{ item }}'>{{ item }}</a></li> | |
{% endfor %} | |
</ul> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment