-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmappings.py
164 lines (153 loc) · 4.23 KB
/
mappings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import contextlib
import datetime
import os
import re
import typing
import validators
from . import args
class Resource(typing.NamedTuple):
url: str = None
start: float = -1
final: float = -1
class Mapping(typing.NamedTuple):
source: Resource = None
target: Resource = None
def read(file):
file = file or args.mappings
if not os.path.exists(file):
return []
with open(file, 'r') as f:
mappings = \
list(
map(
parse_mapping,
f.read().splitlines()
)
)
return mappings
def write(file, mappings):
with open(args.mappings, 'w') as f:
f.writelines(
map(format_mapping, mappings)
)
if args.output_subtitles:
write_subtitles(
args.subtitles_output
.format(args.media_output)
if '{}' in args.subtitles_output
else args.subtitles_output,
mappings
)
def parse_mapping(s):
t = s.split('\t')
if t[2:]:
regexp = None
with contextlib.suppress(re.error):
regexp = re.compile(t[2])
if not 'regexp' in locals() and \
not os.path.isfile(t[2]) and \
not validators.url(
t[2].replace('---', '-')
):
raise ValueError(
f'Invalid value "{t[2]}".'
)
return Mapping(
source=
Resource(
url=t[2] if t[2:] else None,
start=parse_timestamp(t[3])
if t[3:] else -1,
final=parse_timestamp(t[4])
if t[4:] else -1
),
target=
Resource(
start=parse_timestamp(t[0]),
final=parse_timestamp(t[1])
)
)
def format_mapping(mapping):
_mapping = [
format_timestamp(mapping.target.start),
format_timestamp(mapping.target.final)
]
if mapping.source:
if mapping.source.url:
_mapping.append(mapping.source.url)
if mapping.source.start != -1:
_mapping.append(
format_timestamp(
mapping.source.start
)
)
if mapping.source.final != -1:
_mapping.append(
format_timestamp(
mapping.source.final
)
)
return '\t'.join(_mapping) + '\n'
def read_subtitles(file):
with open(file, 'r') as f:
return [
Mapping(
target=
Resource(
start=
parse_timestamp(t[0]),
final=
parse_timestamp(t[1])
)
) for s in f.read().splitlines()
for t in s.split(' --> ')
if t[1:]
]
def write_subtitles(file, mappings):
with open(file, 'w') as f:
f.writelines(
'{}\n{} --> {}\n{}\n\n'
.format(
index + 1,
format_timestamp(
item.target.start
),
format_timestamp(
item.target.final
),
index + 1
) for index, item in
enumerate(mappings)
)
def parse_timestamp(s):
try:
return float(s)
except ValueError:
pass
t = s.split(':')
if t[1:] and int(t[0]) >= 60:
s = ':'.join((
str(int(t[0]) // 60),
str(int(t[0]) % 60).zfill(2),
*t[1:]
))
if '.' not in s:
s += '.0'
try:
t = datetime.datetime \
.strptime(s, '%H:%M:%S.%f')
except ValueError:
try:
t = datetime.datetime \
.strptime(s, '%H:%M:%S.%f')
except ValueError:
t = datetime.datetime \
.strptime(s, '%M:%S.%f')
return t.hour * 3600 + \
t.minute * 60 + \
t.second + \
t.microsecond * 0.000001
def format_timestamp(t):
return datetime.datetime \
.utcfromtimestamp(t) \
.strftime('%H:%M:%S.%f')[:-3]