aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRicardo Garcia <sarbalap+freshmeat@gmail.com>2009-09-13 10:45:04 +0200
committerRicardo Garcia <sarbalap+freshmeat@gmail.com>2010-10-31 11:24:52 +0100
commiteae2666cb43b254b88cf8cc71b654fc6312643da (patch)
tree5abd74ece4d7e6e32bd599fc730d78c04cc05dc4
parent2a04438c7c6de2dc9ef2a0bbaaa8ebc1dc654f44 (diff)
Handle weird OSX locale settings gracefully (fixes issue #43)
-rwxr-xr-xyoutube-dl26
1 files changed, 21 insertions, 5 deletions
diff --git a/youtube-dl b/youtube-dl
index becf1d25d..472a8e1cc 100755
--- a/youtube-dl
+++ b/youtube-dl
@@ -27,6 +27,22 @@ std_headers = {
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):
"""Download Error exception.
@@ -224,12 +240,12 @@ class FileDownloader(object):
def to_stdout(self, message, skip_eol=False):
"""Print message to stdout if not in quiet mode."""
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()
def to_stderr(self, message):
"""Print message to stderr."""
- print >>sys.stderr, message.encode(locale.getpreferredencoding())
+ print >>sys.stderr, message.encode(preferredencoding())
def fixed_template(self):
"""Checks if the output template is fixed."""
@@ -297,9 +313,9 @@ class FileDownloader(object):
# Forced printings
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):
- print info_dict['url'].encode(locale.getpreferredencoding())
+ print info_dict['url'].encode(preferredencoding())
return
@@ -1191,7 +1207,7 @@ if __name__ == '__main__':
'forcetitle': opts.gettitle,
'simulate': (opts.simulate or opts.geturl or opts.gettitle),
'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.useliteral and u'%(title)s-%(id)s.%(ext)s')
or u'%(id)s.%(ext)s'),