Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Quantities: Size | ||
""" | ||
import re | ||
from abc import abstractmethod | ||
|
||
import six | ||
|
||
from ..common import seps | ||
|
||
|
||
class Quantity(object): | ||
""" | ||
Represent a quantity object with magnitude and units. | ||
""" | ||
|
||
parser_re = re.compile(r'(?P<magnitude>\d+(?:[.]\d+)?)(?P<units>[^\d]+)') | ||
|
||
def __init__(self, magnitude, units): | ||
self.magnitude = magnitude | ||
self.units = units | ||
|
||
@classmethod | ||
@abstractmethod | ||
def parse_units(cls, value): | ||
""" | ||
Parse a string to a proper unit notation. | ||
""" | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
def fromstring(cls, string): | ||
""" | ||
Parse the string into a quantity object. | ||
:param string: | ||
:return: | ||
""" | ||
values = cls.parser_re.match(string).groupdict() | ||
try: | ||
magnitude = int(values['magnitude']) | ||
except ValueError: | ||
magnitude = float(values['magnitude']) | ||
units = cls.parse_units(values['units']) | ||
|
||
return cls(magnitude, units) | ||
|
||
def __hash__(self): | ||
return hash(str(self)) | ||
|
||
def __eq__(self, other): | ||
if isinstance(other, six.string_types): | ||
return str(self) == other | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
return self.magnitude == other.magnitude and self.units == other.units | ||
|
||
def __ne__(self, other): | ||
return not self == other | ||
|
||
def __repr__(self): | ||
return '<{0} [{1}]>'.format(self.__class__.__name__, self) | ||
|
||
def __str__(self): | ||
return '{0}{1}'.format(self.magnitude, self.units) | ||
|
||
|
||
class Size(Quantity): | ||
""" | ||
Represent size. | ||
e.g.: 1.1GB, 300MB | ||
""" | ||
|
||
@classmethod | ||
def parse_units(cls, value): | ||
return value.strip(seps).upper() | ||
|
||
|
||
class BitRate(Quantity): | ||
""" | ||
Represent bit rate. | ||
e.g.: 320Kbps, 1.5Mbps | ||
""" | ||
|
||
@classmethod | ||
def parse_units(cls, value): | ||
return value.strip(seps).capitalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
video_bit_rate and audio_bit_rate properties | ||
""" | ||
import re | ||
|
||
from rebulk import Rebulk | ||
from rebulk.rules import Rule, RenameMatch | ||
|
||
from ..common import dash, seps | ||
from ..common.quantity import BitRate | ||
from ..common.validators import seps_surround | ||
|
||
|
||
def bit_rate(): | ||
""" | ||
Builder for rebulk object. | ||
:return: Created Rebulk object | ||
:rtype: Rebulk | ||
""" | ||
rebulk = Rebulk().regex_defaults(flags=re.IGNORECASE, abbreviations=[dash]) | ||
rebulk.defaults(name='audio_bit_rate', validator=seps_surround) | ||
rebulk.regex(r'\d+-?[kmg]bps', r'\d+\.\d+-?[kmg]bps', | ||
conflict_solver=( | ||
lambda match, other: match | ||
if other.name == 'audio_channels' and 'weak-audio_channels' not in other.tags | ||
else other | ||
), | ||
formatter=BitRate.fromstring, tags=['release-group-prefix']) | ||
|
||
rebulk.rules(BitRateTypeRule) | ||
|
||
return rebulk | ||
|
||
|
||
class BitRateTypeRule(Rule): | ||
""" | ||
Convert audio bit rate guess into video bit rate. | ||
""" | ||
consequence = RenameMatch('video_bit_rate') | ||
|
||
def when(self, matches, context): | ||
for match in matches.named('audio_bit_rate'): | ||
previous = matches.previous(match, index=0, | ||
predicate=lambda m: m.name in ('source', 'screen_size', 'video_codec')) | ||
if previous and not matches.holes(previous.end, match.start, predicate=lambda m: m.value.strip(seps)): | ||
yield match |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters