Merge pull request #802 from joeframbach/master
[youtube-dl] / youtube_dl / utils.py
index 3a2f0022fb87f9c6426f295cbc5da214e6370b72..f2342b10a0e2521be52c8ffed0c279bceaa15e5c 100644 (file)
@@ -430,6 +430,28 @@ def decodeOption(optval):
     assert isinstance(optval, compat_str)
     return optval
 
+def formatSeconds(secs):
+    if secs > 3600:
+        return '%d:%02d:%02d' % (secs // 3600, (secs % 3600) // 60, secs % 60)
+    elif secs > 60:
+        return '%d:%02d' % (secs // 60, secs % 60)
+    else:
+        return '%d' % secs
+
+def make_HTTPS_handler(opts):
+    if sys.version_info < (3,2):
+        # Python's 2.x handler is very simplistic
+        return compat_urllib_request.HTTPSHandler()
+    else:
+        import ssl
+        context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
+        context.set_default_verify_paths()
+        
+        context.verify_mode = (ssl.CERT_NONE
+                               if opts.no_check_certificate
+                               else ssl.CERT_REQUIRED)
+        return compat_urllib_request.HTTPSHandler(context=context)
+
 class ExtractorError(Exception):
     """Error during info extraction."""
     def __init__(self, msg, tb=None):
@@ -586,7 +608,29 @@ def unified_strdate(date_str):
     return upload_date
 
 def date_from_str(date_str):
-    """Return a datetime object from a string in the format YYYYMMDD"""
+    """
+    Return a datetime object from a string in the format YYYYMMDD or
+    (now|today)[+-][0-9](day|week|month|year)(s)?"""
+    today = datetime.date.today()
+    if date_str == 'now'or date_str == 'today':
+        return today
+    match = re.match('(now|today)(?P<sign>[+-])(?P<time>\d+)(?P<unit>day|week|month|year)(s)?', date_str)
+    if match is not None:
+        sign = match.group('sign')
+        time = int(match.group('time'))
+        if sign == '-':
+            time = -time
+        unit = match.group('unit')
+        #A bad aproximation?
+        if unit == 'month':
+            unit = 'day'
+            time *= 30
+        elif unit == 'year':
+            unit = 'day'
+            time *= 365
+        unit += 's'
+        delta = datetime.timedelta(**{unit: time})
+        return today + delta
     return datetime.datetime.strptime(date_str, "%Y%m%d").date()
     
 class DateRange(object):
@@ -601,7 +645,7 @@ class DateRange(object):
             self.end = date_from_str(end)
         else:
             self.end = datetime.datetime.max.date()
-        if self.start >= self.end:
+        if self.start > self.end:
             raise ValueError('Date range: "%s" , the start date must be before the end date' % self)
     @classmethod
     def day(cls, day):
@@ -609,7 +653,8 @@ class DateRange(object):
         return cls(day,day)
     def __contains__(self, date):
         """Check if the date is in the range"""
-        date = date_from_str(date)
-        return self.start <= date and date <= self.end
+        if not isinstance(date, datetime.date):
+            date = date_from_str(date)
+        return self.start <= date <= self.end
     def __str__(self):
         return '%s - %s' % ( self.start.isoformat(), self.end.isoformat())