-
Notifications
You must be signed in to change notification settings - Fork 0
/
preferences.py
73 lines (54 loc) · 2.34 KB
/
preferences.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
import json
import os
def get_preferences_path():
if os.name == 'nt':
return ['AppData', 'Local', 'Google', 'Chrome', 'User Data', 'Default', 'preferences']
else:
return ['Library', 'Application Support', 'Google', 'Chrome', 'Default', 'Preferences']
class ChromePreferences:
def __init__(self):
home = os.path.expanduser("~")
preferences_path = os.path.join(home, *get_preferences_path())
with open(preferences_path) as fin:
self.preferences = json.load(fin)
print("Loaded Chrome preferences from", preferences_path)
def get_language_profile(self):
return [(l['language'], l['probability']) for l in self.get('language_profile').get('reading').get('preference')]
def get_google_url(self):
return self.get('browser').get('last_known_google_url')
def get_top_sites(self):
sites = self.get('profile').get('content_settings').get('exceptions').get('site_engagement')
return sorted([
(url.split(',')[0], data['setting']['rawScore'])
for url, data in sites.items()
], key=lambda pair: -pair[1])
def get_account_info(self):
return self.get('account_info')[-1]
def get_email(self):
return self.get_account_info()['email']
def get_custom_handlers(self):
return {
handler['protocol']: handler['url']
for handler in self.get('custom_handlers').get('registered_protocol_handlers')
}
def get_account_infos(self):
return self.get('account_info')
def get_chrome_version(self):
return self.get('extensions').get('last_chrome_version')
def get_download_folder(self):
return self.get('savefile').get('default_directory')
def get(self, key):
return self.preferences[key]
def __str__(self):
return json.dumps(self.preferences, indent=4)
if __name__ == '__main__':
p = ChromePreferences()
print('language_profile:', p.get_language_profile())
print('custom_handlers:', p.get_custom_handlers())
print('top_sites[:10]:')
for n, (url, score) in enumerate(p.get_top_sites()[:10]):
print(' ', n+1, url, score)
print('full_name:', p.get_account_info().get('full_name'))
print('account_info:',json.dumps(p.get_account_info(), indent=4))
import hosts
hosts.setup_as_administrator()