forked from MarkHedleyJones/dmenu-extended-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_internetSearch.py
98 lines (78 loc) · 2.83 KB
/
plugin_internetSearch.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
import dmenu_extended
import sys
file_prefs = dmenu_extended.path_prefs + '/internetSearch.json'
class extension(dmenu_extended.dmenu):
title = 'Szukaj w Internecie: '
is_submenu = True
def __init__(self):
self.load_preferences()
def create_default_providers(self):
default = {
'providers': [
{
'title': 'Google',
'url': 'https://www.google.com/search?q='
},
{
'title': 'DuckDuckGo',
'url': 'https://duckduckgo.com/?q='
},
{
'title': 'Wikipedia PL',
'url': 'https://pl.wikipedia.org/wiki/Special:Search?search='
},
{
'title': 'Google obrazki',
'url': 'https://www.google.com/images?q='
},
{
'title': 'Github',
'url': 'https://github.com/search?q='
}
],
'default': 'Google'
}
self.save_json(file_prefs, default)
def load_providers(self):
providers = self.load_json(file_prefs)
if providers == False:
self.create_default_providers()
providers = self.load_json(file_prefs)
return providers
def conduct_search(self, searchTerm, providerName=False):
default = self.providers['default']
primary = False
fallback = False
for provider in self.providers['providers']:
if provider['title'] == default:
fallback = provider['url'] + searchTerm
elif provider['title'] == providerName:
primary = provider['url'] + searchTerm
if primary:
self.open_url(primary)
else:
self.open_url(fallback)
def run(self, inputText):
self.providers = self.load_providers()
if inputText != '':
self.conduct_search(inputText)
else:
items = []
for provider in self.providers['providers']:
items.append(provider['title'])
item_editPrefs = self.prefs['indicator_edit'] + ' Edytuj strony'
items.append(item_editPrefs)
provider = self.menu(items, prompt='Wybierz stronę:')
if provider == item_editPrefs:
self.open_file(file_prefs)
elif provider == '':
sys.exit()
else:
if provider not in items:
self.conduct_search(provider)
else:
search = self.menu('', prompt='Wyszukaj')
if search == '':
sys.exit()
else:
self.conduct_search(search, provider)