AutoYADM commit: 2025-06-03 19:50:18
This commit is contained in:
parent
f4b83f2b5a
commit
d962ff6dc2
2 changed files with 75 additions and 5 deletions
|
@ -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"]["<Escape>"] = "click-element css main"
|
||||
c.url.searchengines["w"] = (
|
||||
"https://en.wikipedia.org/w/index.php?title=Special:Search&search={}"
|
||||
)
|
||||
config.bind(
|
||||
"<Shift-Escape>",
|
||||
"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"],
|
||||
|
|
68
.config/qutebrowser/userscripts/getbib
Normal file
68
.config/qutebrowser/userscripts/getbib
Normal file
|
@ -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")
|
Loading…
Add table
Add a link
Reference in a new issue