Browse Source

Handle weird OSX locale settings gracefully (fixes issue #43)

master
Ricardo Garcia 15 years ago
parent
commit
eae2666cb4
1 changed files with 21 additions and 5 deletions
  1. 26
      youtube-dl

26
youtube-dl

@ -27,6 +27,22 @@ std_headers = {
simple_title_chars = string.ascii_letters.decode('ascii') + string.digits.decode('ascii') simple_title_chars = string.ascii_letters.decode('ascii') + string.digits.decode('ascii')
def preferredencoding():
"""Get preferred encoding.
Returns the best encoding scheme for the system, based on
locale.getpreferredencoding() and some further tweaks.
"""
try:
pref = locale.getpreferredencoding()
# Mac OSX systems have this problem sometimes
if pref == '':
return 'UTF-8'
return pref
except:
sys.stderr.write('WARNING: problem obtaining preferred encoding. Falling back to UTF-8.\n')
return 'UTF-8'
class DownloadError(Exception): class DownloadError(Exception):
"""Download Error exception. """Download Error exception.
@ -224,12 +240,12 @@ class FileDownloader(object):
def to_stdout(self, message, skip_eol=False): def to_stdout(self, message, skip_eol=False):
"""Print message to stdout if not in quiet mode.""" """Print message to stdout if not in quiet mode."""
if not self.params.get('quiet', False): if not self.params.get('quiet', False):
print (u'%s%s' % (message, [u'\n', u''][skip_eol])).encode(locale.getpreferredencoding()),
print (u'%s%s' % (message, [u'\n', u''][skip_eol])).encode(preferredencoding()),
sys.stdout.flush() sys.stdout.flush()
def to_stderr(self, message): def to_stderr(self, message):
"""Print message to stderr.""" """Print message to stderr."""
print >>sys.stderr, message.encode(locale.getpreferredencoding())
print >>sys.stderr, message.encode(preferredencoding())
def fixed_template(self): def fixed_template(self):
"""Checks if the output template is fixed.""" """Checks if the output template is fixed."""
@ -297,9 +313,9 @@ class FileDownloader(object):
# Forced printings # Forced printings
if self.params.get('forcetitle', False): if self.params.get('forcetitle', False):
print info_dict['title'].encode(locale.getpreferredencoding())
print info_dict['title'].encode(preferredencoding())
if self.params.get('forceurl', False): if self.params.get('forceurl', False):
print info_dict['url'].encode(locale.getpreferredencoding())
print info_dict['url'].encode(preferredencoding())
return return
@ -1191,7 +1207,7 @@ if __name__ == '__main__':
'forcetitle': opts.gettitle, 'forcetitle': opts.gettitle,
'simulate': (opts.simulate or opts.geturl or opts.gettitle), 'simulate': (opts.simulate or opts.geturl or opts.gettitle),
'format': opts.format, 'format': opts.format,
'outtmpl': ((opts.outtmpl is not None and opts.outtmpl.decode(locale.getpreferredencoding()))
'outtmpl': ((opts.outtmpl is not None and opts.outtmpl.decode(preferredencoding()))
or (opts.usetitle and u'%(stitle)s-%(id)s.%(ext)s') or (opts.usetitle and u'%(stitle)s-%(id)s.%(ext)s')
or (opts.useliteral and u'%(title)s-%(id)s.%(ext)s') or (opts.useliteral and u'%(title)s-%(id)s.%(ext)s')
or u'%(id)s.%(ext)s'), or u'%(id)s.%(ext)s'),

Loading…
Cancel
Save