allow profile name to be specified on command-line
[public/misc-sysadmin.git] / save_firefox_urls.py
1 #!/usr/bin/python3
2
3 # Copyright 2012, Svenne Krap
4 # svenne@krap.dk
5 #
6 # Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
7 #
8 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9
10 # This script enables you to save all tabs in a (single-window) firefox sessions to a text file
11
12 import json
13 import os
14 import os.path
15 import sys
16 import time
17
18 ts = time.strftime("%Y-%m-%d-%H:%M:%S")
19
20 if len(sys.argv) == 2:
21     profile = sys.argv[1]
22 else:
23     profile = "default"
24 basedir = os.environ['HOME'] + "/.mozilla/firefox/"
25 histdir = os.environ['HOME'] + "/mozhistory";
26 if not os.path.exists(histdir): 
27     os.mkdir(histdir)
28 outfile = histdir + "/" + profile + "-" + ts
29
30 os.chdir(basedir)
31 files = os.listdir(".")
32
33 thefile = None
34
35 for fil in files:
36     if not fil.endswith("." + profile): continue
37     thefile = fil
38
39 if thefile == None:
40     print("cant find profile")
41     sys.exit()
42
43 thefile = basedir + "/" + thefile + "/sessionstore.js"
44 print ("Using " + thefile)
45 data = open(thefile,'r').read()
46 j = json.loads(data)
47 fields = ["title","url"]
48 windows = len(j['windows'])
49 outfp = open(outfile,"w")
50
51 for w in range(windows):
52     print("Looking at window " + str(w))
53     tabs = len(j['windows'][w]["tabs"])
54     print ("  Found " + str(tabs) + " tabs")
55     for t in range(tabs):
56         print("  Looking at tab " + str(w))
57         entries = len(j['windows'][w]["tabs"][t]["entries"]) 
58         print("    History depth: " + str(entries))
59
60         for e in range(entries):
61             try:
62                 title = j['windows'][w]["tabs"][t]["entries"][e]["title"]
63             except:
64                 title = "<unknown>";
65             try:
66                 url = j['windows'][w]["tabs"][t]["entries"][e]["url"]
67             except:
68                 continue
69             print ("    Found:")
70             print ("      Entry: " + str(e))
71             print ("      Title: " + title)
72             print ("      Url  : " + url)
73             outfp.write( str(w) + " " + str(t) + " " + str(e) + " " + title + " " + url + "\n")
74
75 outfp.close()
76 print ("Saved as " + outfile)