-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
93 lines (86 loc) · 3.54 KB
/
main.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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, SystemExitEvent,PreferencesUpdateEvent, PreferencesEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction
from history import FirefoxHistory
class FirefoxHistoryExtension(Extension):
def __init__(self):
super(FirefoxHistoryExtension, self).__init__()
# Firefox History Getter
self.fh = FirefoxHistory()
# Ulauncher Events
self.subscribe(KeywordQueryEvent,KeywordQueryEventListener())
self.subscribe(SystemExitEvent,SystemExitEventListener())
self.subscribe(PreferencesEvent,PreferencesEventListener())
self.subscribe(PreferencesUpdateEvent,PreferencesUpdateEventListener())
class PreferencesEventListener(EventListener):
def on_event(self,event,extension):
# Aggregate Results
#extension.fh.aggregate = event.preferences['aggregate']
# Results Order
#extension.fh.order = event.preferences['order']
# Results Number
try:
n = int(event.preferences['limit'])
except:
n = 10
extension.fh.limit = n
class PreferencesUpdateEventListener(EventListener):
def on_event(self,event,extension):
# Results Order
#if event.id == 'order':
# extension.fh.order = event.new_value
# Results Number
if event.id == 'limit':
try:
n = int(event.new_value)
extension.fh.limit = n
except:
pass
#elif event.id == 'aggregate':
# extension.fh.aggregate = event.new_value
class SystemExitEventListener(EventListener):
def on_event(self,event,extension):
extension.fh.close()
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
query = event.get_argument()
# Blank Query
if query == None:
query = ''
items = []
# Search into Firefox History
results = extension.fh.search(query)
for link in results:
# Encode
hostname = link[0]
# Split Domain Levels
dm = hostname.split('.')
# Remove WWW
if dm[0]=='www':
i = 1
else:
i = 0
# Join remaining domains and capitalize
name = ' '.join(dm[i:len(dm)-1]).title()
# TODO: favicon of the website
#if extension.fh.aggregate == "true":
# items.append(ExtensionResultItem(icon='images/icon.png',
# name=name,
# on_enter=OpenUrlAction('https://'+hostname)))
#else:
title = link[0]
url = link[1]
#if link[1] == None:
# title = hostname
#else:
# title = link[1]
items.append(ExtensionResultItem(icon='images/icon.png',
name=title,
description=url,
on_enter=OpenUrlAction(url)))
return RenderResultListAction(items)
if __name__ == '__main__':
FirefoxHistoryExtension().run()