-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabs.py
52 lines (40 loc) · 1.4 KB
/
tabs.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
from common import *
import lz4.block
import json
import os.path
def load_tabs(input_dir):
path = os.path.join(input_dir, 'sessionstore-backups', 'recovery.jsonlz4')
comp_data = open(path, 'rb').read()
assert comp_data[:8] == b'mozLz40\0'
data = lz4.block.decompress(comp_data[8:])
tabs = [ ]
for win in json.loads(data)['windows']:
tabs += [ Tab(tab) for tab in win['tabs'] ]
return tabs
def save_tabs(input_dir = '.', output_dir = '.', tabs = None):
if tabs is None: tabs = load_tabs(input_dir)
f = open(os.path.join(output_dir, 'tabs.csv'), 'w', encoding = 'utf-8')
f.write(Tab.title + '\n')
for tab in sort_tabs(tabs):
f.write(str(tab) + '\n')
f.close()
def sort_tabs(tabs):
dict_ = { }
for tab in tabs:
if tab.url not in dict_ or tab.access_time > dict_[tab.url].access_time:
dict_[tab.url] = tab
return sorted(dict_.values(), key = lambda tab: tab.access_time)
class Tab:
def __init__(self, data):
self.title = data['entries'][-1]['title']
self.url = data['entries'][-1]['url']
self.access_time = data['lastAccessed']
title = ','.join([ 'title', 'url', 'access time' ])
def __str__(self):
return '{},{},{}'.format(
escape(self.title),
escape(self.url),
format_timestamp(self.access_time)
)
if __name__ == '__main__':
save_tabs()