Moves map to config

Adds suggested characters in terminal
Adds ignored list
Some cosmetic changes
This commit is contained in:
Wojciech Burda
2023-02-06 18:43:36 +01:00
parent a916b6f27c
commit f36b5549f1
4 changed files with 60 additions and 54 deletions
+11 -1
View File
@@ -5,12 +5,22 @@ Simple script to:
- convert line endings to UNIX standard
- fix broken characters
## Configuration
You can place your own map in `characters` section of `config.yaml` file.
Default configuration includes polish characters.
When run in terminal, script lists all characters found in the subtitles file that are not in ASCII or ignored list.
## Usage
```bash
./subtitles-fixer.py /path/to/subtitles.ext
$ python3 /path/to/subtitles-fixer.py /path/to/subtitles.ext
```
## Bonus
Nemo action for context menu. Place `fix-subtitles.nemo_action` in `~/.local/share/nemo/actions/` and restart nemo.
## To do
- Translation?
+33
View File
@@ -0,0 +1,33 @@
characters:
¿: ż
³: ł
æ: ć
¹: ą
ê: ę
ñ: ń
£: Ł
¯: Ż
œ: ś
Ÿ: ź
Œ: Ś
ignored:
- ą
- ć
- ę
- ł
- ń
- ó
- ś
- ź
- ż
- Ą
- Ć
- Ę
- Ł
- Ń
- Ó
- Ś
- Ź
- Ż
+1 -1
View File
@@ -3,7 +3,7 @@
Active=true
Name=Napraw napisy
Comment=Odpala skrypt do naprawiania napisów
Exec=/path/to/subtitles-fixer.py %F
Exec=python3 /path/to/subtitles-fixer.py %F
Icon-Name=edit
Selection=s
Extensions=txt;srt;sub;
+15 -52
View File
@@ -4,6 +4,7 @@ import sys
import os.path
import subprocess
import chardet
import yaml
def notify(header, body, exitCode):
subprocess.run(['/usr/bin/notify-send', header, body])
@@ -24,58 +25,10 @@ filepath = sys.argv[1]
if not os.path.isfile(filepath):
notify('Błąd', 'Aby naprawić napisy, musisz wskazać plik', 1)
map = {
'': '',
'Ä…': 'ą',
'¹': 'ą',
'š': 'ą',
'ć': 'ć',
'ĂŚ': 'ć',
'æ': 'ć',
'ć': 'ć',
'Ä™': 'ę',
'ĂŞ': 'ę',
'ê': 'ę',
'ę': 'ę',
'Ĺ‚': 'ł',
'Âł': 'ł',
'³': 'ł',
'ł': 'ł',
'Ĺ„': 'ń',
'Ăą': 'ń',
'ñ': 'ń',
'ń': 'ń',
'Ăł': 'ó',
'Ĺ›': 'ś',
'œ': 'ś',
'œ': 'ś',
'œ': 'ś',
'Ĺş': 'ź',
'Ÿ': 'ź',
'ĹĽ': 'ż',
'Âż': 'ż',
'¿': 'ż',
'Ĺź': 'ź',
'Ÿ': 'ź',
'Ÿ': 'ź',
'Ä„': 'Ą',
'Ć': 'Ć',
'Æ': 'Ć',
'Ę': 'Ę',
'Ê': 'Ę',
'Ł': 'Ł',
'£': 'Ł',
#'': 'Ń',
'Ñ': 'Ń',
'Ń': 'Ń',
#'': 'Ó',
'Ĺš': 'Ś',
'Œ': 'Ś',
'Œ': 'Ś',
'': 'Ź',
'Ĺ»': 'Ż',
'¯': 'Ż',
}
with open('config.yaml', 'r') as configFile:
config = yaml.safe_load(configFile)
configFile.close()
map = config['characters']
try:
encoding = detectEncodingOfAFile(filepath)
@@ -89,6 +42,16 @@ try:
for badChar, goodChar in map.items():
text = text.replace(badChar, goodChar)
badChars = []
for c in text:
if ord(c) > 127 and not c in badChars:
badChars.append(c)
for char in config['ignored']:
if char in badChars:
badChars.remove(char)
if len(badChars):
print('Znalezione znaki, które można dodać do mapy: ' + badChars.__str__())
with open(filepath, 'w', newline='\n') as f:
f.write(text)
f.close()