Gfy

nzb_age.py

Gfy
Dec 15th, 2011
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program.  If not, see <http://www.gnu.org/licenses/>
  16.  
  17. import optparse
  18. import sys
  19. import os
  20. import re
  21. import datetime
  22.  
  23. def main(options, args):
  24.     if len(args) < 2:
  25.         print("Usage: start-end directory")
  26.         sys.exit(1)
  27.  
  28.     (start, end) = args[0].split("-")
  29.     (start, end) = (int(start), int(end))
  30.     dir = args[1]
  31.     print("Start: %d days, end: %d days." % (start, end))
  32.    
  33.     if options.output_dir:
  34.         print("Moving NZB files to %s" % options.output_dir)
  35.  
  36.     for file in os.listdir(dir):
  37.         if file[-4:] != ".nzb":
  38.             continue
  39.         date = None
  40.         # not parsing the whole file, just what we need
  41.         for line in open(os.path.join(dir, file), "r").readlines():
  42.             match = re.match(".* date=\"(\d+)\".*", line)
  43.             if match:  
  44.                 date = datetime.datetime.fromtimestamp(int(match.group(1)))
  45.                 break
  46.         if date:
  47.             diff = datetime.datetime.now() - date
  48.             if start < diff.days < end:
  49.                 if options.output_dir:
  50.                     old = os.path.join(dir, file)
  51.                     os.renames(old, os.path.join(options.output_dir, file))
  52.                 else:
  53.                     print(file)
  54.         else:
  55.             print("Bad NZB file: %s" % file)
  56.        
  57. if __name__ == '__main__':
  58.     parser = optparse.OptionParser(
  59.         usage="Usage: %prog from-to directory\n"
  60.               "       e.g. %prog 1000-1200 . -o /will/move/nzbs\n"
  61.         "This tool will list or move NZB files between a certain age, "
  62.         "expressed in days.\n",
  63.         version="%prog 1.0 (2011-12-15)") # --help, --version
  64.    
  65.     parser.add_option("-o", help="where to move the matched NZBs to.",
  66.                     dest="output_dir", metavar="DIRECTORY", default=None)
  67.    
  68.     # no arguments given
  69.     if len(sys.argv) < 2:
  70.         print(parser.format_help())
  71.     else:
  72.         (options, args) = parser.parse_args()
  73.         main(options, args)
RAW Paste Data