-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
70 lines (60 loc) · 2.18 KB
/
utils.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
import os
import json
BROWSERS = {'chrome':'chrome','firefox':'firefox','edge':'msedge','explorer':'iexplore','opera':'opera','brave':'brave-browser'}
ENGINE = {'google':'https://www.google.com/search?q=','yahoo':'https://sg.search.yahoo.com/search?p=','bing':'https://www.bing.com/search?q=','duckduckgo':'https://duckduckgo.com/?q='}
def create_folder(path):
try:
os.mkdir(path)
return True
except FileExistsError:
return False
def verify_config(config):
if config['browser'].lower() in BROWSERS and config['engine'].lower().replace(' ','') in ENGINE:
return True
else:
return False
def set_config(data):
root = os.path.expanduser('~') + '/.config/search'
folder = create_folder(root)
if folder != None and verify_config(data):
with open(root + '/config.json','w') as f:
json.dump(data, f)
else:
return False
def read_config():
root = os.path.expanduser('~') + '/.config/search'
folder = create_folder(root)
if folder != None:
if not os.path.isfile(root + '/config.json'):
with open(root + '/config.json', 'w') as f:
json.dump({'browser':'firefox','engine':'google'},f)
with open(root + '/config.json', 'r') as f:
data = json.load(f)
return data
def verify_tld(url):
split_url = url.split('.')
tld_extracted = '.'.join(split_url[:-1])
if tld_extracted != '' and len(split_url) > 1 and not ' ' in url:
return True
else:
return False
def verify_search(text):
search = ''
config = read_config()
if hasattr(text,"__len__") and len(text) > 1:
for char in text:
search += char+'+'
return ENGINE[config['engine']]+search
else:
if 'www.' in text[0] or verify_tld(text[0]):
if 'http://' in text[0]:
return text[0].replace('http://','')
else:
return text[0]
return ENGINE[config['engine']]+text[0]
def exec_search(search):
if search != None:
config = read_config()
return os.system(f"start {BROWSERS[config['browser']]} {search}")
else:
return 'the search cannot be empty'