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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if name is not None:
  30. args.append('--filter')
  31. args.append('name == \"' + name + '\"')
  32. if format is not None:
  33. args.append('--format')
  34. args.append(format)
  35. subprocess.run(args)
  36. def unrar(src, dest='extract'):
  37. if not os.path.isdir(dest):
  38. os.makedirs(dest)
  39. subprocess.run(['unrar', 'x', '-y', src, dest + '/'])
  40. return dest
  41. # Load the config.
  42. cfgpath = os.path.join(os.path.expanduser('~'), '.config', 'bootybot.conf')
  43. if 'BOOTYCFG' in os.environ:
  44. cfgpath = os.path.expanduser(os.environ['BOOTYCFG'])
  45. cfg = None
  46. with open(cfgpath) as data:
  47. cfg = json.load(data)
  48. # Now go over all of the input files and figure out what to do with them.
  49. sources = []
  50. rarregex = re.compile('\\.r[0-9][0-9]')
  51. for f in os.listdir(os.getcwd()):
  52. if os.path.isdir(f):
  53. continue
  54. name, ext = os.path.splitext(f)
  55. if ext in video_exts:
  56. sources.append({ 'src': f, 'action': 'hardlink' })
  57. elif ext == '.rar':
  58. contents = unrar(f)
  59. for ff in os.listdir(contents):
  60. _, ext2 = os.path.splitext(ff)
  61. if ext2 in video_exts:
  62. sources.append({ 'src': os.path.join(contents, ff), 'action': 'move' })
  63. else:
  64. pass
  65. elif ext in meta_exts or rarregex.match(ext) is not None:
  66. pass
  67. else:
  68. print('warning: ignoring file because of extension: ' + f)
  69. #raw_tv_episode_regex = re.compile('\\.S[0-9][0-9]E[0-9][0-9]\\.')
  70. #raw_tv_season_regex = re.compile('\\.S[0-9]\\.')
  71. #tv_name_regex = re.compile('.* -')
  72. #movie_regex = re.compile('.* \\([0-9][0-9][0-9][0-9]\\)')
  73. outdir = cfg['outdir']
  74. if not os.path.isdir(outdir):
  75. os.makedirs(outdir, mode=0o755)
  76. print('created destination directory ' + outdir)
  77. for e in sources:
  78. filebot_rename(e['src'], outdir, e['action'], format='{plex}')