Skip to content

Instantly share code, notes, and snippets.

@Hubcapp
Forked from mirogula/session2html.py
Created June 29, 2018 20:43
Show Gist options
  • Save Hubcapp/2e24fa701b4caa734a4d4afbbe172b02 to your computer and use it in GitHub Desktop.
Save Hubcapp/2e24fa701b4caa734a4d4afbbe172b02 to your computer and use it in GitHub Desktop.
Convert Firefox sessionstore.js file to html.
#!/usr/bin/python
################################################################################
# usage: session2html.py [-h] -s SESSION_FILE [-o OUTPUT_FILE]
#
# Convert Firefox sessionstore.js file to html.
#
# optional arguments:
# -h, --help show this help message and exit
# -s SESSION_FILE session file to be converted
# -o OUTPUT_FILE write html output to OUTPUT_FILE; if not defined write html
# output to stdout
################################################################################
import os, sys, json, time, argparse
from xml.sax.saxutils import escape
html_escape_table = {
'"': """,
"'": "'"
}
# escapes this characters: '&', '<', '>', '"', and '''
def html_escape(text):
return escape(text, html_escape_table)
# parse script arguments
desc="Convert Firefox sessionstore.js file to html."
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("-s", dest="sessionfile", type=argparse.FileType('r'),
metavar="SESSION_FILE", required=True, help="session file to be converted")
parser.add_argument("-o", dest="outputfile", type=argparse.FileType('w'),
default=sys.stdout, metavar="OUTPUT_FILE", help="write html output \
to OUTPUT_FILE; if not defined write html output to stdout")
args = parser.parse_args()
# read session file and parse it to json structure
ss = json.loads(args.sessionfile.read().decode("utf-8"))
mtimeInfo = "<p>File mtime {0}</p>".format(
time.ctime(os.stat(args.sessionfile.name).st_mtime))
lastUpdateInfo = ""
if "session" in ss:
if "lastUpdate" in ss["session"]:
lastUpdateInfo = "<p>Recorded session.lastUpdate {0}</p>".format(
time.ctime(ss["session"]["lastUpdate"]/1000.0))
args.outputfile.write("""
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Open Tabs from session file</title>
</head>
<body>
<h1>Open Tabs from Session File</h1>
{0}
{1}
<ul style="list-style-type:none">
""".format(mtimeInfo, lastUpdateInfo))
windows = []
#entries need associated window and group. i'll give it a favicon too, why not...
groupedTabs = []
windowCounter = 0
windowGroups = []
for window in ss["windows"]:
windowGroups.append([])
groupedTabs.append([])
for tab in window["tabs"]:
tabgroup = "none"
if 'extData' in tab.keys():
if 'tabview-tab' in tab['extData'].keys():
if tab['extData']['tabview-tab'] != "null":
sss = json.loads(tab['extData']['tabview-tab'].decode("utf-8"))
tabgroup = str(sss['groupID'])
else:
tabgroup = "null"
if tabgroup not in windowGroups[windowCounter]:
windowGroups[windowCounter].append(tabgroup)
groupedTabs[windowCounter].append({'favicon': tab['image'], 'entries': tab["entries"], "group" : tabgroup})
windowCounter += 1
#have now structured data so that we can use tabgroups in the html
counter = 1
for windowNum in range(len(windowGroups)):
args.outputfile.write('<h2>Window #{0}</h2>'.format(windowNum+1))
for group in windowGroups[windowNum]:
args.outputfile.write('<h3>Group #{0}</h3>'.format(group))
for tab in groupedTabs[windowNum]:
if tab['group'] == group:
entries = tab["entries"]
args.outputfile.write('<li>#{0}<ul style="list-style-type:none">'.format(
counter))
for entry in enumerate(entries):
url = entry[1]["url"].encode("utf-8")
title = html_escape(entry[1].get("title", url)).encode("utf-8")
img = '<img width=32 height=32 src="{0}"/> '.format(tab['favicon'])
if entry[0] != len(entries)-1:
line = '<li>{0}<a href="{1}">{2}</a> : <tt>{3}</tt></li>\n'.format(
img, url, title, url)
else:
line = '<li>{0}<b><a href="{1}">{2}</a> : <tt>{3}</tt></b></li>\n'.format(
img, url, title, url)
args.outputfile.write(line)
args.outputfile.write("</ul>")
print "tabs processed: {0}".format(counter)
counter += 1
counter = 1
args.outputfile.write("""
</ul>
<p></p>
</body>
</html>
"""
)
args.outputfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment