-
Notifications
You must be signed in to change notification settings - Fork 0
/
_bukuaddbase
executable file
·70 lines (59 loc) · 2.14 KB
/
_bukuaddbase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# Adds buku bookmark and sets time_added field in database. Also does some cleaning of metadata
# $1 - URL
# $2 - Tags
import sys
import subprocess
import re
import sqlite3
import os
from datetime import datetime
if len(sys.argv) < 2:
print("No URL given")
sys.exit(1)
url = sys.argv[1]
url_without_prefix = url
prefixes = ["https://","http://","www.","m."]
for p in prefixes:
if url_without_prefix.startswith(p):
url_without_prefix = url_without_prefix[len(p):]
tags = sys.argv[2] if len(sys.argv) > 2 else ""
autotag_path = os.environ["HOME"] + "/.config/buku/autotag"
if os.path.isfile(autotag_path):
with open(autotag_path, "r") as f:
for line in f:
parts = line.split(" ")
autotag_url, autotag_tags = parts[0], "".join(parts[1:])
if url_without_prefix.startswith(autotag_url):
tags += "," + autotag_tags
break
out = subprocess.check_output(["buku", "--nc", "-a", url, tags]).decode("utf-8")
new_id = re.match(r"[0-9]+", out)
if new_id:
new_id = new_id.group()
print(f"Added with id {new_id}")
else:
print("Bad buku -a output")
sys.exit(1)
con = sqlite3.connect(os.environ["HOME"] + "/.local/share/buku/bookmarks.db")
cur = con.cursor()
# Potentially clean metadata fetched from <meta> tags
title, desc = cur.execute(f"select metadata,desc from bookmarks where id = {new_id}").fetchone()
wipe_regex = [
r"^github - ", # GH tends to prefix page title with 'GitHub - '
r"\. contribute to .*$", # GH tends to add a contribute to message
r" - github - .*$" # sometimes GH repeats the repo description
]
strings_to_clean = [title, desc]
for wp in wipe_regex:
for i in range(len(strings_to_clean)):
m = re.search(wp, strings_to_clean[i],re.IGNORECASE)
if m:
strings_to_clean[i] = strings_to_clean[i].replace(m.group(), "")
title, desc = strings_to_clean
current_time = datetime.today().strftime("%Y-%m-%d %H:%M:%S")
sql_command = f"update bookmarks set time_added ='{current_time}', metadata = ?, desc = ? where id = {new_id}"
cur.execute(sql_command, (title,desc))
cur.close()
con.commit()
con.close()