-# Verify both or none present
-if ((cmdl_opts.username is None and not cmdl_opts.password is None) or
- (not cmdl_opts.username is None and cmdl_opts.password is None)):
- sys.exit('Error: both username and password must be given, or none.')
+if cmdl_opts.use_netrc and cmdl_opts.password is not None:
+ sys.exit('Error: using netrc conflicts with giving command line password.')
+
+# Incorrect option formatting
+if cmdl_opts.username is None and cmdl_opts.password is not None:
+ sys.exit('Error: password give but username is missing.')
+
+# Get account information if any
+account_username = None
+account_password = None
+
+if cmdl_opts.use_netrc:
+ try:
+ info = netrc.netrc().authenticators('youtube')
+ if info is None:
+ sys.exit('Error: no authenticators for machine youtube.')
+ netrc_username = info[0]
+ netrc_password = info[2]
+ except IOError:
+ sys.exit('Error: unable to read .netrc file.')
+ except netrc.NetrcParseError:
+ sys.exit('Error: unable to parse .netrc file.')
+
+if cmdl_opts.password is not None:
+ account_username = cmdl_opts.username
+ account_password = cmdl_opts.password
+else:
+ if cmdl_opts.username is not None and cmdl_opts.use_netrc:
+ if cmdl_opts.username != netrc_username:
+ sys.exit('Error: conflicting username from .netrc and command line options.')
+ account_username = cmdl_opts.username
+ account_password = netrc_password
+ elif cmdl_opts.username is not None:
+ account_username = cmdl_opts.username
+ account_password = getpass.getpass('Type YouTube password and press return: ')
+ elif cmdl_opts.use_netrc:
+ if len(netrc_username) == 0:
+ sys.exit('Error: empty username in .netrc file.')
+ account_username = netrc_username
+ account_password = netrc_password