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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
"""Main Subscleaner module."""
"""
Subscleaner.
Copyright (C) 2023 Roger Gonzalez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import re
import sys
import time
import chardet
import pysrt
AD_PATTERNS = [
re.compile(r"\bnordvpn\b", re.IGNORECASE),
re.compile(r"\ba Card Shark AMERICASCARDROOM\b", re.IGNORECASE),
re.compile(r"\bOpenSubtitles\b", re.IGNORECASE),
re.compile(r"\bAdvertise your product or brand here\b", re.IGNORECASE),
re.compile(r"\bApóyanos y conviértete en miembro VIP Para\b", re.IGNORECASE),
re.compile(r"\bAddic7ed\b", re.IGNORECASE),
re.compile(r"\bargenteam\b", re.IGNORECASE),
re.compile(r"\bAllSubs\b", re.IGNORECASE),
re.compile(r"\bCreated and Encoded by\b", re.IGNORECASE),
re.compile(r"\bcorrected\s+by\b", re.IGNORECASE),
re.compile(r"\bEntre a AmericasCardroom\.com Hoy\b", re.IGNORECASE),
re.compile(r"\bEveryone is intimidated by a shark\. Become\b", re.IGNORECASE),
re.compile(r"\bJuegue Poker en Línea por Dinero Real\b", re.IGNORECASE),
re.compile(r"\bOpen Subtitles\b", re.IGNORECASE),
re.compile(r"\bMKV Player\b", re.IGNORECASE),
re.compile(r"\bResync\s+for\b", re.IGNORECASE),
re.compile(r"\bResync\s+improved\b", re.IGNORECASE),
re.compile(r"\bRipped\s+By\b", re.IGNORECASE),
re.compile(r'\bSigue "Community" en\b', re.IGNORECASE),
re.compile(r"\bSubtitles\s+by\b", re.IGNORECASE),
re.compile(r"\bSubt[íi]tulos\s+por\b", re.IGNORECASE),
re.compile(r"\bSupport us and become VIP member\b", re.IGNORECASE),
re.compile(r"\bSubs\s+Team\b", re.IGNORECASE),
re.compile(r"\bsubscene\b", re.IGNORECASE),
re.compile(r"\bSubtitulado por\b", re.IGNORECASE),
re.compile(r"\bsubtitulamos\b", re.IGNORECASE),
re.compile(r"\bSynchronized\s+by\b", re.IGNORECASE),
re.compile(r"\bSincronizado y corregido por\b", re.IGNORECASE),
re.compile(r"\bsubdivx\b", re.IGNORECASE),
re.compile(r"\bSync\s+Corrected\b", re.IGNORECASE),
re.compile(r"\bSync\s+corrections\s+by\b", re.IGNORECASE),
re.compile(r"\bsync and corrections by\b", re.IGNORECASE),
re.compile(r"\bSync\s+by\b", re.IGNORECASE),
re.compile(r"\bUna\s+traducci[óo]n\s+de\b", re.IGNORECASE),
re.compile(r"\btvsubtitles\b", re.IGNORECASE),
re.compile(r"\bTacho8\b", re.IGNORECASE),
re.compile(r"\bwww\.\S+\.com\b", re.IGNORECASE),
re.compile(r"\bwww\.\S+\.es\b", re.IGNORECASE),
]
def contains_ad(subtitle_line: str) -> bool:
"""
Check if the given subtitle line contains an ad.
Args:
subtitle_line (str): The subtitle line to be checked.
Returns:
bool: True if the subtitle line contains an ad, False otherwise.
"""
return any(pattern.search(subtitle_line) for pattern in AD_PATTERNS)
def is_processed_before(subtitle_file: str) -> bool:
"""
Check if the subtitle file has already been processed.
Args:
subtitle_file (str): The path to the subtitle file.
Returns:
bool: True if the subtitle file has already been processed, False otherwise.
"""
file_creation_time = os.path.getctime(subtitle_file)
processed_timestamp = time.mktime(
time.strptime("2021-05-13 00:00:00", "%Y-%m-%d %H:%M:%S"),
)
return file_creation_time < processed_timestamp
def get_encoding(subtitle_file: str) -> str:
"""
Detect the encoding of the subtitle file.
Args:
subtitle_file (str): The path to the subtitle file.
Returns:
str: The detected encoding of the subtitle file.
"""
with open(subtitle_file, "rb") as file:
return chardet.detect(file.read())["encoding"]
def remove_ad_lines(subtitle_data: pysrt.SubRipFile) -> bool:
"""
Remove ad lines from the subtitle data.
Args:
subtitle_data (pysrt.SubRipFile): The subtitle data object.
Returns:
bool: True if the subtitle data was modified, False otherwise.
"""
modified = False
for index, subtitle in enumerate(subtitle_data):
if contains_ad(subtitle.text):
print(f"Removing: {subtitle}\n")
del subtitle_data[index]
modified = True
return modified
def process_subtitle_file(subtitle_file: str) -> bool:
"""
Process a subtitle file to remove ad lines.
Args:
subtitle_file (str): The path to the subtitle file.
Returns:
bool: True if the subtitle file was modified, False otherwise.
"""
try:
if is_processed_before(subtitle_file):
print(f"Already processed {subtitle_file}")
return False
print(f"Analyzing: {subtitle_file}")
encoding = get_encoding(subtitle_file)
subtitle_data = pysrt.open(subtitle_file, encoding=encoding)
if remove_ad_lines(subtitle_data):
print(f"Saving {subtitle_file}")
subtitle_data.save(subtitle_file)
return True
return False
except Exception as e:
print(f"Error processing {subtitle_file}: {e}")
return False
def process_subtitle_files(subtitle_files: list[str]) -> list[str]:
"""
Process multiple subtitle files to remove ad lines.
Args:
subtitle_files (list[str]): A list of subtitle file paths.
Returns:
list[str]: A list of modified subtitle file paths.
"""
modified_files = []
for subtitle_file in subtitle_files:
if process_subtitle_file(subtitle_file):
modified_files.append(subtitle_file)
return modified_files
def main():
"""
Process subtitle files to remove ad lines.
Read subtitle file paths from standard input, process each file to remove ad lines,
and print the result. Keep track of the modified files and print
a summary at the end.
"""
subtitle_files = [file_path.strip() for file_path in sys.stdin]
print("Starting script")
modified_files = process_subtitle_files(subtitle_files)
if modified_files:
print(f"Modified {len(modified_files)} files")
print("Done")
if __name__ == "__main__":
main()
|