-
Notifications
You must be signed in to change notification settings - Fork 2
/
BrowserBookmarks.py
109 lines (86 loc) · 3.7 KB
/
BrowserBookmarks.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
import logging
import os
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction
from ulauncher.api.shared.action.RenderResultListAction import \
RenderResultListAction
from ulauncher.api.shared.event import ItemEnterEvent, KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
logging.basicConfig()
logger = logging.getLogger(__name__)
support_browsers = ['google-chrome', 'chromium', 'Brave-Browser']
browser_imgs = {
'google-chrome': 'images/chrome.png',
'chromium': 'images/chromium.png',
'Brave-Browser': 'images/brave.png',
}
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
items = extension.get_items(event.get_argument())
return RenderResultListAction(items)
class BrowserBookmarks(Extension):
matches_len = 0
max_matches_len = 10
def __init__(self):
self.bookmarks_paths = self.find_bookmarks_paths()
super(BrowserBookmarks, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
@staticmethod
def find_bookmarks_paths():
res_lst = []
for browser in support_browsers:
f = os.popen('find $HOME/.config/%s | grep Bookmarks' % browser)
res = f.read().split('\n')
if len(res) == 0:
logger.info('Path to the %s Bookmarks was not found' % browser)
continue
for one_path in res:
if one_path.endswith('Bookmarks'):
res_lst.append((one_path, browser))
if len(res_lst) == 0:
logger.exception('Path to the Chrome Bookmarks was not found')
return res_lst
def find_rec(self, bookmark_entry, query, matches):
if self.matches_len >= self.max_matches_len:
return
if bookmark_entry['type'] == 'folder':
for child_bookmark_entry in bookmark_entry['children']:
self.find_rec(child_bookmark_entry, query, matches)
else:
sub_queries = query.split(' ')
bookmark_title = bookmark_entry['name']
if not self.contains_all_substrings(bookmark_title, sub_queries):
return
matches.append(bookmark_entry)
self.matches_len += 1
def get_items(self, query):
items = []
self.matches_len = 0
if query is None:
query = ''
logger.debug('Finding bookmark entries for query %s' % query)
for bookmarks_path, browser in self.bookmarks_paths:
matches = []
with open(bookmarks_path) as data_file:
data = json.load(data_file)
self.find_rec(data['roots']['bookmark_bar'], query, matches)
self.find_rec(data['roots']['synced'], query, matches)
self.find_rec(data['roots']['other'], query, matches)
for bookmark in matches:
bookmark_name = bookmark['name'].encode('utf-8')
bookmark_url = bookmark['url'].encode('utf-8')
item = ExtensionResultItem(
icon=browser_imgs.get(browser),
name='%s' % bookmark_name.decode('utf-8'),
description='%s' % bookmark_url.decode('utf-8'),
on_enter=OpenUrlAction(bookmark_url.decode('utf-8'))
)
items.append(item)
return items
def contains_all_substrings(self, text, substrings):
for substring in substrings:
if substring.lower() not in text.lower():
return False
return True