Use pysrt to provide better content search of srt files.
This commit is contained in:
parent
0fe9364d8d
commit
fa2610e206
1 changed files with 29 additions and 0 deletions
29
search.py
29
search.py
|
@ -9,6 +9,10 @@ try:
|
||||||
import winshell
|
import winshell
|
||||||
except ImportError:
|
except ImportError:
|
||||||
winshell = None
|
winshell = None
|
||||||
|
try:
|
||||||
|
import pysrt
|
||||||
|
except ImportError:
|
||||||
|
pysrt = None
|
||||||
|
|
||||||
from voussoirkit import expressionmatch
|
from voussoirkit import expressionmatch
|
||||||
from voussoirkit import pathclass
|
from voussoirkit import pathclass
|
||||||
|
@ -90,6 +94,29 @@ def search_contents_generic(filepath, content_args):
|
||||||
yield from results
|
yield from results
|
||||||
yield ''
|
yield ''
|
||||||
|
|
||||||
|
def _srt_format_line(line):
|
||||||
|
text = line.text.replace('\n', ' ')
|
||||||
|
timestamp = f'{line.start.hours:02d}:{line.start.minutes:02d}:{line.start.seconds:02d}:{line.start.milliseconds:03d}'
|
||||||
|
return f'{timestamp} {text}'
|
||||||
|
|
||||||
|
def search_contents_srt(filepath, content_args):
|
||||||
|
try:
|
||||||
|
srtlines = pysrt.open(filepath.absolute_path)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
log.warn('%s experienced Unicode Error', filepath.absolute_path)
|
||||||
|
return
|
||||||
|
|
||||||
|
content_args['text'] = '\n'.join(_srt_format_line(line) for line in srtlines)
|
||||||
|
|
||||||
|
results = search(**content_args)
|
||||||
|
results = list(results)
|
||||||
|
if not results:
|
||||||
|
return
|
||||||
|
|
||||||
|
yield filepath.absolute_path
|
||||||
|
yield from results
|
||||||
|
yield ''
|
||||||
|
|
||||||
def search_contents_windows_lnk(filepath, content_args):
|
def search_contents_windows_lnk(filepath, content_args):
|
||||||
try:
|
try:
|
||||||
shortcut = winshell.Shortcut(filepath.absolute_path)
|
shortcut = winshell.Shortcut(filepath.absolute_path)
|
||||||
|
@ -227,6 +254,8 @@ def search(
|
||||||
|
|
||||||
if filepath.extension == 'lnk' and winshell:
|
if filepath.extension == 'lnk' and winshell:
|
||||||
yield from search_contents_windows_lnk(filepath, content_args)
|
yield from search_contents_windows_lnk(filepath, content_args)
|
||||||
|
if filepath.extension == 'srt' and pysrt:
|
||||||
|
yield from search_contents_srt(filepath, content_args)
|
||||||
else:
|
else:
|
||||||
yield from search_contents_generic(filepath, content_args)
|
yield from search_contents_generic(filepath, content_args)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue