forked from vdcrim/AvsP-macros
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chapter names from file.py
105 lines (86 loc) · 3.81 KB
/
Chapter names from file.py
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
# -*- coding: utf-8 -*-
"""
Replace default chapter names in Matroska chapter files with names provided
in a specified file, one chapter name per line. All xml files in the given
directory are processed.
Intended for use with vfr.py (http://forum.doom9.org/showthread.php?t=154535)
Date: 2012-09-11
Latest version: https://github.com/vdcrim/avsp-macros
Doom9 Forum thread: http://forum.doom9.org/showthread.php?t=163653
Changelog:
- update prompt dialog
- fix macro doesn't working - a 'return' was left from testing
- other fixes
- fix Python 2.6 compatibility
Copyright (C) 2012 Diego Fernández Gosende <[email protected]>
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 2 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 <http://www.gnu.org/licenses/gpl-2.0.html>.
"""
# PREFERENCES
# Default name of chapters (it can be a regular expression)
default_name = r'Chapter'
# Filename list for chapter names file search
chapters_name_filename = ['chapter names.txt', 'chapter_names.txt',
'chapters.txt', 'cnames.txt', 'chapters'
'chapter titles.txt', 'chapter_titles.txt',
'titles.txt', 'ctitles.txt', 'titles']
# ------------------------------------------------------------------------------
# run in thread
from os import listdir
from os.path import isfile, isdir, dirname, join
import re
avs = avsp.GetScriptFilename()
chapters_directory = dirname(avs)
chapters_name_filename += [name.capitalize() for name in chapters_name_filename]
for path in (join(chapters_directory, name) for name in chapters_name_filename):
if isfile(path):
names_file = path
break
else:
names_file = ''
txt_filter = (_('Text files') + ' (*.txt)|*.txt|' + _('All files') + '|*.*')
options = avsp.GetTextEntry(title='Chapter names from file',
message=['Chapter names file',
'Matroska chapter files directory'],
default=[(names_file, txt_filter), chapters_directory],
types=['file_open', 'dir'])
if not options:
return
else:
names_file, chapters_directory = options
if not isfile(names_file):
avsp.MsgBox('Chapter names file not found:\n' + names_file)
return
if not isdir(chapters_directory):
avsp.MsgBox('The specified directory does not exist:\n' + chapters_directory)
return
with open(names_file) as names_file:
names = names_file.readlines()
xml_list = filter(lambda path: path.endswith('.xml'), listdir(chapters_directory))
re_chapter = re.compile(
r'(^.*<ChapterString>)\s*' + default_name + r'\s*(</ChapterString>.*$)')
chapter = 0
for xml in xml_list:
with open(join(chapters_directory, xml), 'r+') as chapter_file:
lines = chapter_file.readlines()
for i, line in enumerate(lines):
res = re_chapter.search(line)
if res:
if len(names) < chapter + 1:
avsp.MsgBox('Not enough chapter names: {0} titles, {1} files'
.format(len(names), chapter + 1), 'Warning')
return
lines[i] = re_chapter.sub(
r'\g<1>{0}\g<2>'.format(names[chapter].strip()), line)
chapter += 1
chapter_file.seek(0)
chapter_file.truncate()
chapter_file.writelines(lines)