Moves map to config
Adds suggested characters in terminal Adds ignored list Some cosmetic changes
This commit is contained in:
@@ -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
@@ -0,0 +1,33 @@
|
||||
characters:
|
||||
¿: ż
|
||||
³: ł
|
||||
æ: ć
|
||||
¹: ą
|
||||
ê: ę
|
||||
ñ: ń
|
||||
£: Ł
|
||||
¯: Ż
|
||||
œ: ś
|
||||
Ÿ: ź
|
||||
Œ: Ś
|
||||
|
||||
ignored:
|
||||
- ą
|
||||
- ć
|
||||
- ę
|
||||
- ł
|
||||
- ń
|
||||
- ó
|
||||
- ś
|
||||
- ź
|
||||
- ż
|
||||
- Ą
|
||||
- Ć
|
||||
- Ę
|
||||
- Ł
|
||||
- Ń
|
||||
- Ó
|
||||
- Ś
|
||||
- Ź
|
||||
- Ż
|
||||
|
||||
@@ -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
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user