-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathargs.py
129 lines (108 loc) · 3.98 KB
/
args.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import argparse
from enum import Enum, auto
from pathlib import Path
from typing import NamedTuple
class VerboseLevel(Enum):
NONE = auto()
PROGRAM = auto()
PROGRAM_AND_LIBRARIES = auto()
class MediaToHandle(Enum):
"""Arg to select what media to convert."""
ALL = auto()
IMAGE = auto()
VIDEO = auto()
class Args(NamedTuple):
"""A class to specifically name the arguments."""
memories_history: Path
memories_folder: Path
output_folder: Path
verbose: VerboseLevel
type_handled: MediaToHandle
run_async: bool
def parse_args() -> Args:
"""Parse arguments from the command line and return an Args object."""
default_memories_history = Path("./input/memories_history.json")
default_memories_folder = Path("./input/memories")
default_output_folder = Path("./output")
parser = argparse.ArgumentParser(
description="Adds metadata (captions and timestamps) to your exported Snapchat "
"memories."
)
parser.add_argument(
"--memories-history",
help=f"Where the memories_history.json file is. Defaults to {default_memories_history}",
default=str(default_memories_history),
)
parser.add_argument(
"--memories-folder",
help=f"Where the memories folder is. Defaults to {default_memories_folder}",
default=str(default_memories_folder),
)
parser.add_argument(
"--output",
help=f"A folder for the converted memories to go. Defaults to {default_output_folder}",
default=str(default_output_folder),
)
parser.add_argument(
"--verbose",
"-v",
action="count",
default=0,
help="Output what the script is doing to help debug any issues. -v will get logs from just this application, -vv will also get logs from ffmpeg and vips libraries",
)
type_handled_group = parser.add_mutually_exclusive_group()
type_handled_group.add_argument(
"--image-only", help="Only process images", action="store_true"
)
type_handled_group.add_argument(
"--video-only", help="Only process videos", action="store_true"
)
parser.add_argument(
"--video-async",
help="[EXPERIMENTAL] Convert several videos at once for a faster overall conversion.",
action="store_true",
)
args_raw = parser.parse_args()
memories_history = Path(args_raw.memories_history)
if not memories_history.is_file():
raise Exception(
f"{memories_history} is set as memories_history.json, but it isn't a file! Please place it in the "
f"default location ({default_memories_history}) or specify the "
"path with the --memories-history flag."
)
memories_folder = Path(args_raw.memories_folder)
if not memories_folder.is_dir():
raise Exception(
f"{memories_folder} is set as the memories folder, but it isn't a folder! Please place the memories "
f"folder in the default location ({default_memories_folder}) or "
"specify the path with the --memories-folder flag."
)
output_folder = Path(args_raw.output)
if output_folder.exists():
raise Exception(
f"{output_folder} already exists, but is set to be the output "
"folder! Stopping the script to not delete any exported photos. "
"Please delete this directory if you intend to use it as the "
"output folder."
)
match args_raw.verbose:
case 0:
verbose = VerboseLevel.NONE
case 1:
verbose = VerboseLevel.PROGRAM
case _:
verbose = VerboseLevel.PROGRAM_AND_LIBRARIES
if args_raw.image_only:
type_handled = MediaToHandle.IMAGE
elif args_raw.video_only:
type_handled = MediaToHandle.VIDEO
else:
type_handled = MediaToHandle.ALL
return Args(
memories_history,
memories_folder,
output_folder,
verbose,
type_handled,
args_raw.video_async,
)