X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Futils.py;h=83a274043388ed9acedf175ab6ebb7783e1fbfc6;hb=241bce7aaf53ab57615434684c7bfddbddcabf01;hp=f3ad47422445de82cc34c024359ad743bb9ece97;hpb=d7dda1688886284b17c35be200d980f24c2546d2;p=youtube-dl diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index f3ad47422..83a274043 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -767,6 +767,10 @@ def unified_strdate(date_str): upload_date = datetime.datetime.strptime(date_str, expression).strftime('%Y%m%d') except: pass + if upload_date is None: + timetuple = email.utils.parsedate_tz(date_str) + if timetuple: + upload_date = datetime.datetime(*timetuple[:6]).strftime('%Y%m%d') return upload_date def determine_ext(url, default_ext=u'unknown_video'): @@ -1066,13 +1070,14 @@ def fix_xml_all_ampersand(xml_str): def setproctitle(title): + assert isinstance(title, type(u'')) try: libc = ctypes.cdll.LoadLibrary("libc.so.6") except OSError: return title = title buf = ctypes.create_string_buffer(len(title) + 1) - buf.value = title + buf.value = title.encode('utf-8') try: libc.prctl(15, ctypes.byref(buf), 0, 0, 0) except AttributeError: @@ -1083,3 +1088,33 @@ def remove_start(s, start): if s.startswith(start): return s[len(start):] return s + + +def url_basename(url): + path = compat_urlparse.urlparse(url).path + return path.strip(u'/').split(u'/')[-1] + + +class HEADRequest(compat_urllib_request.Request): + def get_method(self): + return "HEAD" + + +def int_or_none(v): + return v if v is None else int(v) + + +def parse_duration(s): + if s is None: + return None + + m = re.match( + r'(?:(?:(?P[0-9]+):)?(?P[0-9]+):)?(?P[0-9]+)$', s) + if not m: + return None + res = int(m.group('secs')) + if m.group('mins'): + res += int(m.group('mins')) * 60 + if m.group('hours'): + res += int(m.group('hours')) * 60 * 60 + return res