You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import re
  5. import subprocess
  6. import shutil
  7. import json
  8. video_exts = [
  9. '.mkv',
  10. '.mp4',
  11. '.avi'
  12. ]
  13. meta_exts = [
  14. '.nfo',
  15. '.srt', # We can get these on our own.
  16. '.jpg',
  17. '.jpeg',
  18. '.png',
  19. '.bmp'
  20. ]
  21. def filebot_rename(src, destdir, action, name=None, format=None):
  22. if not os.path.isdir(destdir):
  23. os.makedirs(destdir)
  24. args = ['filebot',
  25. '-rename', src,
  26. '-non-strict',
  27. '--output', destdir,
  28. '--action', action,
  29. '--conflict', 'index']
  30. if name is not None:
  31. args.append('--filter')
  32. args.append('n == \"' + name + '\"')
  33. if format is not None:
  34. args.append('--format')
  35. args.append(format)
  36. subprocess.run(args)
  37. def unrar(src, dest='extract'):
  38. if not os.path.isdir(dest):
  39. os.makedirs(dest)
  40. subprocess.run(['unrar', 'x', '-y', src, dest + '/'])
  41. return dest
  42. # Load the config.
  43. cfgpath = os.path.join(os.path.expanduser('~'), '.config', 'bootybot.conf')
  44. if 'BOOTYCFG' in os.environ:
  45. cfgpath = os.path.expanduser(os.environ['BOOTYCFG'])
  46. cfg = None
  47. with open(cfgpath) as data:
  48. cfg = json.load(data)
  49. # Load custom title names for TV shows.
  50. title_overrides = []
  51. for t in cfg['overrides']:
  52. title_overrides.append({
  53. 'name': t['name'],
  54. 'pat': re.compile(t['pattern'])
  55. })
  56. raw_tv_regex = re.compile('.*\\.S[0-9][0-9](E[0-9][0-9])?\\..*')
  57. def gen_title_action(fpath, action):
  58. mname = None
  59. bn = os.path.basename(fpath)
  60. # Check to see if it's a TV show and if we can provide a specific title for it.
  61. if raw_tv_regex.match(bn) is not None:
  62. for to in title_overrides:
  63. if to['pat'].match(bn) is not None:
  64. mname = to['name']
  65. print('found title ' + to['name'] + ' for ' + fpath)
  66. return { 'src': fpath, 'action': action, 'medianame': mname }
  67. def get_simple_rename_action():
  68. if 'BOOTY_SIMPLE_RENAME_ACTION' in os.environ:
  69. return os.environ['BOOTY_SIMPLE_RENAME_ACTION']
  70. else:
  71. return cfg['simpleaction']
  72. # Now go over all of the input files and figure out what to do with them.
  73. sources = []
  74. rarregex = re.compile('\\.r[0-9][0-9]')
  75. for f in os.listdir(os.getcwd()):
  76. if os.path.isdir(f):
  77. continue
  78. name, ext = os.path.splitext(f)
  79. if ext in video_exts:
  80. sources.append(gen_title_action(f, get_simple_rename_action()))
  81. elif ext == '.rar':
  82. # RARs are a little more complicated to handle.
  83. exd = os.path.expanduser(os.path.join(cfg['extractdir'], os.path.basename(f)))
  84. contents = unrar(f, dest=exd)
  85. for ff in os.listdir(contents):
  86. _, ext2 = os.path.splitext(ff)
  87. if ext2 in video_exts:
  88. sources.append(gen_title_action(os.path.join(contents, ff), 'move'))
  89. else:
  90. pass
  91. elif ext in meta_exts or rarregex.match(ext) is not None:
  92. pass
  93. else:
  94. print('warning: ignoring file because of extension: ' + f)
  95. # Create the output directory if it doesn't exist yet.
  96. outdir = cfg['outdir']
  97. if not os.path.isdir(outdir):
  98. os.makedirs(outdir, mode=0o755)
  99. print('created destination directory ' + outdir)
  100. for e in sources:
  101. filebot_rename(e['src'], outdir, e['action'], name=e['medianame'], format='{plex}')