Skip to content

Instantly share code, notes, and snippets.

@tomdwaggy
Forked from mirogula/session2html.py
Last active January 20, 2017 18:55
Show Gist options
  • Save tomdwaggy/291638af6894ea5da8d626fe3d89cc17 to your computer and use it in GitHub Desktop.
Save tomdwaggy/291638af6894ea5da8d626fe3d89cc17 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)
def _str_to_bool(s):
"""Convert string to bool (in argparse context)."""
if s.lower() not in ['true', 'false']:
raise ValueError('Need bool; got %r' % s)
return {'true': True, 'false': False}[s.lower()]
def add_boolean_argument(parser, name, default=False):
"""Add a boolean argument to an ArgumentParser instance."""
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--' + name, nargs='?', default=default, const=True, type=_str_to_bool)
group.add_argument('--no' + name, dest=name, action='store_false')
# 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")
add_boolean_argument(parser, 'images', default=False)
args = parser.parse_args()
show_images = args.images
# 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))
window_counter = 1
for window in ss["windows"]:
args.outputfile.write('<h2>Window #{0}</h2>'.format(window_counter))
args.outputfile.write('<ol>')
tab_counter = 1
for tab in window["tabs"]:
entries = tab.get("entries", {})
if len(entries) < 1:
continue
entry = entries[-1]
url = entry["url"].encode("utf-8")
title = html_escape(entry.get("title", url)).encode("utf-8")
args.outputfile.write('<li>')
if show_images:
image = html_escape(tab.get("attributes", {}).get("image", "")).encode("utf-8")
if image is "":
image = "about:blank"
args.outputfile.write('<a href="{0}"><img src="{1}" style="width:16px;height:16px" /></a>'.format(url, image))
args.outputfile.write('&nbsp;<a href="{0}">{1}</a> : <tt>{2}</tt>'.format(url, title, url))
args.outputfile.write('</li>\n')
print "tabs processed: {0}".format(tab_counter)
tab_counter += 1
args.outputfile.write('</ol>')
window_counter += 1
print "windows processed: {0}".format(window_counter)
args.outputfile.write("""
</ul>
<p></p>
</body>
</html>
"""
)
args.outputfile.close()
#!/bin/bash
if [ -z "${SESSION_LOCATION}" ]; then
SESSION_LOCATION="${HOME}/.mozilla/seamonkey"
fi
if [ -z "${SESSION_SAVE_PATH}" ]; then
SESSION_SAVE_PATH="${HOME}/sessions"
fi
for i in ${SESSION_LOCATION}/*; do
if [ -f "${i}/sessionstore.json" ]; then
echo "Converting session ${i}..."
./session2html.py -s "${i}/sessionstore.json" -o "${SESSION_SAVE_PATH}/`basename ${i}.html`"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment