forked from allejok96/jw-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwb-offline-import
More file actions
executable file
·76 lines (58 loc) · 2.22 KB
/
jwb-offline-import
File metadata and controls
executable file
·76 lines (58 loc) · 2.22 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
#!/usr/bin/env python3
import os
import shutil
from sys import stderr
from jwlib.arguments import ArgumentParser, Settings
from jwlib.download import disk_usage_info, disk_cleanup
class File:
def __init__(self, name: str, directory: str):
self.name = name
self.file = os.path.join(directory, name)
try:
stat = os.stat(self.file)
self.size = stat.st_size
self.date = stat.st_mtime
except FileNotFoundError:
self.size = None
self.date = None
class LocalSettings(Settings):
percent = False
source = None
dest = None
parser = ArgumentParser(prog='jwb-offline-import',
usage='%(prog)s SOURCE DEST ',
description='Copy new videos from SOURCE to DEST')
parser.add_arguments(['--free', '--no-warning', '--quiet'])
parser.add_argument('--percent', action='store_true', help='output progress in percent to stdout (for piping)')
parser.add_argument('source', metavar='SOURCE', help='source directory')
parser.add_argument('dest', metavar='DEST', help='destination directory')
settings = LocalSettings()
parser.parse_args(namespace=settings)
if settings.keep_free > 0:
disk_usage_info(settings)
# Create a list of all mp4 files to be copied
source_files = []
for name in os.listdir(settings.source):
if not name.lower().endswith('.mp4'):
continue
source = File(name, directory=settings.source)
dest = File(name, directory=settings.dest)
if source.size != dest.size:
source_files.append(source)
# Newest file first
source_files.sort(key=lambda x: x.date, reverse=True)
os.makedirs(settings.dest, exist_ok=True)
total = len(source_files)
for source in source_files:
i = source_files.index(source)
if settings.keep_free > 0:
disk_cleanup(settings, directory=settings.dest, reference_media=source)
if settings.percent:
print(i * 100 // total, flush=True)
if settings.quiet < 1:
print('copying [{}/{}]: {}'.format(i + 1, total, source.name), file=stderr, flush=True)
shutil.copy2(source.file, os.path.join(settings.dest, source.name))
if settings.percent:
print(100, flush=True)
if settings.quiet < 1:
print('done', file=stderr)