Skip to content

Commit

Permalink
合并简化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xhlove committed Feb 26, 2022
1 parent 23a6a12 commit 841fb93
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 81 deletions.
63 changes: 5 additions & 58 deletions XstreamDL_CLI/extractors/dash/mpditem.py
@@ -1,4 +1,7 @@
class MPDItem:
from XstreamDL_CLI.extractors.metaitem import MetaItem


class MPDItem(MetaItem):
def __init__(self, name: str = "MPDItem"):
self.name = name
self.innertext = ''
Expand All @@ -14,60 +17,4 @@ def addattrs(self, attrs: dict):
self.addattr(attr_name, attr_value)

def find(self, name: str):
return [child for child in self.childs if child.name == name]

def match_duration(self, _duration: str) -> float:
'''
test samples
- PT50M0S
- PT1H54.600S
- PT23M59.972S
- P8DT11H6M41.1367016S
- P0Y0M0DT0H3M30.000S
'''
if isinstance(_duration, str) is False:
return

def reset_token():
nonlocal token_unit, token_time
token_unit, token_time = '', ''
offset = 0
duration = 0.0
token_unit = ''
token_time = ''
t_flag = False
while offset < len(_duration):
if _duration[offset].isalpha():
token_unit += _duration[offset]
elif _duration[offset].isdigit() or _duration[offset] == '.':
token_time += _duration[offset]
else:
assert False, f'not possible be here _duration => {_duration}'
offset += 1
if token_unit == 'P':
reset_token()
elif token_unit == 'Y' or (t_flag is False and token_unit == 'M'):
# 暂时先不计算年和月 有问题再说
reset_token()
elif token_unit == 'D':
duration += 24 * int(token_time) * 60 * 60
reset_token()
elif token_unit == 'T':
t_flag = True
reset_token()
elif token_unit == 'H':
duration += int(token_time) * 60 * 60
reset_token()
elif token_unit == 'M':
duration += int(token_time) * 60
reset_token()
elif token_unit == 'S':
duration += float("0" + token_time)
reset_token()
return duration

def generate(self):
pass

def to_int(self):
pass
return [child for child in self.childs if child.name == name]
56 changes: 56 additions & 0 deletions XstreamDL_CLI/extractors/metaitem.py
@@ -0,0 +1,56 @@
class MetaItem:
def generate(self):
pass

def to_int(self):
pass

def match_duration(self, _duration: str) -> float:
'''
test samples
- PT50M0S
- PT1H54.600S
- PT23M59.972S
- P8DT11H6M41.1367016S
- P0Y0M0DT0H3M30.000S
'''
if isinstance(_duration, str) is False:
return

def reset_token():
nonlocal token_unit, token_time
token_unit, token_time = '', ''
offset = 0
duration = 0.0
token_unit = ''
token_time = ''
t_flag = False
while offset < len(_duration):
if _duration[offset].isalpha():
token_unit += _duration[offset]
elif _duration[offset].isdigit() or _duration[offset] == '.':
token_time += _duration[offset]
else:
assert False, f'not possible be here _duration => {_duration}'
offset += 1
if token_unit == 'P':
reset_token()
elif token_unit == 'Y' or (t_flag is False and token_unit == 'M'):
# 暂时先不计算年和月 有问题再说
reset_token()
elif token_unit == 'D':
duration += 24 * int(token_time) * 60 * 60
reset_token()
elif token_unit == 'T':
t_flag = True
reset_token()
elif token_unit == 'H':
duration += int(token_time) * 60 * 60
reset_token()
elif token_unit == 'M':
duration += int(token_time) * 60
reset_token()
elif token_unit == 'S':
duration += float("0" + token_time)
reset_token()
return duration
25 changes: 2 additions & 23 deletions XstreamDL_CLI/extractors/mss/ismitem.py
@@ -1,7 +1,7 @@
import re
from XstreamDL_CLI.extractors.metaitem import MetaItem


class ISMItem(object):
class ISMItem(MetaItem):
def __init__(self, name: str = "ISMItem"):
self.name = name
self.innertext = ''
Expand All @@ -19,27 +19,6 @@ def addattrs(self, attrs: dict):
def find(self, name: str):
return [child for child in self.childs if child.name == name]

def match_duration(self, _duration: str) -> float:
if isinstance(_duration, str) is False:
return
duration = re.match(r"PT(\d+)(\.?\d+)S", _duration)
if duration is not None:
return float(duration.group(1)) if duration else 0.0
# PT23M59.972S
duration = re.match(r"PT(\d+)M(\d+)(\.?\d+)S", _duration)
if duration is not None:
_m, _s, _ss = duration.groups()
return int(_m) * 60 + int(_s) + float("0" + _ss)
# P0Y0M0DT0H3M30.000S
duration = re.match(r"PT(\d+)H(\d+)M(\d+)(\.?\d+)S", _duration.replace('0Y0M0D', ''))
if duration is not None:
_h, _m, _s, _ss = duration.groups()
return int(_h) * 60 * 60 + int(_m) * 60 + int(_s) + float("0" + _ss)
return 0.0

def generate(self):
pass

def to_int(self, attr_name: str):
value = self.__getattribute__(attr_name) # type: str
if isinstance(value, str) and value.isdigit():
Expand Down

0 comments on commit 841fb93

Please sign in to comment.