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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#!/usr/bin/env python
"""
Small utility to convert MSIE favourites to an object structure.
Originally written by Fredrik Lundh.
Modified by Lars Marius Garshol
2-17-2002 tbp Now closes folder when its traverse is done. Also works
with current IE shortcut format, which can differ from the format
that was assumed here.
"""
import bookmark,os,string
DIR = "Favoritter" # Norwegian version
#USRDIR = os.environ["USERPROFILE"] # NT version
USRDIR = r"c:\windows" # 95 version
class MSIE:
# internet explorer
def __init__(self,bookmarks, path):
self.bms=bookmarks
self.root = None
self.path = path
self.__walk()
def __walk(self, subpath=[]):
# traverse favourites folder
path = os.path.join(self.path, string.join(subpath, os.sep))
for file in os.listdir(path):
fullname = os.path.join(path, file)
if os.path.isdir(fullname):
self.bms.add_folder(file,None)
self.__walk(subpath + [file])
self.bms.leave_folder()
else:
url = self.__geturl(fullname)
if url:
self.bms.add_bookmark(os.path.splitext(file)[0],None,
None,None,url)
def __geturl(self, file):
try:
fp = open(file)
#if fp.readline() != "[InternetShortcut]\n":
# return None
while 1:
line=fp.readline()
if not line:
return None
if line=="[InternetShortcut]\n":
s = fp.readline()
if not s:
break
if s[:4] == "URL=":
fp.close()
return s[4:-1]
elif line=="[DEFAULT]\n":
s = fp.readline()
if not s:
break
if s[:8] == "BASEURL=":
fp.close()
return s[8:-1]
except IOError:
return ''
fp.close()
return ''
# --- Testprogram
if __name__ == '__main__':
import sys
if len(sys.argv)>1:
path = sys.argv[1]
else:
try:
import win32api, win32con
except ImportError:
print "The win32api module is not available on this system"
print "so we can't automatically find your favorites folder."
print "Please re-run this program specifiying the location of your"
print "favorites folder on the command line."
sys.exit(1)
keyname = r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
hkey = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, keyname)
path, pathtype = win32api.RegQueryValueEx(hkey, "Favorites")
assert pathtype == win32con.REG_SZ
msie=MSIE(bookmark.Bookmarks(), path)
msie.bms.dump_xbel()
|