-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPinboard.py
95 lines (72 loc) · 2.16 KB
/
Pinboard.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import pinboard
import yaml
import time
import Levenshtein
from collections import defaultdict
from albertv0 import *
# see https://albertlauncher.github.io/docs/extensions/python/
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Pinboard"
__version__ = "0.0.1"
__trigger__ = "pb"
__author__ = "Alexandre Leblanc"
HOME_DIR = os.environ["HOME"]
APITOKEN = os.environ["PINBOARD_API"]
PINBOARD_DIR = os.environ.get("PINBOARD_DIR", os.path.join(HOME_DIR, ".config/pinboard"))
PINBOARD_DB = os.path.join(PINBOARD_DIR, "pinboard_db")
TIME_KEY = "epoch"
def bookmark_dict(bookmark, db):
for tag in bookmark.tags:
db[tag].append([bookmark.description, bookmark.url])
def refresh(pb):
bookmark_list = pb.posts.all()
dict_db = defaultdict(list)
for bm in bookmark_list:
bookmark_dict(bm, dict_db)
dict_db[TIME_KEY] = time.time()
with open(PINBOARD_DB, "r+") as pb:
pb.write(yaml.dump(dict_db))
# The api allow us to fetch once every 5 minutes max
def need_refresh(epoch):
return time.time() - epoch > (60 * 6)
def read_yaml():
with open(PINBOARD_DB, "r+") as pbrc:
db = yaml.load(pbrc.read())
return db
def heuristic(key, string):
return Levenshtein.ratio(key, string) > 0.5
def match_key(string, dict_db):
l = defaultdict(list)
for k, v in dict_db.items():
if heuristic(k, string):
l[k] = v
return l
def run(string):
pb = pinboard.Pinboard(APITOKEN)
yaml_db = read_yaml()
if yaml_db is None or need_refresh(yaml_db.get(TIME_KEY, 0)):
refresh(pb)
yaml_db = read_yaml()
return match_key(string, yaml_db)
def handleQuery(query):
if query.isTriggered:
return agregate(query)
def agregate(query):
results = run(query.string)
l = []
for k, v in results.items():
for l in v:
l.append(
Item(
id=__prettyname__,
text="{0} {1}".format(k, l[0]),
actions=[
UrlAction("Open", l[1])
]
)
)
return l
if __name__ == "__main__":
x = run("linux")
print(x)