job-scrapper/lib/mozilla.py

75 lines
2.7 KiB
Python
Raw Permalink Normal View History

2024-08-23 10:13:15 +00:00
#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
2024-08-23 10:22:07 +00:00
2024-08-23 10:13:15 +00:00
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):
2024-08-23 10:13:15 +00:00
DBFILE = "../db/sqlite3.db"
2024-08-23 10:22:07 +00:00
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"
2024-08-23 10:13:15 +00:00
tries=0
cookie = ''
rows = [0]
while(cookie == '' and tries < 2):
tries+=1
if os.name == 'posix':
shutil.copyfile(cookiePath,tmpPath)
elif os.name == 'nt':
2024-08-23 10:22:07 +00:00
shutil.copyfile(winCookiePath,tmpPath)#workaround for loked database
2024-08-23 10:13:15 +00:00
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:
2024-08-23 10:13:15 +00:00
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