Skip to content
This repository
Browse code

Add --age option in CLI

  • Loading branch information...
commit 680c699dfa66c2cbf99b8a1e258a4d4378b3948e 1 parent b7ba6d5
Antoine Bertin authored April 11, 2012

Showing 1 changed file with 19 additions and 1 deletion. Show diff stats Hide diff stats

  1. 20  scripts/subliminal
20  scripts/subliminal
@@ -20,6 +20,8 @@ import argparse
20 20
 import subliminal
21 21
 import logging
22 22
 import os
  23
+import re
  24
+import datetime
23 25
 import sys
24 26
 
25 27
 
@@ -30,6 +32,7 @@ def main():
30 32
     parser.add_argument('-m', '--multi', action='store_true', help='download multiple subtitle languages')
31 33
     parser.add_argument('-f', '--force', action='store_true', help='replace existing subtitle file')
32 34
     parser.add_argument('-w', '--workers', action='store', help='use N threads (default: %(default)s)', metavar='N', type=int, default=4)
  35
+    parser.add_argument('-a', '--age', action='store', help='scan only for files newer (-) or older (+) than this AGE. (e.g. +1w3d6h, -12h)', metavar='AGE', default=None)
33 36
     parser.add_argument('-c', '--compatibility', action='store_true', help='try not to use unicode (use this if you have encoding errors)')
34 37
     group_verbosity = parser.add_mutually_exclusive_group()
35 38
     group_verbosity.add_argument('-q', '--quiet', action='store_true', help='disable output')
@@ -51,6 +54,21 @@ def main():
51 54
     if not os.path.exists(args.cache_dir):
52 55
         os.mkdir(args.cache_dir)
53 56
 
  57
+    # Create filter function
  58
+    if args.age:
  59
+        regex = re.compile(r'^(?P<sign>\+?)((?P<weeks>\d+?)w)?((?P<days>\d+?)d)?((?P<hours>\d+?)h)?')
  60
+        parts = regex.match(args.age)
  61
+        if not parts:
  62
+            raise ValueError('Incorrect age format')
  63
+        time_params = {}
  64
+        parts = parts.groupdict()
  65
+        for name, param in parts.iteritems():
  66
+            if param and name != 'sign':
  67
+                time_params[name] = int(param)
  68
+        scan_filter = lambda x: datetime.datetime.now() - datetime.datetime.fromtimestamp(os.path.getmtime(x)) > datetime.timedelta(**time_params)
  69
+        if parts['sign'] == '+':
  70
+            scan_filter = lambda x: datetime.datetime.now() - datetime.datetime.fromtimestamp(os.path.getmtime(x)) < datetime.timedelta(**time_params)
  71
+
54 72
     # Compatibility mode
55 73
     if args.compatibility:
56 74
         paths = args.paths
@@ -61,7 +79,7 @@ def main():
61 79
     with subliminal.Pool(args.workers) as p:
62 80
         subtitles = p.download_subtitles(paths, languages=args.languages,
63 81
                              services=args.services, cache_dir=args.cache_dir,
64  
-                             force=args.force, multi=args.multi)
  82
+                             force=args.force, multi=args.multi, scan_filter=scan_filter)
65 83
 
66 84
     if not subtitles:
67 85
         if not args.quiet:

0 notes on commit 680c699

Please sign in to comment.
Something went wrong with that request. Please try again.