From d962ff6dc2b43e2fba034181337c0925c1e24fdd Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Tue, 3 Jun 2025 19:50:18 -0400 Subject: [PATCH] AutoYADM commit: 2025-06-03 19:50:18 --- .config/qutebrowser/config.py | 12 +++-- .config/qutebrowser/userscripts/getbib | 68 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 .config/qutebrowser/userscripts/getbib diff --git a/.config/qutebrowser/config.py b/.config/qutebrowser/config.py index 0771ed53..2f27d134 100644 --- a/.config/qutebrowser/config.py +++ b/.config/qutebrowser/config.py @@ -36,14 +36,15 @@ c.colors.webpage.preferred_color_scheme = "dark" # searches c.url.searchengines["DEFAULT"] = "https://www.startpage.com/sp/search?query={}" -c.url.searchengines["!d"] = "https://duckduckgo.com/?q={}" -c.url.searchengines["!aw"] = "https://wiki.archlinux.org/?search={}" -c.url.searchengines["!g"] = ( +c.url.searchengines["d"] = "https://duckduckgo.com/?q={}" +c.url.searchengines["aw"] = "https://wiki.archlinux.org/?search={}" +c.url.searchengines["g"] = ( "http://www.google.com/search?hl=en&source=hp&ie=ISO-8859-l&q={}" ) c.url.searchengines["ap"] = "https://www.archlinux.org/packages/?sort=&q={}" -# with config.pattern("chatgpt.com") as p: -# p.bindings.commands["normal"][""] = "click-element css main" +c.url.searchengines["w"] = ( + "https://en.wikipedia.org/w/index.php?title=Special:Search&search={}" +) config.bind( "", "mode-leave ;; jseval -q document.activeElement.blur()", @@ -58,6 +59,7 @@ config.bind( sets = { "normal": [ + ["\\", "mode-enter passthrough"], ["m", "scroll left"], ["n", "scroll down"], ["e", "scroll up"], diff --git a/.config/qutebrowser/userscripts/getbib b/.config/qutebrowser/userscripts/getbib new file mode 100644 index 00000000..0ab0ba54 --- /dev/null +++ b/.config/qutebrowser/userscripts/getbib @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Qutebrowser userscript scraping the current web page for DOIs and downloading +corresponding bibtex information. + +Set the environment variable 'QUTE_BIB_FILEPATH' to indicate the path to +download to. Otherwise, bibtex information is downloaded to '/tmp' and hence +deleted at reboot. + +Installation: see qute://help/userscripts.html + +Inspired by +https://ocefpaf.github.io/python4oceanographers/blog/2014/05/19/doi2bibtex/ +""" + +import os +import sys +import re +from collections import Counter +from urllib import parse as url_parse +from urllib import request as url_request + + +FIFO_PATH = os.getenv("QUTE_FIFO") + +def message_fifo(message, level="warning"): + """Send message to qutebrowser FIFO. The level must be one of 'info', + 'warning' (default) or 'error'.""" + with open(FIFO_PATH, "w") as fifo: + fifo.write("message-{} '{}'".format(level, message)) + + +source = os.getenv("QUTE_TEXT") +with open(source) as f: + text = f.read() + +# find DOIs on page using regex +dval = re.compile(r'(10\.(\d)+/([^(\s\>\"\<)])+)') +# https://stackoverflow.com/a/10324802/3865876, too strict +# dval = re.compile(r'\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b') +dois = dval.findall(text) +dois = Counter(e[0] for e in dois) +try: + doi = dois.most_common(1)[0][0] +except IndexError: + message_fifo("No DOIs found on page") + sys.exit() +message_fifo("Found {} DOIs on page, selecting {}".format(len(dois), doi), + level="info") + +# get bibtex data corresponding to DOI +url = "https://dx.doi.org/" + url_parse.quote(doi) +headers = dict(Accept='text/bibliography; style=bibtex') +request = url_request.Request(url, headers=headers) +response = url_request.urlopen(request) +status_code = response.getcode() +if status_code >= 400: + message_fifo("Request returned {}".format(status_code)) + sys.exit() + +# obtain content and format it +bibtex = response.read().decode("utf-8").strip() +bibtex = bibtex.replace(" ", "\n ", 1).\ + replace("}, ", "},\n ").replace("}}", "}\n}") + +# append to file +bib_filepath = os.getenv("QUTE_BIB_FILEPATH", "/tmp/qute.bib") +with open(bib_filepath, "a") as f: + f.write(bibtex + "\n\n")