forked from Anthirian/script.service.janitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.py
More file actions
754 lines (651 loc) · 34.9 KB
/
Copy pathdefault.py
File metadata and controls
754 lines (651 loc) · 34.9 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import json
from ctypes import *
import xbmc
import xbmcaddon
import xbmcgui
import xbmcvfs
from settings import *
from utils import *
# Addon info
__addonID__ = "script.filecleaner"
__addon__ = Addon(__addonID__)
__title__ = xbmc.translatePath(__addon__.getAddonInfo("name")).decode("utf-8")
__author__ = "Anthirian, drewzh"
__icon__ = xbmc.translatePath(__addon__.getAddonInfo("icon")).decode("utf-8")
class Cleaner(object):
"""
The Cleaner class allows users to clean up their movie, TV show and music video collection by removing watched
items. The user can apply a number of conditions to cleaning, such as limiting cleaning to files with a given
rating, excluding a particular folder or only cleaning when a particular disk is low on disk space.
The main method to call is the ``cleanup()`` method. This method will invoke the subsequent checks and (re)move
your videos. Upon completion, you will receive a short summary of the cleaning results.
*Example*
``summary = Cleaner().cleanup()``
"""
# Constants to ensure correct (Frodo-compatible) JSON-RPC requests for XBMC
MOVIES = "movies"
MUSIC_VIDEOS = "musicvideos"
TVSHOWS = "episodes"
CLEANING_TYPE_MOVE = "0"
CLEANING_TYPE_DELETE = "1"
movie_filter_fields = ["title", "plot", "plotoutline", "tagline", "votes", "rating", "time", "writers",
"playcount", "lastplayed", "inprogress", "genre", "country", "year", "director",
"actor", "mpaarating", "top250", "studio", "hastrailer", "filename", "path", "set",
"tag", "dateadded", "videoresolution", "audiochannels", "videocodec", "audiocodec",
"audiolanguage", "subtitlelanguage", "videoaspect", "playlist"]
episode_filter_fields = ["title", "tvshow", "plot", "votes", "rating", "time", "writers", "airdate",
"playcount", "lastplayed", "inprogress", "genre", "year", "director", "actor",
"episode", "season", "filename", "path", "studio", "mpaarating", "dateadded",
"videoresolution", "audiochannels", "videocodec", "audiocodec", "audiolanguage",
"subtitlelanguage", "videoaspect", "playlist"]
musicvideo_filter_fields = ["title", "genre", "album", "year", "artist", "filename", "path", "playcount",
"lastplayed", "time", "director", "studio", "plot", "dateadded", "videoresolution",
"audiochannels", "videocodec", "audiocodec", "audiolanguage", "subtitlelanguage",
"videoaspect", "playlist"]
supported_filter_fields = {
TVSHOWS: episode_filter_fields,
MOVIES: movie_filter_fields,
MUSIC_VIDEOS: musicvideo_filter_fields
}
methods = {
TVSHOWS: "VideoLibrary.GetEpisodes",
MOVIES: "VideoLibrary.GetMovies",
MUSIC_VIDEOS: "VideoLibrary.GetMusicVideos"
}
properties = {
TVSHOWS: ["file", "showtitle", "season"],
MOVIES: ["file", "title", "year"],
MUSIC_VIDEOS: ["file", "artist"]
}
stacking_indicators = ["part", "pt", "cd", "dvd", "disk", "disc"]
def __init__(self):
debug("%s version %s loaded." % (__addon__.getAddonInfo("name").decode("utf-8"),
__addon__.getAddonInfo("version").decode("utf-8")))
def cleanup(self):
"""
Delete watched videos from the XBMC video database.
The videos to be deleted are subject to a number of criteria as can be specified in the addon's settings.
:rtype: str
:return: A comma separated summary of the cleaning results.
"""
debug("Starting cleaning routine.")
return_code = 0
if get_setting(clean_when_idle) and xbmc.Player().isPlayingVideo():
debug("A video is currently playing. Skipping cleaning.", xbmc.LOGWARNING)
return None, return_code
if not get_setting(clean_when_low_disk_space) or (get_setting(clean_when_low_disk_space)
and self.disk_space_low()):
summary = dict()
cleaned_files = []
if get_setting(clean_movies):
movies = self.get_expired_videos(self.MOVIES)
if movies:
count = 0
for abs_path, title, year in movies:
unstacked_path = self.unstack(abs_path)
if xbmcvfs.exists(unstacked_path[0]):
if get_setting(cleaning_type) == self.CLEANING_TYPE_MOVE:
if get_setting(holding_folder) == "":
# No destination set, prompt user to set one now
if xbmcgui.Dialog().yesno(__title__, translate(32521), translate(32522),
translate(32523)):
xbmc.executebuiltin("Addon.OpenSettings(%s)" % __addonID__)
# Set return code to 1 to indicate failure
return_code = 1
return None, return_code
if get_setting(create_subdirs):
new_path = os.path.join(get_setting(holding_folder), "%s (%d)" % (title, year))
else:
new_path = get_setting(holding_folder)
if self.move_file(abs_path, new_path):
count += 1
if len(unstacked_path) > 1:
cleaned_files.extend(unstacked_path)
else:
cleaned_files.append(abs_path)
self.clean_related_files(abs_path, new_path)
self.delete_empty_folders(abs_path)
elif get_setting(cleaning_type) == self.CLEANING_TYPE_DELETE:
if self.delete_file(abs_path):
count += 1
if len(unstacked_path) > 1:
cleaned_files.extend(unstacked_path)
else:
cleaned_files.append(abs_path)
self.clean_related_files(abs_path)
self.delete_empty_folders(abs_path)
else:
debug("Unable to figure out what to do with expired videos. Aborting.", xbmc.LOGERROR)
return_code = 1
return None, return_code
else:
debug("XBMC could not find %r" % abs_path, xbmc.LOGWARNING)
if count > 0:
summary[self.MOVIES] = count
if get_setting(clean_tv_shows):
episodes = self.get_expired_videos(self.TVSHOWS)
if episodes:
count = 0
for abs_path, show_name, season_number in episodes:
if xbmcvfs.exists(abs_path):
if get_setting(cleaning_type) == self.CLEANING_TYPE_MOVE:
if get_setting(holding_folder) == "":
# No destination set, prompt user to set one now
if xbmcgui.Dialog().yesno(__title__, translate(32521), translate(32522),
translate(32523)):
xbmc.executebuiltin("Addon.OpenSettings(%s)" % __addonID__)
# Set return code to 1 to indicate failure
return_code = 1
return None, return_code
if get_setting(create_subdirs):
new_path = os.path.join(get_setting(holding_folder), show_name,
"Season %d" % season_number)
else:
new_path = get_setting(holding_folder)
if self.move_file(abs_path, new_path):
count += 1
cleaned_files.append(abs_path)
self.clean_related_files(abs_path, new_path)
self.delete_empty_folders(abs_path)
elif get_setting(cleaning_type) == self.CLEANING_TYPE_DELETE:
if self.delete_file(abs_path):
count += 1
cleaned_files.append(abs_path)
self.clean_related_files(abs_path)
self.delete_empty_folders(abs_path)
else:
debug("Unable to figure out what to do with expired videos. Aborting.", xbmc.LOGERROR)
return_code = 1
return None, return_code
else:
debug("XBMC could not find %r" % abs_path, xbmc.LOGWARNING)
if count > 0:
summary[self.TVSHOWS] = count
if get_setting(clean_music_videos):
musicvideos = self.get_expired_videos(self.MUSIC_VIDEOS)
if musicvideos:
count = 0
for abs_path, artists in musicvideos:
if xbmcvfs.exists(abs_path):
if get_setting(cleaning_type) == self.CLEANING_TYPE_MOVE:
if get_setting(holding_folder) == "":
# No destination set, prompt user to set one now
if xbmcgui.Dialog().yesno(__title__, translate(32521), translate(32522),
translate(32523)):
xbmc.executebuiltin("Addon.OpenSettings(%s)" % __addonID__)
# Set return code to 1 to indicate failure
return_code = 1
return None, return_code
if get_setting(create_subdirs):
artist = ", ".join(str(a) for a in artists)
new_path = os.path.join(get_setting(holding_folder), artist)
else:
new_path = get_setting(holding_folder)
if self.move_file(abs_path, new_path):
count += 1
cleaned_files.append(abs_path)
self.clean_related_files(abs_path, new_path)
self.delete_empty_folders(abs_path)
elif get_setting(cleaning_type) == self.CLEANING_TYPE_DELETE:
if self.delete_file(abs_path):
count += 1
cleaned_files.append(abs_path)
self.clean_related_files(abs_path)
self.delete_empty_folders(abs_path)
else:
debug("Unable to figure out what to do with expired videos. Aborting.", xbmc.LOGERROR)
return_code = 1
return None, return_code
else:
debug("XBMC could not find %r" % abs_path, xbmc.LOGWARNING)
if count > 0:
summary[self.MUSIC_VIDEOS] = count
# Give a status report if any deletes occurred
if cleaned_files:
Log().prepend(cleaned_files)
# Finally clean the library to account for any deleted videos.
if get_setting(clean_xbmc_library) and cleaned_files:
xbmc.sleep(5000) # Sleep 5 seconds until file I/O is done.
if xbmc.getCondVisibility("Library.IsScanningVideo"):
debug("The video library is being updated. Skipping library cleanup.", xbmc.LOGWARNING)
else:
xbmc.executebuiltin("XBMC.CleanLibrary(video)")
return self.summarize(summary), return_code
def summarize(self, details):
"""
Create a summary from the cleaning results.
:type details: dict
:rtype: str
:return: A comma separated summary of the cleaning results.
"""
summary = ""
# Localize video types
for vid_type, amount in details.items():
if vid_type is self.MOVIES:
video_type = utils.translate(32515)
elif vid_type is self.TVSHOWS:
video_type = utils.translate(32516)
elif vid_type is self.MUSIC_VIDEOS:
video_type = utils.translate(32517)
else:
video_type = ""
summary += "%d %s, " % (amount, video_type)
# strip the comma and space from the last iteration and add the localized suffix
if summary:
return "%s%s" % (summary.rstrip(", "), utils.translate(32518))
else:
return ""
def get_expired_videos(self, option):
"""
Find videos in the XBMC library that have been watched.
Respects any other conditions user enables in the addon's settings.
:type option: str
:param option: The type of videos to find (one of the globals MOVIES, MUSIC_VIDEOS or TVSHOWS).
:rtype: list
:return: A list of expired videos, along with a number of extra attributes specific to the video type.
"""
# A non-exhaustive list of pre-defined filters to use during JSON-RPC requests
# These are possible conditions that must be met before a video can be deleted
by_playcount = {"field": "playcount", "operator": "greaterthan", "value": "0"}
by_date_played = {"field": "lastplayed", "operator": "notinthelast", "value": "%d" % get_setting(expire_after)}
# TODO: add GUI setting for date_added
by_date_added = {"field": "dateadded", "operator": "notinthelast", "value": "7"}
by_minimum_rating = {"field": "rating", "operator": "lessthan", "value": "%d" % get_setting(minimum_rating)}
by_no_rating = {"field": "rating", "operator": "isnot", "value": "0"}
# TODO: Don't hard code 'Muse' as artist
by_artist = {"field": "artist", "operator": "contains", "value": "Muse"}
by_progress = {"field": "inprogress", "operator": "false", "value": ""}
# link settings and filters together
settings_and_filters = [
(get_setting(enable_expiration), by_date_played),
(get_setting(clean_when_low_rated), by_minimum_rating),
(get_setting(not_in_progress), by_progress)
]
# Only check not rated videos if checking for video ratings at all
if get_setting(clean_when_low_rated):
settings_and_filters.append((get_setting(ignore_no_rating), by_no_rating))
enabled_filters = [by_playcount]
for s, f in settings_and_filters:
if s and f["field"] in self.supported_filter_fields[option]:
enabled_filters.append(f)
debug("[%s] Filters enabled: %r" % (self.methods[option], enabled_filters))
filters = {"and": enabled_filters}
request = {
"jsonrpc": "2.0",
"method": self.methods[option],
"params": {
"properties": self.properties[option],
"filter": filters
},
"id": 1
}
rpc_cmd = json.dumps(request)
response = xbmc.executeJSONRPC(rpc_cmd)
debug("[%s] Response: %r" % (self.methods[option], response))
result = json.loads(response)
try:
error = result["error"]
debug("An error occurred. %r" % error)
return None
except KeyError as ke:
if "error" in ke:
pass # no error
else:
raise
debug("Building list of expired videos")
expired_videos = []
response = result["result"]
try:
debug("Found %d watched %s matching your conditions" % (response["limits"]["total"], option))
debug("JSON Response: " + str(response))
for video in response[option]:
# Gather all properties and add it to this video's information
temp = []
for p in self.properties[option]:
temp.append(video[p])
expired_videos.append(temp)
except KeyError as ke:
if option in ke:
pass # no expired videos found
else:
debug("KeyError: %r not found" % ke, xbmc.LOGWARNING)
debug("%r" % response, xbmc.LOGWARNING)
raise
finally:
debug("Expired videos: " + str(expired_videos))
return expired_videos
def is_excluded(self, full_path):
"""Check if the file path is part of the excluded sources.
:type full_path: str
:param full_path: the path to the file that should be checked for exclusion
:rtype: bool
:return: True if the path matches a user-set excluded path, False otherwise.
"""
if not get_setting(exclusion_enabled):
debug("Path exclusion is disabled.")
return False
if not full_path:
debug("File path is empty and cannot be checked for exclusions")
return False
exclusions = [get_setting(exclusion1), get_setting(exclusion2), get_setting(exclusion3)]
if r"://" in full_path:
debug("Detected a network path")
pattern = re.compile("(?:smb|afp|nfs)://(?:(?:.+):(?:.+)@)?(?P<tail>.*)$", flags=re.U | re.I)
debug("Converting excluded network paths for easier comparison")
normalized_exclusions = []
for ex in exclusions:
# Strip everything but the folder structure
try:
if ex and r"://" in ex:
# Only normalize non-empty excluded paths
normalized_exclusions.append(pattern.match(ex).group("tail").lower())
except (AttributeError, IndexError, KeyError) as err:
debug("Could not parse the excluded network path %r\n%s" % (ex, err), xbmc.LOGWARNING)
return True
debug("Conversion result: %r" % normalized_exclusions)
debug("Proceeding to match a file with the exclusion paths")
debug("The file to match is %r" % full_path)
result = pattern.match(full_path)
try:
debug("Converting file path for easier comparison.")
converted_path = result.group("tail").lower()
debug("Result: %r" % converted_path)
for ex in normalized_exclusions:
debug("Checking against exclusion %r." % ex)
if converted_path.startswith(ex):
debug("File %r matches excluded path %r." % (converted_path, ex))
return True
debug("No match was found with an excluded path.")
return False
except (AttributeError, IndexError, KeyError) as err:
debug("Error converting %r. No files will be deleted.\n%s" % (full_path, err), xbmc.LOGWARNING)
return True
else:
debug("Detected a local path")
for ex in exclusions:
if ex and full_path.startswith(ex):
debug("File %r matches excluded path %r." % (full_path, ex))
return True
debug("No match was found with an excluded path.")
return False
def get_free_disk_space(self, path):
"""Determine the percentage of free disk space.
:type path: str
:param path: The path to the drive to check. This can be any path of any depth on the desired drive.
:rtype: float
:return: The percentage of free space on the disk; 100% if errors occur.
"""
percentage = float(100)
debug("Checking for disk space on path: %r" % path)
dirs, files = xbmcvfs.listdir(path)
if dirs or files: # Workaround for xbmcvfs.exists("C:\")
if xbmc.getCondVisibility("System.Platform.Windows"):
debug("We are checking disk space from a Windows file system")
debug("The path to check is %r" % path)
if r"://" in path:
debug("We are dealing with network paths")
debug("Extracting information from share %r" % path)
regex = "(?P<type>smb|nfs|afp)://(?:(?P<user>.+):(?P<pass>.+)@)?(?P<host>.+?)/(?P<share>.+?).*$"
pattern = re.compile(regex, flags=re.I | re.U)
match = pattern.match(path)
try:
share = match.groupdict()
debug("Protocol: %r, User: %r, Password: %r, Host: %r, Share: %r" %
(share["type"], share["user"], share["pass"], share["host"], share["share"]))
except AttributeError as ae:
debug("%r\nCould not extract required data from %r" % (ae, path), xbmc.LOGERROR)
return percentage
debug("Creating UNC paths so Windows understands the shares")
path = os.path.normcase(r"\\" + share["host"] + os.sep + share["share"])
debug("UNC path: %r" % path)
debug("If checks fail because you need credentials, please mount the share first")
else:
debug("We are dealing with local paths")
if not isinstance(path, unicode):
debug("Converting path to unicode for disk space checks")
path = path.decode("mbcs")
debug("New path: %r" % path)
bytes_total = c_ulonglong(0)
bytes_free = c_ulonglong(0)
windll.kernel32.GetDiskFreeSpaceExW(c_wchar_p(path), byref(bytes_free), byref(bytes_total), None)
try:
percentage = float(bytes_free.value) / float(bytes_total.value) * 100
debug("Hard disk check results:")
debug("Bytes free: %s" % bytes_free.value)
debug("Bytes total: %s" % bytes_total.value)
except ZeroDivisionError:
notify(translate(32511), 15000, level=xbmc.LOGERROR)
else:
debug("We are checking disk space from a non-Windows file system")
debug("Stripping %r of all redundant stuff." % path)
path = os.path.normpath(path)
debug("The path now is " + path)
try:
diskstats = os.statvfs(path)
percentage = float(diskstats.f_bfree) / float(diskstats.f_blocks) * 100
debug("Hard disk check results:")
debug("Bytes free: %r" % diskstats.f_bfree)
debug("Bytes total: %r" % diskstats.f_blocks)
except OSError as ose:
notify(translate(32512), 15000, level=xbmc.LOGERROR)
debug("Error accessing %r: %r" % (path, ose))
except ZeroDivisionError:
notify(translate(32511), 15000, level=xbmc.LOGERROR)
else:
notify(translate(32513), 15000, level=xbmc.LOGERROR)
debug("Free space: %0.2f%%" % percentage)
return percentage
def disk_space_low(self):
"""Check whether the disk is running low on free space.
:rtype: bool
:return: True if disk space is below threshold (set through addon settings), False otherwise.
"""
return self.get_free_disk_space(get_setting(disk_space_check_path)) <= get_setting(disk_space_threshold)
def unstack(self, path):
"""Unstack path if it is a stacked movie. See http://wiki.xbmc.org/index.php?title=File_stacking for more info.
:type path: str
:param path: The path that should be unstacked.
:rtype: list
:return: A list of paths that are part of the stack. If it is no stacked movie, a one-element list is returned.
"""
if path.startswith("stack://"):
debug("Unstacking %r." % path)
return path.replace("stack://", "").split(" , ")
else:
debug("Unstacking %r is not needed." % path)
return [path]
def get_stack_bare_title(self, filenames):
"""Find the common title of files part of a stack, minus the volume and file extension.
Example:
["Movie_Title_part1.ext", "Movie_Title_part2.ext"] yields "Movie_Title"
:type filenames: list
:param filenames: a list of file names that are part of a stack. Use unstack() to find these file names.
:rtype: str
:return: common title of file names part of a stack
"""
title = os.path.basename(os.path.commonprefix(filenames))
for e in self.stacking_indicators:
if title.endswith(e):
title = title[:-len(e)].rstrip("._-")
break
return title
def delete_file(self, location):
"""
Delete a file from the file system. Also supports stacked movie files.
Example:
success = delete_file(location)
:type location: str
:param location: the path to the file you wish to delete.
:rtype: bool
:return: True if (at least one) file was deleted successfully, False otherwise.
"""
debug("Attempting to delete %r" % location)
paths = self.unstack(location)
success = []
if self.is_excluded(paths[0]):
debug("Detected a file on an excluded path. Aborting.")
return False
for p in paths:
if xbmcvfs.exists(p):
success.append(bool(xbmcvfs.delete(p)))
else:
debug("File %r no longer exists." % p, xbmc.LOGERROR)
success.append(False)
debug("Return statuses: %r" % success)
return any(success)
def delete_empty_folders(self, location):
"""
Delete the folder if it is empty. Presence of custom file extensions can be ignored while scanning.
To achieve this, edit the ignored file types setting in the addon settings.
Example:
success = delete_empty_folders(path)
:type location: str
:param location: The path to the folder to be deleted.
:rtype: bool
:return: True if the folder was deleted successfully, False otherwise.
"""
if not get_setting(delete_folders):
debug("Deleting of folders is disabled.")
return False
folder = os.path.dirname(self.unstack(location)[0]) # Stacked paths should have the same parent, use any
debug("Checking if %r is empty" % folder)
ignored_file_types = [file_ext.strip() for file_ext in get_setting(ignore_extensions).split(",")]
debug("Ignoring file types %r" % ignored_file_types)
subfolders, files = xbmcvfs.listdir(folder)
debug("Contents of %r:\nSubfolders: %r\nFiles: %r" % (folder, subfolders, files))
empty = True
try:
for f in files:
_, ext = os.path.splitext(f)
debug("File extension: " + ext)
if ext not in ignored_file_types:
debug("Found non-ignored file type %r" % ext)
empty = False
break
except OSError as oe:
debug("Error deriving file extension. Errno " + str(oe.errno), xbmc.LOGERROR)
empty = False
# Only delete directories if we found them to be empty (containing no files or filetypes we ignored)
if empty:
debug("Directory is empty and will be removed")
try:
# Recursively delete any subfolders
for f in subfolders:
debug("Deleting file at " + str(os.path.join(folder, f)))
self.delete_empty_folders(os.path.join(folder, f))
# Delete any files in the current folder
for f in files:
debug("Deleting file at " + str(os.path.join(folder, f)))
xbmcvfs.delete(os.path.join(folder, f))
# Finally delete the current folder
return xbmcvfs.rmdir(folder)
except OSError as oe:
debug("An exception occurred while deleting folders. Errno " + str(oe.errno), xbmc.LOGERROR)
return False
else:
debug("Directory is not empty and will not be removed")
return False
def clean_related_files(self, source, dest_folder=None):
"""Clean files related to another file based on the user's preferences.
Related files are files that only differ by extension, or that share a prefix in case of stacked movies.
Examples of related files include NFO files, thumbnails, subtitles, fanart, etc.
:type source: str
:param source: Location of the file whose related files should be cleaned.
:type dest_folder: str
:param dest_folder: (Optional) The folder where related files should be moved to. Not needed when deleting.
"""
if settings.get_setting(clean_related):
debug("Cleaning related files.")
path_list = self.unstack(source)
path, name = os.path.split(path_list[0]) # Because stacked movies are in the same folder, only check one
if source.startswith("stack://"):
name = self.get_stack_bare_title(path_list)
else:
name, ext = os.path.splitext(name)
debug("Attempting to match related files in %r with prefix %r" % (path, name))
for extra_file in xbmcvfs.listdir(path)[1]:
if isinstance(extra_file, unicode):
# TODO: Possible fix for UnicodeDecodeError: ordinal not in range(128). Needs testing.
# TODO: Not sure if extra_file causes the issue or name does.
extra_file = extra_file.encode("utf-8")
if extra_file.startswith(name):
debug("%r starts with %r." % (extra_file, name))
extra_file_path = os.path.join(path, extra_file)
if settings.get_setting(delete_files):
if extra_file_path not in path_list:
debug("Deleting %r." % extra_file_path)
xbmcvfs.delete(extra_file_path)
else:
new_extra_path = os.path.join(dest_folder, os.path.basename(extra_file))
if new_extra_path not in path_list:
debug("Moving %r to %r." % (extra_file_path, new_extra_path))
xbmcvfs.rename(extra_file_path, new_extra_path)
debug("Finished searching for related files.")
else:
debug("Cleaning of related files is disabled.")
def move_file(self, source, dest_folder):
"""Move a file to a new destination. Will create destination if it does not exist.
Example:
success = move_file(a, b)
:type source: str # TODO: Check p.
:param source: the source path (absolute)
:type dest_folder: str
:param dest_folder: the destination path (absolute)
:rtype: bool
:return: True if (at least one) file was moved successfully, False otherwise.
"""
if isinstance(source, unicode):
source = source.encode("utf-8")
paths = self.unstack(source)
success = []
dest_folder = xbmc.makeLegalFilename(dest_folder)
if self.is_excluded(paths[0]):
debug("Detected a file on an excluded path. Aborting.")
return False
for p in paths:
debug("Attempting to move %r to %r." % (p, dest_folder))
if xbmcvfs.exists(p):
if not xbmcvfs.exists(dest_folder):
if xbmcvfs.mkdirs(dest_folder):
debug("Created destination %r." % dest_folder)
else:
debug("Destination %r could not be created." % dest_folder, xbmc.LOGERROR)
return False
new_path = os.path.join(dest_folder, os.path.basename(p))
if xbmcvfs.exists(new_path):
debug("A file with the same name already exists in the holding folder. Checking file sizes.")
existing_file = xbmcvfs.File(new_path)
file_to_move = xbmcvfs.File(p)
if file_to_move.size() > existing_file.size():
debug("This file is larger than the existing file. Replacing it with this one.")
existing_file.close()
file_to_move.close()
success.append(bool(xbmcvfs.delete(new_path) and xbmcvfs.rename(p, new_path)))
else:
debug("This file isn't larger than the existing file. Deleting it instead of moving.")
existing_file.close()
file_to_move.close()
success.append(bool(xbmcvfs.delete(p)))
else:
debug("Moving %r to %r." % (p, new_path))
success.append(bool(xbmcvfs.rename(p, new_path)))
else:
debug("File %r no longer exists." % p, xbmc.LOGWARNING)
success.append(False)
return any(success)
if __name__ == "__main__":
cleaner = Cleaner()
results, exit_status = cleaner.cleanup()
if results:
# Videos were cleaned. Ask the user to view the log file.
if xbmcgui.Dialog().yesno(utils.translate(32514), results, utils.translate(32519)):
xbmc.executescript("special://home/addons/script.filecleaner/viewer.py")
elif exit_status == 0:
# Everything went well, but no videos needed cleaning. Show a notification.
notify(utils.translate(32520))
else:
debug("Cleaning finished with exit status %d" % exit_status, xbmc.LOGERROR)