#access cookies from firefox: #copy (because locked): cp .mozilla/firefox/imibizoh.default/cookies.sqlite cookies.sqlite #Select value from moz_cookies where host like '%indeed%' import webbrowser import tempfile import os import sqlite3 import shutil from time import sleep import manipulateString as ms DEBUG = True def log(*s): if DEBUG: print(s) def findDefaultProfile(path): target = '' dirlist = os.listdir(path) for directory in dirlist: posDot = ms.skipAfterChar(directory,'.') stringParse = ms.dropBeforePos(directory,posDot) log(stringParse) if stringParse == "default": target = directory break; if target == '': return -1 else: return target def getCookiesFromBrowser(url,force=False): DBFILE = "../db/sqlite3.db" if os.name == 'posix': homePath = os.path.expanduser('~') cookiePath = homePath + "/.mozilla/firefox/" + findDefaultProfile(homePath + "/.mozilla/firefox/") + "/cookies.sqlite" tmpPath = "/tmp/cookies.sqlite" if os.name == 'nt': appdata = os.getenv('APPDATA') winCookiePath = appdata + "\\Mozilla\\Firefox\\Profiles\\" + findDefaultProfile(appdata + "\\Mozilla\\Firefox\\Profiles\\") + "cookies.sqlite" winFirefoxPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe" tmpPath = tempfile.gettempdir() + "\\cookies.sqlite" tries=0 cookie = '' rows = [0] while(cookie == '' and tries < 2): tries+=1 if os.name == 'posix': shutil.copyfile(cookiePath,tmpPath) elif os.name == 'nt': shutil.copyfile(winCookiePath,tmpPath)#workaround for loked database with sqlite3.connect(tmpPath) as connection: cmd_read_cookies = f"""SELECT name,value FROM moz_cookies WHERE host like ?;""" print(cmd_read_cookies) cursor = connection.cursor() cursor.execute(cmd_read_cookies,(ms.urlToDomain(url),)) while len(rows)!=0: rows = cursor.fetchmany(25) for row in rows: print("row:",row) cookie = cookie + row[0] + '=' + row[1] cookie += ";" print("Cookies:",cookie) if cookie == '' and force == False: if os.name == 'posix': webbrowser.register("firefox",None,webbrowser.BackgroundBrowser("firefox")) webbrowser.get('firefox').open(url) elif os.name == 'nt': webbrowser.register("firefox",None,webbrowser.BackgroundBrowser(winFirefoxPath)) webbrowser.get('firefox').open(url) sleep(1) return cookie