[common] load_cookies(): copy cookies.sqlite to a temporary file in case database is locked (e.g., Firefox is running)

This commit is contained in:
Mort Yao 2019-03-03 18:28:58 +01:00
parent 2f9263c01f
commit 077b604748
No known key found for this signature in database
GPG Key ID: 07DA00CB78203251

View File

@ -1276,9 +1276,8 @@ def download_main(download, download_playlist, urls, playlist, **kwargs):
def load_cookies(cookiefile): def load_cookies(cookiefile):
from http.cookiejar import Cookie
global cookies global cookies
try: if cookiefile.endswith('.txt'):
# MozillaCookieJar treats prefix '#HttpOnly_' as comments incorrectly! # MozillaCookieJar treats prefix '#HttpOnly_' as comments incorrectly!
# do not use its load() # do not use its load()
# see also: # see also:
@ -1287,6 +1286,7 @@ def load_cookies(cookiefile):
# - https://curl.haxx.se/libcurl/c/CURLOPT_COOKIELIST.html#EXAMPLE # - https://curl.haxx.se/libcurl/c/CURLOPT_COOKIELIST.html#EXAMPLE
#cookies = cookiejar.MozillaCookieJar(cookiefile) #cookies = cookiejar.MozillaCookieJar(cookiefile)
#cookies.load() #cookies.load()
from http.cookiejar import Cookie
cookies = cookiejar.MozillaCookieJar() cookies = cookiejar.MozillaCookieJar()
now = time.time() now = time.time()
ignore_discard, ignore_expires = False, False ignore_discard, ignore_expires = False, False
@ -1338,24 +1338,28 @@ def load_cookies(cookiefile):
continue continue
cookies.set_cookie(c) cookies.set_cookie(c)
except Exception: elif cookiefile.endswith(('.sqlite', '.sqlite3')):
import sqlite3 import sqlite3, shutil, tempfile
temp_dir = tempfile.gettempdir()
temp_cookiefile = os.path.join(temp_dir, 'temp_cookiefile.sqlite')
shutil.copy2(cookiefile, temp_cookiefile)
cookies = cookiejar.MozillaCookieJar() cookies = cookiejar.MozillaCookieJar()
con = sqlite3.connect(cookiefile) con = sqlite3.connect(temp_cookiefile)
cur = con.cursor() cur = con.cursor()
try: cur.execute("""SELECT host, path, isSecure, expiry, name, value
cur.execute("""SELECT host, path, isSecure, expiry, name, value FROM moz_cookies""")
FROM moz_cookies""") for item in cur.fetchall():
for item in cur.fetchall(): c = cookiejar.Cookie(
c = cookiejar.Cookie( 0, item[4], item[5], None, False, item[0],
0, item[4], item[5], None, False, item[0], item[0].startswith('.'), item[0].startswith('.'),
item[0].startswith('.'), item[0].startswith('.'), item[1], False, item[2], item[3], item[3] == '', None,
item[1], False, item[2], item[3], item[3] == '', None, None, {},
None, {}, )
) cookies.set_cookie(c)
cookies.set_cookie(c)
except Exception: else:
pass log.e('[error] unsupported cookies format')
# TODO: Chromium Cookies # TODO: Chromium Cookies
# SELECT host_key, path, secure, expires_utc, name, encrypted_value # SELECT host_key, path, secure, expires_utc, name, encrypted_value
# FROM cookies # FROM cookies