metadatafromtitle.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. import re
  3. from .common import PostProcessor
  4. from ..utils import PostProcessingError
  5. class MetadataFromTitlePPError(PostProcessingError):
  6. pass
  7. class MetadataFromTitlePP(PostProcessor):
  8. def __init__(self, downloader, titleformat):
  9. self._titleformat = titleformat
  10. self._titleregex = self.fmtToRegex(titleformat)
  11. def fmtToRegex(self, fmt):
  12. """
  13. Converts a string like
  14. '%(title)s - %(artist)s'
  15. to a regex like
  16. '(?P<title>.+)\ \-\ (?P<artist>.+)'
  17. and a list of the named groups [title, artist]
  18. """
  19. lastpos = 0
  20. regex = ""
  21. groups = []
  22. # replace %(..)s with regex group and escape other string parts
  23. for match in re.finditer(r'%\((\w+)\)s', fmt):
  24. regex += re.escape(fmt[lastpos:match.start()])
  25. regex += r'(?P<' + match.group(1) + '>.+)'
  26. lastpos = match.end()
  27. if lastpos < len(fmt):
  28. regex += re.escape(fmt[lastpos:len(fmt)])
  29. return regex
  30. def run(self, info):
  31. title = info['title']
  32. match = re.match(self._titleregex, title)
  33. if match is None:
  34. raise MetadataFromTitlePPError('Could not interpret title of video as "%s"' % self._titleformat)
  35. for attribute, value in match.groupdict().items():
  36. value = match.group(attribute)
  37. info[attribute] = value
  38. self._downloader.to_screen('[fromtitle] parsed ' + attribute + ': ' + value)
  39. return True, info