Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tabs Outliner Deduplication
import json
import os
export_file = 'tree-exported-Sat-Apr-09-2016.tree'
backup_export_file = '{}.bak'.format(export_file)
new_tree = []
seen_urls = set()
os.rename(export_file, backup_export_file)
with open(backup_export_file) as f:
tree = json.load(f)
for node in reversed(tree):
if isinstance(node, list):
item = node[1]
if ('type' not in item) or (item['type'] == 'tab'):
url = item['data']['url']
if url in seen_urls:
print 'skipping duplicate: {}'.format(url)
continue
seen_urls.add(url)
new_tree.append(node)
new_tree = list(reversed(new_tree))
window_count = -1
tab_count = -1
for node in new_tree:
if isinstance(node, list):
item = node[1]
pos = []
if item.get('type') in ('win', 'savedwin'):
window_count += 1
tab_count = -1
pos = [window_count]
else:
tab_count += 1
pos = [window_count, tab_count]
node[2] = pos
with open(export_file, 'w') as f:
json.dump(new_tree, f)
@jalaziz

This comment has been minimized.

Copy link
Owner Author

jalaziz commented Apr 10, 2016

Some TODOs:

  • Remove empty windows
  • Preserve tab trees
  • Detect duplicate pages with different anchor tags or query parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.