Deieses Skript entfernt doppelte Bookmarks im Firefox.
Firefox muß geschlossen sein, Script läuft derzeit nur unter Linux.
Weiß jemand wie ich unter python feststellen kann ob gerade ein Firefox läuft (und ihn ggf beenden kann) ?
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# FirefoxBookmarkSorter
# written 9/2006 by Horst JENS (horst.jens@gmx.at)
# This program is under the GPL License http://www.gnu.org/licenses/gpl.html
# Make sure to close FireFox before running this script. Only run on linux. Tested on Ubuntu Dapper.
import os
import shutil
def urlinside(string):
""" check if a string contain a url """
if 'HREF="http' in string:
return True
else:
return False
if os.name != 'posix': # Linux ?
raise SystemExit, "no-linux Operations System detected. Yuk !"
if os.path.exists(os.path.join(os.environ["HOME"],".mozilla","firefox")):
print "i found the hidden firefox directory"
else:
raise SystemExit, "no ~/.mozilla/firefox directory found. Bye !"
#--- start of program ------
firefoxdir = os.path.join(os.environ["HOME"], '.mozilla', 'firefox')
bookmarkdirlist = []
for rootdir, dirlist, filelist in os.walk(firefoxdir): #--- recursive scan for filenames
for fileitem in filelist: # fileitem is my own word, "file" is a reserved word !!
if fileitem == "bookmarks.html":
bookmarkdirlist.append(os.path.join(firefoxdir, rootdir, "bookmarks.html"))
for bookmarkfile in bookmarkdirlist: # process each firefox profile
print "processing " + bookmarkfile #--- processing each bookmark file
shutil.copyfile(bookmarkfile, bookmarkfile + ".old") # make backup copy
num = 0
f = file(bookmarkfile,'r')
kopf = ""
kopfinarbeit = True
schwanz = ""
schwanzinarbeit = False
myfile = {} # a directory of {url:string} for nearly all of the file where string are the lines of the file
mynums = {} # a directory of {num:string} where string is the url for myfile to sort the lines of the file
while True:
line = f.readline()
num += 1
if len(line)==0:
break # end of file
if "</DL><p>" in line:
if kopfinarbeit:
kopf = kopf + line
kopfinarbeit = False
else:
schwanz = schwanz + line
schwanzinarbeit = True
else:
if kopfinarbeit:
kopf = kopf + line
elif schwanzinarbeit:
schwanz = schwanz + line
else: #--- normal line
if urlinside(line): # a key (the url) is hidden in this line ! now extract it
for junkstring in line.split(" "):
if urlinside(junkstring): # that is my url !
if not myfile.has_key(junkstring):
mynums[num] = junkstring # the url is new, give it a line number
myfile[junkstring] = line # add a new url:textline(s) pair to myfile
break
else: # this line contains no url at all and should be added to the last value
if len(myfile) > 0:
myfile[junkstring] = myfile[junkstring] + line
else: kopf += line #add this boring line to the head
f.close() # close the file (no more reading)
#--- now writing ---
f = file(bookmarkfile,'w')
f.write(kopf)
for x in range(1, num +1):
if mynums.has_key(x):
f.write(myfile[mynums[x]])
f.write(schwanz)
f.close
print "bookmarkfile changed. old bookmarkfile saved as bookmarkfile.old"
print "Ready. You can restart Firefox now."