2021-01-26 10:20:20 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from .common import PostProcessor
|
|
|
|
from ..compat import compat_str
|
|
|
|
|
|
|
|
|
|
|
|
class MetadataFromFieldPP(PostProcessor):
|
2021-04-21 05:42:04 +00:00
|
|
|
regex = r'(?P<in>.*?)(?<!\\):(?P<out>.+)$'
|
2021-01-26 10:20:20 +00:00
|
|
|
|
|
|
|
def __init__(self, downloader, formats):
|
|
|
|
PostProcessor.__init__(self, downloader)
|
|
|
|
assert isinstance(formats, (list, tuple))
|
|
|
|
self._data = []
|
|
|
|
for f in formats:
|
|
|
|
assert isinstance(f, compat_str)
|
|
|
|
match = re.match(self.regex, f)
|
|
|
|
assert match is not None
|
2021-04-21 05:42:04 +00:00
|
|
|
inp = match.group('in').replace('\\:', ':')
|
2021-01-26 10:20:20 +00:00
|
|
|
self._data.append({
|
2021-04-21 05:42:04 +00:00
|
|
|
'in': inp,
|
2021-03-24 22:02:15 +00:00
|
|
|
'out': match.group('out'),
|
2021-04-21 05:42:04 +00:00
|
|
|
'tmpl': self.field_to_template(inp),
|
2021-03-24 22:02:15 +00:00
|
|
|
'regex': self.format_to_regex(match.group('out')),
|
|
|
|
})
|
2021-01-26 10:20:20 +00:00
|
|
|
|
2021-03-24 22:02:15 +00:00
|
|
|
@staticmethod
|
|
|
|
def field_to_template(tmpl):
|
2021-07-11 23:40:08 +00:00
|
|
|
if re.match(r'[a-zA-Z_]+$', tmpl):
|
2021-03-24 22:02:15 +00:00
|
|
|
return '%%(%s)s' % tmpl
|
|
|
|
return tmpl
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format_to_regex(fmt):
|
2021-01-26 10:20:20 +00:00
|
|
|
r"""
|
|
|
|
Converts a string like
|
|
|
|
'%(title)s - %(artist)s'
|
|
|
|
to a regex like
|
|
|
|
'(?P<title>.+)\ \-\ (?P<artist>.+)'
|
|
|
|
"""
|
|
|
|
if not re.search(r'%\(\w+\)s', fmt):
|
|
|
|
return fmt
|
|
|
|
lastpos = 0
|
|
|
|
regex = ''
|
|
|
|
# replace %(..)s with regex group and escape other string parts
|
|
|
|
for match in re.finditer(r'%\((\w+)\)s', fmt):
|
|
|
|
regex += re.escape(fmt[lastpos:match.start()])
|
2021-04-03 08:29:55 +00:00
|
|
|
regex += r'(?P<%s>.+)' % match.group(1)
|
2021-01-26 10:20:20 +00:00
|
|
|
lastpos = match.end()
|
|
|
|
if lastpos < len(fmt):
|
|
|
|
regex += re.escape(fmt[lastpos:])
|
|
|
|
return regex
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
for dictn in self._data:
|
2021-06-03 18:00:38 +00:00
|
|
|
tmpl, tmpl_dict = self._downloader.prepare_outtmpl(dictn['tmpl'], info)
|
|
|
|
data_to_parse = tmpl % tmpl_dict
|
|
|
|
self.write_debug('Searching for r"%s" in %s' % (dictn['regex'], dictn['tmpl']))
|
2021-03-24 22:02:15 +00:00
|
|
|
match = re.search(dictn['regex'], data_to_parse)
|
2021-01-26 10:20:20 +00:00
|
|
|
if match is None:
|
2021-03-24 22:02:15 +00:00
|
|
|
self.report_warning('Could not interpret video %s as "%s"' % (dictn['in'], dictn['out']))
|
2021-01-26 10:20:20 +00:00
|
|
|
continue
|
|
|
|
for attribute, value in match.groupdict().items():
|
|
|
|
info[attribute] = value
|
2021-07-11 23:40:08 +00:00
|
|
|
self.to_screen('parsed %s from "%s": %s' % (attribute, dictn['tmpl'], value if value is not None else 'NA'))
|
2021-01-26 10:20:20 +00:00
|
|
|
return [], info
|
|
|
|
|
|
|
|
|
|
|
|
class MetadataFromTitlePP(MetadataFromFieldPP): # for backward compatibility
|
|
|
|
def __init__(self, downloader, titleformat):
|
2021-04-21 05:42:04 +00:00
|
|
|
super(MetadataFromTitlePP, self).__init__(downloader, ['%%(title)s:%s' % titleformat])
|
2021-01-26 10:20:20 +00:00
|
|
|
self._titleformat = titleformat
|
|
|
|
self._titleregex = self._data[0]['regex']
|