Skip to content

Commit

Permalink
Read default Firefox profile name from profiles.ini an use it if dire…
Browse files Browse the repository at this point in the history
…ctory exists.

rewrite get_firefox_profile_name(path) to read profiles.ini from default mozilla profile directory
and return default profile PATH

reformat FF_BM_DB_PATH to accept new path from "profile"
  • Loading branch information
shv-q3 committed Sep 20, 2017
1 parent 21304b3 commit 1df2383
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions buku.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,22 +2174,22 @@ def auto_import_from_browser(self):
DEFAULT_FF_FOLDER = os.path.expanduser('~/.mozilla/firefox')
profile = get_firefox_profile_name(DEFAULT_FF_FOLDER)
if profile:
FF_BM_DB_PATH = '~/.mozilla/firefox/{}.default/places.sqlite'.format(profile)
FF_BM_DB_PATH = '~/.mozilla/firefox/{}/places.sqlite'.format(profile)
elif sys.platform == 'darwin':
GC_BM_DB_PATH = '~/Library/Application Support/Google/Chrome/Default/Bookmarks'

DEFAULT_FF_FOLDER = os.path.expanduser('~/Library/Application Support/Firefox')
profile = get_firefox_profile_name(DEFAULT_FF_FOLDER)
if profile:
FF_BM_DB_PATH = '~/Library/Application Support/Firefox/{}.default/places.sqlite'.format(profile)
FF_BM_DB_PATH = '~/Library/Application Support/Firefox/{}/places.sqlite'.format(profile)
elif sys.platform == 'win32':
username = os.getlogin()
GC_BM_DB_PATH = 'C:/Users/{}/AppData/Local/Google/Chrome/User Data/Default/Bookmarks'.format(username)

DEFAULT_FF_FOLDER = 'C:/Users/{}/AppData/Roaming/Mozilla/Firefox/Profiles'.format(username)
profile = get_firefox_profile_name(DEFAULT_FF_FOLDER)
if profile:
FF_BM_DB_PATH = os.path.join(DEFAULT_FF_FOLDER, '{}.default/places.sqlite'.format(profile))
FF_BM_DB_PATH = os.path.join(DEFAULT_FF_FOLDER, '{}/places.sqlite'.format(profile))
else:
logerr('Buku does not support {} yet'.format(sys.platform))
self.close_quit(1)
Expand Down Expand Up @@ -2558,19 +2558,40 @@ def print_help(self, file=sys.stdout):
# ----------------

def get_firefox_profile_name(path):
"""List folder and detect default Firefox profile name.
"""Read profiles.ini and return path of default profile.
Returns
-------
profile : str
Firefox profile name.
"""
iniprof = path + '/profiles.ini'

try:
names = os.listdir(path)
profile = [name[:-8] for name in names if name.endswith('.default')][0]
except FileNotFoundError:
profile = None
if not os.path.isfile(iniprof):
print('profiles.ini does not exsist')
else:
lines = [line.strip() for line in open(iniprof)]
lres = list(filter(None, lines))
nlines = []
for line in lres:
if 'Profile' in line:
nlines.append(line)
else:
nlines.append(line)
total="\n".join(nlines)
temp = re.split("\[Profile.*?\]", total)
dprof=temp[1::1]
for ans in dprof:
if not 'Default=1'in ans:
dprof.remove(ans)
if not len(dprof)==1:
print('error')
else:
for x in dprof:
ff = re.split("\n",x )
for x in ff:
if 'Path' in x:
profile = (x[5:])

return profile

Expand Down

0 comments on commit 1df2383

Please sign in to comment.