[common] support both cookies.txt and cookies.sqlite

This commit is contained in:
Mort Yao 2015-10-17 03:38:55 +02:00
parent 1496c77978
commit ede432659c

View File

@ -21,7 +21,6 @@ force = False
player = None player = None
extractor_proxy = None extractor_proxy = None
cookies = None cookies = None
cookies_txt = None
fake_headers = { fake_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
@ -152,6 +151,11 @@ def undeflate(data):
# DEPRECATED in favor of get_content() # DEPRECATED in favor of get_content()
def get_response(url, faker = False): def get_response(url, faker = False):
# install cookies
if cookies:
opener = request.build_opener(request.HTTPCookieProcessor(cookies))
request.install_opener(opener)
if faker: if faker:
response = request.urlopen(request.Request(url, headers = fake_headers), None) response = request.urlopen(request.Request(url, headers = fake_headers), None)
else: else:
@ -199,8 +203,8 @@ def get_content(url, headers={}, decoded=True):
""" """
req = request.Request(url, headers=headers) req = request.Request(url, headers=headers)
if cookies_txt: if cookies:
cookies_txt.add_cookie_header(req) cookies.add_cookie_header(req)
req.headers.update(req.unredirected_hdrs) req.headers.update(req.unredirected_hdrs)
response = request.urlopen(req) response = request.urlopen(req)
data = response.read() data = response.read()
@ -894,7 +898,6 @@ def script_main(script_name, download, download_playlist = None):
global player global player
global extractor_proxy global extractor_proxy
global cookies global cookies
global cookies_txt
info_only = False info_only = False
playlist = False playlist = False
@ -925,10 +928,12 @@ def script_main(script_name, download, download_playlist = None):
dry_run = True dry_run = True
info_only = False info_only = False
elif o in ('-c', '--cookies'): elif o in ('-c', '--cookies'):
#cookies_txt = cookiejar.MozillaCookieJar(a) try:
#cookies_txt.load()
import sqlite3
cookies = cookiejar.MozillaCookieJar(a) cookies = cookiejar.MozillaCookieJar(a)
cookies.load()
except:
import sqlite3
cookies = cookiejar.MozillaCookieJar()
con = sqlite3.connect(a) con = sqlite3.connect(a)
cur = con.cursor() cur = con.cursor()
cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies") cur.execute("SELECT host, path, isSecure, expiry, name, value FROM moz_cookies")