libsecret stuff moved to extractor/common.py, reworked

This commit is contained in:
Aleksei Kovura 2017-11-10 18:17:52 +03:00
parent 5b295effdd
commit 1579fd3889
3 changed files with 26 additions and 18 deletions

View File

@ -11,10 +11,6 @@ import os
import random
import sys
import gi
gi.require_version('Secret', '1')
from gi.repository import Secret
from .compat import compat_urllib_parse
from .options import (
parseOpts,
@ -50,8 +46,6 @@ from .YoutubeDL import YoutubeDL
def _real_main(argv=None):
LIBSECRET_SCHEMA = Secret.Schema.new("io.github.rg3.youtube-dl.Store", Secret.SchemaFlags.DONT_MATCH_NAME, {"user-name": Secret.SchemaAttributeType.STRING, "domain-name": Secret.SchemaAttributeType.STRING})
# Compatibility fixes for Windows
if sys.platform == 'win32':
# https://github.com/rg3/youtube-dl/issues/820
@ -133,6 +127,10 @@ def _real_main(argv=None):
# Conflicting, missing and erroneous options
if opts.usenetrc and (opts.username is not None or opts.password is not None):
parser.error('using .netrc conflicts with giving username/password')
if opts.usekeyring and (opts.username is not None or opts.password is not None):
parser.error('using keyring conflicts with giving username/password')
if opts.usekeyring and opts.usenetrc:
parser.error('using keyring conflicts with using .netrc')
if opts.password is not None and opts.username is None:
parser.error('account username missing\n')
if opts.ap_password is not None and opts.ap_username is None:
@ -147,16 +145,7 @@ def _real_main(argv=None):
parser.error('auto number start must be positive or 0')
if opts.usetitle and opts.useid:
parser.error('using title conflicts with using video ID')
if opts.username is not None and opts.password is None and opts.keyring is True:
# extract domain names, check if all videos are from the same domain.
all_domains = set(map(lambda url: compat_urllib_parse.urlparse(url).netloc, all_urls))
if len(all_domains) > 1:
parser.error('You passed URLs from more than one domain - supplying credentials from command line is not supported in this case.')
domain_name = all_domains.pop()
password = Secret.password_lookup_sync(LIBSECRET_SCHEMA, {"user-name": opts.username, "domain-name": domain_name}, None)
if password is None:
parser.error('Password not found in keyring/wallet')
if opts.username is not None and opts.password is None and opts.keyring is False:
if opts.username is not None and opts.password is None:
opts.password = compat_getpass('Type account password and press [Return]: ')
if opts.ap_username is not None and opts.ap_password is None:
opts.ap_password = compat_getpass('Type TV provider account password and press [Return]: ')
@ -324,6 +313,7 @@ def _real_main(argv=None):
ydl_opts = {
'usenetrc': opts.usenetrc,
'usekeyring': opts.usekeyring,
'username': opts.username,
'password': opts.password,
'twofactor': opts.twofactor,

View File

@ -827,6 +827,24 @@ class InfoExtractor(object):
self._downloader.report_warning(
'parsing .netrc: %s' % error_to_compat_str(err))
if self._downloader.params.get('usekeyring', False):
try:
import gi
gi.require_version('Secret', '1')
from gi.repository import Secret
LIBSECRET_SCHEMA = Secret.Schema.new("io.github.rg3.youtube-dl.Store",Secret.SchemaFlags.DONT_MATCH_NAME,{"extractor": Secret.SchemaAttributeType.STRING,"app": Secret.SchemaAttributeType.STRING})
secret = Secret.password_lookup_sync(LIBSECRET_SCHEMA, {"extractor": "json", "app": "youtube-dl"}, None)
if secret is None:
raise netrc.NetrcParseError('Can\'t find credentials in keyring for "' + netrc_machine + '"')
print(str(secret))
secret_dict = json.loads(secret)
print('JSON Parsed: ' + str(secret_json))
print('JSON User: ' + secret_json['u'])
print('JSON Password: ' + secret_json['p'])
except (IOError, netrc.NetrcParseError) as err:
self._downloader.report_warning(
'Libsecret: %s' % error_to_compat_str(err))
return username, password
def _get_login_info(self, username_option='username', password_option='password', netrc_machine=None):
@ -843,7 +861,7 @@ class InfoExtractor(object):
downloader_params = self._downloader.params
# Attempt to use provided username and password or .netrc data
# Attempt to use provided username and password or .netrc data or libsecret data
if downloader_params.get(username_option) is not None:
username = downloader_params[username_option]
password = downloader_params[password_option]

View File

@ -368,7 +368,7 @@ def parseOpts(overrideArguments=None):
help='Video password (vimeo, smotri, youku)')
authentication.add_option(
'--keyring',
action='store_true', dest='keyring', default=False,
action='store_true', dest='usekeyring', default=False,
help='Retrieve password from keyring. Experimental.')
adobe_pass = optparse.OptionGroup(parser, 'Adobe Pass Options')