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
|
#!/usr/bin/python3
"""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
ADS = [
r".*nordvpn.*",
r".*a Card Shark AMERICASCARDROOM.*",
r".*OpenSubtitles.*",
r".*Advertise your product or brand here.*",
r".*Apóyanos y conviértete en miembro VIP Para.*",
r".*Addic7ed.*",
r".*argenteam.*",
r".*AllSubs.*",
r"Created and Encoded by.*",
r".*corrected.*by.*",
r".*Entre a AmericasCardroom. com Hoy.*",
r".*Everyone is intimidated by a shark. Become.*",
r".*Juegue Poker en Línea por Dinero Real.*",
r".*OpenSubtitles.*",
r".*Open Subtitles.*",
r".*MKV Player.*",
r".*MKV player.*",
r".*Resync.*for.*",
r".*Resync.*improved.*",
r".*Ripped?By.*",
r'.*Sigue "Community" en.*',
r".*Subtitles.*by.*",
r".*Subt?tulos.*por.*",
r".*Support us and become VIP member.*",
r".*Subs.*Team.*",
r".*subscene.*",
r".*Subtitulado por.*",
r".*subtitulamos.*",
r".*Synchronized.*by.*",
r".*Sincronizado y corregido por.*",
r".*subdivx.*",
r".*Sync.*Corrected.*",
r".*Sync.*corrections.*by.*",
r".*sync and corrections by.*" r".*Sync.*by.*",
r".*Una.*traducci?n.*de.*",
r".*tvsubtitles.*",
r".*Una.*traducci?n.*de.*",
"Tacho8",
r".*www. com.*",
r".*www. es.*",
]
def ads_in_line(line: str) -> bool:
"""
Check if the given line contains an ad.
Args:
line (str): The line of text to be checked.
Returns:
bool: True if the line contains an ad, False otherwise.
"""
return any(re.match(ad, line, re.DOTALL) for ad in ADS)
def process_file(filename):
"""
Process a subtitle file to remove ads.
Args:
filename (str): The path to the subtitle file.
Returns:
bool: True if the file was modified, False otherwise.
"""
try:
created = os.path.getctime(filename)
already_processed = time.mktime(
time.strptime("2021-05-13 00:00:00", "%Y-%m-%d %H:%M:%S"),
)
if created < already_processed:
print(f"Already processed {filename}")
return False
print(f"Analyzing: {filename}")
with open(filename, "rb") as f:
encoding = chardet.detect(f.read())["encoding"]
subs = pysrt.open(filename, encoding=encoding)
modified = False
for i, line in enumerate(subs):
if ads_in_line(line.text):
print(f"Removing: {line}\n")
del subs[i]
modified = True
if modified:
print(f"Saving {filename}")
subs.save(filename)
return True
return False
except Exception as e:
print(f"Error processing {filename}: {e}")
return False
def main():
"""
Process subtitle files to remove ads.
Read filenames from standard input, process each file to remove ads,
and print the result. Keep track of the modified files and print
a summary at the end.
"""
modified_files = []
print("Starting script")
for filename in sys.stdin:
filename = filename.strip()
if process_file(filename):
modified_files.append(filename)
if modified_files:
print(f"Modified {len(modified_files)} files")
print("Done")
if __name__ == "__main__":
main()
|