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.

booty.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, dest, action, name=None, format=None):
  22. if not os.path.isdir(dest):
  23. os.makedirs(dest)
  24. args = ['filebot',
  25. '-rename', src,
  26. '-non-strict',
  27. '--output', dest,
  28. '--action', action,
  29. '--conflict', 'index']
  30. if name is not None:
  31. args.append('--filter')
  32. args.append('name == \"' + 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. # Now go over all of the input files and figure out what to do with them.
  50. sources = []
  51. rarregex = re.compile('\\.r[0-9][0-9]')
  52. for f in os.listdir(os.getcwd()):
  53. if os.path.isdir(f):
  54. continue
  55. name, ext = os.path.splitext(f)
  56. if ext in video_exts:
  57. sources.append({ 'src': f, 'action': 'hardlink' })
  58. elif ext == '.rar':
  59. exd = os.path.expanduser(os.path.join(cfg['extractdir'], os.path.basename(f)))
  60. contents = unrar(f, dest=exd)
  61. for ff in os.listdir(contents):
  62. _, ext2 = os.path.splitext(ff)
  63. if ext2 in video_exts:
  64. sources.append({ 'src': os.path.join(contents, ff), 'action': 'move' })
  65. else:
  66. pass
  67. elif ext in meta_exts or rarregex.match(ext) is not None:
  68. pass
  69. else:
  70. print('warning: ignoring file because of extension: ' + f)
  71. #raw_tv_episode_regex = re.compile('\\.S[0-9][0-9]E[0-9][0-9]\\.')
  72. #raw_tv_season_regex = re.compile('\\.S[0-9]\\.')
  73. #tv_name_regex = re.compile('.* -')
  74. #movie_regex = re.compile('.* \\([0-9][0-9][0-9][0-9]\\)')
  75. outdir = cfg['outdir']
  76. if not os.path.isdir(outdir):
  77. os.makedirs(outdir, mode=0o755)
  78. print('created destination directory ' + outdir)
  79. for e in sources:
  80. filebot_rename(e['src'], outdir, e['action'], format='{plex}')