Created
January 10, 2019 16:49
-
-
Save masakuni-ito/60da15d44d6f8a808d7065c211361d89 to your computer and use it in GitHub Desktop.
Google Cromeのブックマークをwebloc形式のファイルに出力する
This file contains 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
#!/usr/bin/env python3 | |
import os | |
import shutil | |
import datetime | |
import json | |
CHROME_BOOKMARKS = '/Users/ito_masakuni/Library/Application Support/Google/Chrome/Default/Bookmarks' | |
TARGET_DIR = '/Users/ito_masakuni/Desktop/misc/etc/chrome/bookmarks' | |
def main(): | |
xml = [] | |
bookmarks = [] | |
if os.path.exists(TARGET_DIR): | |
bk_dir = os.path.join('/tmp', str(datetime.datetime.now().timestamp()) + '_bookmarks/') | |
shutil.move(os.path.join(TARGET_DIR, ''), bk_dir) | |
os.makedirs(TARGET_DIR, exist_ok=True) | |
with open(CHROME_BOOKMARKS) as fp: | |
xml = json.load(fp) | |
if 'roots' in xml and 'bookmark_bar' in xml['roots']: | |
bookmark_bar = xml['roots']['bookmark_bar'] | |
if 'children' in bookmark_bar: | |
for bookmark_dir in bookmark_bar['children']: | |
if 'children' in bookmark_dir: | |
for bookmark in bookmark_dir['children']: | |
bookmarks.append(bookmark) | |
if 'roots' in xml and 'other' in xml['roots']: | |
other = xml['roots']['other'] | |
if 'children' in other: | |
for bookmark_dir in other['children']: | |
if 'children' in bookmark_dir: | |
for bookmark in bookmark_dir['children']: | |
bookmarks.append(bookmark) | |
for bookmark in bookmarks: | |
with open(os.path.join(TARGET_DIR, bookmark['name']) + '.webloc', mode='w') as fp: | |
body = ''' | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>URL</key> | |
<string>{url}</string> | |
</dict> | |
</plist> | |
'''.format(url=bookmark['url']).strip() | |
fp.write(body) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment