> python sync_alfred.py
Alternatively, you can also import the favicons (low resolution)
> python sync_alfred.py -i
-
-
Save jonasdt/4572934 to your computer and use it in GitHub Desktop.
Steps to transfer Chrome Custom Search Engines to Alfred
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from os import environ, remove, makedirs | |
| from os.path import join, exists | |
| import shutil | |
| import sqlite3 | |
| import plistlib | |
| import urllib2 | |
| import hashlib | |
| PLIST_FILE_PATH = join(environ['HOME'], 'Library/Application Support/Alfred/customsites/customsites.plist') | |
| ICONS_PATH = join(environ['HOME'], 'Library/Application Support/Alfred/customsites/icons/') | |
| WEBDATA_PATH = join(environ['HOME'], 'Library/Application Support/Google/Chrome/Default/Web Data') | |
| def get_custom_searches(webdata_path): | |
| tmp = join(environ['HOME'], '__tmp_web_data_alfred_sync') | |
| shutil.copyfile(webdata_path, tmp) | |
| conn = sqlite3.connect(tmp) | |
| conn.row_factory = sqlite3.Row | |
| c = conn.cursor() | |
| query = 'SELECT short_name as name, keyword, url, favicon_url, input_encodings FROM keywords ORDER BY keyword'; | |
| custom_searches = c.execute(query).fetchall() | |
| conn.close() | |
| remove(tmp) | |
| return custom_searches | |
| def get_icon(favicon_url, data): | |
| if not favicon_url: | |
| favicon_url = 'https://getfavicon.appspot.com/' + data['url'] | |
| icon_hash = hashlib.sha1(data['url']).hexdigest() | |
| opener = urllib2.build_opener() | |
| try: | |
| opener.addheaders = [('User-agent', 'Mozilla/5.0')] | |
| f = opener.open(favicon_url, None, 2) | |
| i = open(join(ICONS_PATH, icon_hash + '.png'),'wb') | |
| i.write(f.read()) | |
| f.close() | |
| i.close() | |
| except urllib2.URLError: | |
| pass | |
| def get_plist_settings(webdata_path, get_icons): | |
| custom_searches = get_custom_searches(webdata_path) | |
| if len(custom_searches) <= 0: | |
| print "No search engines imported." | |
| return | |
| plist_settings = [] | |
| for search in custom_searches: | |
| data = { | |
| 'keyword': search['keyword'], | |
| 'spaces': True, | |
| 'text': search['name'], | |
| 'url': search['url'].replace('{searchTerms}', '{query}'), | |
| 'utf8': search['input_encodings'] == 'UTF-8' | |
| } | |
| plist_settings.append(data) | |
| if get_icons: | |
| get_icon(search['favicon_url'], data) | |
| print "({keyword}) {text} \n\t {url}".format(**data) | |
| return plist_settings | |
| def backup_plist_file(plist_file_path): | |
| shutil.copyfile(plist_file_path, plist_file_path + '.bak') | |
| def write_plist_file(plist_settings, dest): | |
| plistlib.writePlist(plist_settings, dest) | |
| if __name__ == '__main__': | |
| import sys | |
| get_icons = len(sys.argv) > 1 and sys.argv[1] | |
| if get_icons and not exists(ICONS_PATH): | |
| makedirs(ICONS_PATH) | |
| backup_plist_file(PLIST_FILE_PATH) | |
| write_plist_file(get_plist_settings(WEBDATA_PATH, get_icons), PLIST_FILE_PATH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment