diff options
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r-- | youtube_dl/utils.py | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 7b5878830..bd46a2da2 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import ctypes import datetime import email.utils import errno @@ -15,6 +16,7 @@ import platform import re import ssl import socket +import subprocess import sys import traceback import zlib @@ -547,7 +549,7 @@ def make_HTTPS_handler(opts_no_check_certificate): def connect(self): sock = socket.create_connection((self.host, self.port), self.timeout) - if self._tunnel_host: + if getattr(self, '_tunnel_host', False): self.sock = sock self._tunnel() try: @@ -561,11 +563,14 @@ def make_HTTPS_handler(opts_no_check_certificate): return HTTPSHandlerV3() else: context = ssl.SSLContext(ssl.PROTOCOL_SSLv3) - context.set_default_verify_paths() - context.verify_mode = (ssl.CERT_NONE if opts_no_check_certificate else ssl.CERT_REQUIRED) + context.set_default_verify_paths() + try: + context.load_default_certs() + except AttributeError: + pass # Python < 3.4 return compat_urllib_request.HTTPSHandler(context=context) class ExtractorError(Exception): @@ -1021,6 +1026,54 @@ def format_bytes(bytes): converted = float(bytes) / float(1024 ** exponent) return u'%.2f%s' % (converted, suffix) + def str_to_int(int_str): int_str = re.sub(r'[,\.]', u'', int_str) return int(int_str) + + +def get_term_width(): + columns = os.environ.get('COLUMNS', None) + if columns: + return int(columns) + + try: + sp = subprocess.Popen( + ['stty', 'size'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = sp.communicate() + return int(out.split()[1]) + except: + pass + return None + + +def month_by_name(name): + """ Return the number of a month by (locale-independently) English name """ + + ENGLISH_NAMES = [ + u'January', u'February', u'March', u'April', u'May', u'June', + u'July', u'August', u'September', u'October', u'November', u'December'] + try: + return ENGLISH_NAMES.index(name) + 1 + except ValueError: + return None + + +def fix_xml_all_ampersand(xml_str): + """Replace all the '&' by '&' in XML""" + return xml_str.replace(u'&', u'&') + + +def setproctitle(title): + try: + libc = ctypes.cdll.LoadLibrary("libc.so.6") + except OSError: + return + title = title + buf = ctypes.create_string_buffer(len(title) + 1) + buf.value = title + try: + libc.prctl(15, ctypes.byref(buf), 0, 0, 0) + except AttributeError: + return # Strange libc, just skip this |