forked from tlverwijst/SubLime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.py
More file actions
463 lines (350 loc) · 15.4 KB
/
Copy pathdefault.py
File metadata and controls
463 lines (350 loc) · 15.4 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Sublime for Kodi
Author: Tommie82
"""
import os
import re
import xbmc
import xbmcaddon
import xbmcgui
import xbmcvfs
__addon__ = xbmcaddon.Addon()
__addonid__ = __addon__.getAddonInfo('id')
__addonname__ = __addon__.getAddonInfo('name')
__addonversion__ = __addon__.getAddonInfo('version')
################
# PATHS
__cwd__ = xbmc.translatePath(__addon__.getAddonInfo('path')).decode("utf-8")
__resource__ = os.path.join(__cwd__, 'resources')
__data__ = os.path.join(__resource__, 'data')
supported = ['.srt']
################
# REGEX
#including start & end in different lines
paren = re.compile('(\((.+)?\)|\((.+)?|^(.+)?\))')
brace = re.compile('(\[(.+)?\]|\[(.+)?|^(.+)?\])')
music = re.compile('([♩♪♫♭♮♯]+(.+)?[♩♪♫♭♮♯]+|[♩♪♫♭♮♯]+(.+)?|^(.+)?[♩♪♫♭♮♯]+)')
# interpunction -> non alphanumeric leftovers
punct = re.compile('^[\W]+$')
# tags
font_tags = re.compile('(<font[^>]*>)|(<\/font>)')
# emptylist for blacklisted items
bl_reg = []
#specials
#starting with any word and colon
colon_prefix = re.compile('(^(<[A-z]+[^>]*>)*[=\*=\*=\*=[\s]*]*[A-z]+[\w\s]*:[^0-9{2}][\s]*)')
#starting with capitalized word and colon
colon_prefix_capped = re.compile('(^[<[A-z]+[^>]*>]*[=\*=\*=\*=[\s]*]*[A-Z]+[\w\s]*:[^0-9{2}][\s]*)')
#lines starting with a dash (-)
dash_start = re.compile('(^(<[A-z]+[^>]*>)*[\-]+[\s]*)')
start_tag = re.compile('(<[A-z]+[^>]*>)')
replace_tag = re.compile('=\*=\*=\*=[\s]*')
#global functions
def log(msg):
try:
message = '%s: %s' % (__addonname__, str(msg) )
except UnicodeEncodeError:
message = '%s: %s' % (__addonname__, str(msg.encode('utf-8', 'ignore')) )
print message
def notify(msg):
dialog = xbmcgui.Dialog()
dialog.notification(__addonname__, str(msg) , xbmcgui.NOTIFICATION_INFO, 5000)
def confirm(line1, line2="", line3=""):
dialog = xbmcgui.Dialog()
return dialog.yesno(__addonname__, str(line1), str(line2), str(line3))
# class start
class Sublime(xbmc.Player):
def __init__(self, *args, **kwargs):
xbmc.Player.__init__(self)
self.init_properties()
def getSetting(self, setting, boolean=False):
if boolean == True:
return __addon__.getSetting(setting) == 'true'
else:
return __addon__.getSetting(setting)
def init_properties(self):
self.debug = self.getSetting("debug",True)
self.__replace__ = "=*=*=*="
# general
if self.debug == True:
self.sublime_extension = ".sublime.debug"
self.keep_source = True
else:
self.sublime_extension = ".sublime.original"
self.keep_source = self.getSetting("keep_source",True)
self.show_notifications = self.getSetting("show_notifications",True)
self.auto_start = self.getSetting("auto_start",True)
self.current_only = self.getSetting("current_only",True)
# filter settings
self.flt_brace = self.getSetting("flt_brace",True)
self.flt_paren = self.getSetting("flt_paren",True)
self.flt_music = self.getSetting("flt_music",True)
self.flt_dash = self.getSetting("flt_dash_pr",True)
self.flt_colon_pr = self.getSetting("flt_colon_pr",True)
self.flt_colon_capped_pr = self.getSetting("flt_colon_capped_pr",True)
self.flt_font_tags = True
self.wait = False
self.externalAddons = []
if xbmc.getCondVisibility('System.HasAddon(service.autosubs)'):
self.externalAddons.append('autosubs')
if xbmc.getCondVisibility('System.HasAddon(script.service.checkpreviousepisode)'):
self.externalAddons.append('checkpreviousepisode')
# get blacklist file
f = xbmcvfs.File(os.path.join(__data__,'blacklist.txt'))
b = f.read()
f.close()
bl_lines = b.splitlines()
for bl in bl_lines:
bl_reg.append(re.compile('^[=\*=\*=\*=[\s]*]*'+bl, re.IGNORECASE))
# set supported languages
lang_file = os.path.join(__data__,'languages.txt')
if xbmcvfs.exists(lang_file):
log('Using languages from file')
f = xbmcvfs.File(lang_file)
b = f.read()
f.close()
self.langs = b.splitlines()
else:
log('Use default language list')
self.langs = ['de','en','es','fr','nl']
def cleanReplacedTags(self, line):
try:
line = re.sub(replace_tag,'', line)
except:
pass
return line
def cleanLine(self, line, regex):
try:
# is it inside a markup tag?
match = regex.match(line).group(1)
tag = start_tag.match(match).group(1)
line = re.sub(match, tag+self.__replace__, line)
except:
# just try a simple clean
try:
line = re.sub(regex, self.__replace__, line)
except:
pass
return line
def cleanBlacklisted(self, line):
for bl in bl_reg:
if bl.match(line.strip()):
line = re.sub(bl, self.__replace__, line)
return line
def getPercentage(self, num, total_files, current_count):
max_p = float(current_count)/total_files * 100
try:
offset = float(current_count-1)/total_files * 100
except:
offset = 0
percentage = offset + float(num)/100 * (max_p-offset)
return int(percentage)
def findSubs(self, path, filename):
count = 0
sub_files = []
log('Checking for subs in ' + path);
check_list =[]
fname_body = os.path.splitext(filename)[0]
check_list.append(fname_body)
# add country code to file
for l in self.langs:
check_list.append(fname_body +'.'+l)
# get all the files in the path
file_list = xbmcvfs.listdir(path)[1]
for f in file_list:
full_file_path = os.path.join(path,f)
checking = os.path.splitext(f)[0]
# current file in checklist or 'current file only' option disabled
if (self.current_only==True and checking in check_list ) or self.current_only==False:
# is this file a suppported subtitle
if os.path.splitext(f)[1] in supported:
#is there a debug file and debug mode is enabled?
if self.debug == True:
# is there a backup file?
debug_check = xbmcvfs.exists( full_file_path +'.sublime.debug')
else:
debug_check = False
backup_file = xbmcvfs.exists( full_file_path +'.sublime.original') #+ self.sublime_extension)
# ignore this file?
ignore_file = xbmcvfs.exists(full_file_path+'.sublime.ignore')
if not backup_file and not debug_check and not ignore_file:
count = count+1
sub_files.append( f )
log("Found unprocessed subtitle:" + str(full_file_path) )
return {'count': count, 'files':sub_files}
def clean(self, __original_file__,__path__, total_files=1, current_count=1):
progressDialog = xbmcgui.DialogProgress()
progressDialog.create(__addonname__, "Cleaning file "+ str(current_count)+" of "+str(total_files), __original_file__ )
# empty list for cleaned lined
cleanlines = []
progress = 0
nr = 0
# set full path for file
full_file_path = os.path.join(__path__,__original_file__)
# open file and load into buffer
f = xbmcvfs.File(full_file_path)
b = f.read()
f.close()
# split lines into list
data = b.splitlines()
# number of lines in the file
total_lines = len(data)
# loop through lines
for line in data:
if progressDialog.iscanceled():
log("Cancelled by user")
return False
# strip outer whitespace characters
line = line.strip()
nr=nr+1
dirty = True
if dirty == True:
if self.flt_paren == True:
line = self.cleanLine(line, paren)
if self.flt_brace == True:
line = self.cleanLine(line, brace)
if self.flt_dash == True:
line = self.cleanLine(line, dash_start)
if self.flt_music == True:
line = self.cleanLine(line, music)
if self.flt_colon_pr == True:
if self.flt_colon_capped_pr == True:
line = self.cleanLine(line, colon_prefix_capped)
else:
line = self.cleanLine(line, colon_prefix)
line = self.cleanLine(line, font_tags)
line = self.cleanBlacklisted(line)
# if the line is empty here, it means it was always empty, so just add it
if len(line)==0:
cleanlines.append(line+os.linesep)
# remove leftover particles (., etc)
line = self.cleanLine(line, punct)
# clean replacement tags if we 're not in debug mode
if self.debug == False:
line = self.cleanReplacedTags(line)
# add line if is not empty
if len(line)>0:
# add line to cleanlines list
cleanlines.append(line+os.linesep)
progress = progress+1
percentage = (float(progress)/total_lines)*90
progressDialog.update(self.getPercentage(percentage, total_files, current_count))
# time to write the new file
# remove the tempfile if it exists (from a failed rename maybe) otherwise the new content just gets append to it
__temp_file__ = os.path.join(__path__, 'cleaned.tmp');
if xbmcvfs.exists(__temp_file__):
log("temp file exists, removing")
xbmcvfs.delete(__temp_file__)
progressDialog.update(self.getPercentage(92, total_files, current_count), "Creating temp file...")
f = xbmcvfs.File(__temp_file__, 'w')
progressDialog.update(self.getPercentage(93, total_files, current_count), "Writing to temp file...")
# write clean list to temp file first just to be safe, and then close it
f.write(''.join(cleanlines))
f.close()
log('original exists?')
log(xbmcvfs.exists(full_file_path+self.sublime_extension))
if self.keep_source == True:
if self.debug == False and xbmcvfs.exists(full_file_path+'.sublime.debug') == True:
log('renaming .debug-file to '+ self.sublime_extension + '-file')
xbmcvfs.rename(full_file_path+'.sublime.debug',full_file_path+self.sublime_extension);
log("Removing original file")
xbmcvfs.delete(full_file_path)
elif xbmcvfs.exists(full_file_path+self.sublime_extension) == True:
# remove the original file
log("backup exists, skipping backup...")
log("removing original file")
xbmcvfs.delete(full_file_path)
else:
progressDialog.update(self.getPercentage(96, total_files, current_count), "Backing up original file...")
# rename the original file [filename].srt.bak
log("Renaming original file")
xbmcvfs.rename(full_file_path,full_file_path+self.sublime_extension);
else:
log('Not backing up file')
# remove the original file
log("Removing orginal file")
xbmcvfs.delete(full_file_path)
if xbmcvfs.exists(full_file_path+'.sublime.debug') == True:
log("Removing debug file")
xbmcvfs.delete(full_file_path+'.sublime.debug')
# open file and load into buffer
f = xbmcvfs.File(full_file_path+'.sublime.ignore','w')
f.close()
# rename tempfile to original filename
progressDialog.update(self.getPercentage(98, total_files, current_count), "Writing new file...")
xbmcvfs.rename(__temp_file__, full_file_path);
if current_count==total_files:
progressDialog.update(self.getPercentage(100, total_files, current_count), "Done")
xbmc.sleep(500)
return True
def onPlayBackResumed(self):
self.wait = False
def onPlayBackStopped(self):
self.wait = False
def onPlayBackEnded(self):
self.wait = False
def onPlayBackStarted(self):
self.init_properties()
if len(self.externalAddons) > 0:
log('Found conflicting addons:' + str(self.externalAddons))
xbmc.sleep(2500)
# addons like autosubs or check_previous_episode will pause the player
if xbmc.getCondVisibility('Player.Paused') == True:
log("Will wait for playback to continue")
self.wait = True
# sleep until playback resumes
while self.wait == True:
xbmc.sleep(500)
#get file being played
playing = xbmc.Player().getPlayingFile()
# timeout is needed for Pi
xbmc.sleep(1000)
path = os.path.split(playing)[0]
filename = os.path.split(playing)[1]
subsFound = self.findSubs(path, filename)
sub_file_count = subsFound['count']
if sub_file_count >0:
log("pausing playback")
#pause playback
if self.wait == False:
xbmc.Player().pause()
if self.auto_start == True:
confirmed = True
log("Auto clean is enabled")
else:
confirmed = confirm("%s Subtitles found " % (sub_file_count), "Do you want to clean them now?")
if confirmed == True:
files_to_process = sub_file_count
count = 0
for f in subsFound['files']:
count = count+1
if os.path.splitext(f)[1] in supported:
log("start cleaning file %s of %s \n: %s" % ( count, sub_file_count, f ) )
cleaning = self.clean(f, path, files_to_process, count)
sub_file_count = sub_file_count - count
if cleaning == False:
xbmc.Player().pause()
log("User cancelled during cleaning of files")
return
# stop & start player to refresh the subtitle stream
log("Resuming " + str(playing))
full_file_path = os.path.join(path,f)
self.disableSubtitles()
self.setSubtitles(full_file_path)
# jump back to start if not passed 10 secs
if xbmc.Player().getTime() < 10:
xbmc.Player().seekTime(0)
xbmc.Player().pause()
else:
log("User denied cleaning of files")
if self.wait == False:
xbmc.Player().pause()
else:
log("Nothing to do")
monitor = Sublime()
log("Loading '%s' version '%s'" % (__addonname__, __addonversion__))
while not xbmc.abortRequested:
xbmc.sleep(1000)
del monitor