@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import re
import re
@ -12,20 +12,19 @@ class MetadataFromTitlePPError(PostProcessingError):
class MetadataFromTitlePP ( PostProcessor ) :
class MetadataFromTitlePP ( PostProcessor ) :
def __init__ ( self , downloader , titleformat ) :
def __init__ ( self , downloader , titleformat ) :
super ( MetadataFromTitlePP , self ) . __init__ ( downloader )
self . _titleformat = titleformat
self . _titleformat = titleformat
self . _titleregex = self . fmtToR egex ( titleformat )
self . _titleregex = self . format_to_r egex ( titleformat )
def fmtToR egex ( self , fmt ) :
def format_to_r egex ( self , fmt ) :
"""
"""
Converts a string like
Converts a string like
' %(title)s - %(artist)s '
' %(title)s - %(artist)s '
to a regex like
to a regex like
' (?P<title>.+) \ \ - \ (?P<artist>.+) '
' (?P<title>.+) \ \ - \ (?P<artist>.+) '
and a list of the named groups [ title , artist ]
"""
"""
lastpos = 0
lastpos = 0
regex = " "
regex = " "
groups = [ ]
# replace %(..)s with regex group and escape other string parts
# replace %(..)s with regex group and escape other string parts
for match in re . finditer ( r ' % \ (( \ w+) \ )s ' , fmt ) :
for match in re . finditer ( r ' % \ (( \ w+) \ )s ' , fmt ) :
regex + = re . escape ( fmt [ lastpos : match . start ( ) ] )
regex + = re . escape ( fmt [ lastpos : match . start ( ) ] )