-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir_size.py
57 lines (45 loc) · 1.61 KB
/
dir_size.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
from pathlib import Path
from loguru import logger
from flexget import plugin
from flexget.event import event
plugin_name = 'dir_size'
logger = logger.bind(name=plugin_name)
class PluginDirSize(object):
"""
Set the dir_size about directory
when the entries come from the filesystem input. Value in bytes.
Example::
check_dir_size:
filesystem:
path:
- D:\Media\Incoming\series
recursive: yes
retrieve: dirs
dir_size: yes
if:
- dir_size == 0: accept
"""
schema = {'type': 'boolean'}
def on_task_metainfo(self, task, config):
# check if disabled (value set to false)
if not config:
# Config was set to 'no' instead of yes. Don't do anything then.
return
for entry in task.entries:
filename = entry.get('filename')
location = entry.get('location')
path_loc = Path(location)
# If there is no 'filename' field there is also no directory
if filename is None or location is None:
logger.warning(
"Entry {} didn't come from the filesystem plugin", entry.get('title')
)
continue
else:
if not path_loc.is_dir():
continue
# populate with size
entry['dir_size'] = int(sum(f.stat().st_size for f in path_loc.glob('**/*') if f.is_file()))
@event('plugin.register')
def register_plugin():
plugin.register(PluginDirSize, plugin_name, api_ver=2)