diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 4338d5ed1..c5fcf2744 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -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, diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index a67ac4411..dde09939c 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -826,6 +826,24 @@ class InfoExtractor(object): except (IOError, netrc.NetrcParseError) as err: 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 @@ -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] diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 18f6ce4e8..901687e38 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -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')