-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-missing-keys-info.py
164 lines (137 loc) · 4.09 KB
/
fix-missing-keys-info.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
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
"""
This script will check / repair metadata-files (info.json) that may have missing keys or misspelled keys.
Intended usage is to verify metadata before importing.
Usage:
python fix-missing-keys-info.py <input_dir>
input_dir: The directory containing the videos and their associated files.
"""
import os
import sys
import re
import json
from dotenv import load_dotenv
load_dotenv()
EXT_MAP = {
"media": [".mkv", ".webm", ".mp4"],
"metadata": [".info.json"],
"thumb": [".jpg", ".png", ".webp"],
"subtitle": [".vtt"],
"description": [".description"],
}
POSSIBLE_MISSING_KEYS = [
{
"key": "tags",
"default": [],
},
{
"key": "categories",
"default": [],
},
{
"key": "thumbnails",
"default": [],
},
{
"key": "description",
"default": "",
},
{
"key": "view_count",
"default": 0,
},
{
"key": "upload_date",
"default": "",
},
{
"key": "uploader",
"default": "",
},
{
"key": "uploader_id",
"default": "",
},
{
"key": "channel_id",
"default": "",
},
{
"key": "title",
"default": "",
},
]
CORRECT_NAMED_KEYS = [
{
"misspelled": ["channelid"],
"correct": "channel_id",
},
{
"misspelled": ["uploaddate"],
"correct": "upload_date",
},
{
"misspelled": ["viewdate"],
"correct": "view_date",
},
]
def categorize_files(directory):
"""Categorize files based on EXT_MAP and return the desired dictionaries."""
grouped_files = {}
for root, _, files in os.walk(directory):
for file in files:
full_path = os.path.join(root, file)
video_id = extract_video_id(file)
if video_id:
if video_id not in grouped_files:
grouped_files[video_id] = {
"media": False,
"metadata": False,
"thumb": False,
"subtitle": False,
"video_id": video_id,
}
for category, extensions in EXT_MAP.items():
if any(file.endswith(ext) for ext in extensions):
if category == "subtitle":
if grouped_files[video_id]["subtitle"] is False:
grouped_files[video_id]["subtitle"] = []
grouped_files[video_id]["subtitle"].append(full_path)
else:
grouped_files[video_id][category] = full_path
return list(grouped_files.values())
def extract_video_id(filename):
"""Extracts video ID from the filename which is enclosed in square brackets."""
base_name, _ = os.path.splitext(filename)
id_search = re.search(r"\[([a-zA-Z0-9_-]{11})\]", base_name)
if id_search:
youtube_id = id_search.group(1)
return youtube_id
return None
def main(folder=None):
filename = os.path.basename(__file__)
if len(sys.argv) < 2:
print(f"Usage: python {filename} <input_dir>")
return
input_dir = sys.argv[1] if folder is None else folder
if not os.path.exists(input_dir):
print(f"Directory {input_dir} does not exist")
return
grouped_videos = categorize_files(input_dir)
# walk through all metadata files and check for missing keys
for video in grouped_videos:
metadata_file = video["metadata"]
if not metadata_file:
continue
with open(metadata_file, "r", encoding="utf-8") as f:
metadata = json.load(f)
missing_keys = []
for key in POSSIBLE_MISSING_KEYS:
if key["key"] not in metadata:
missing_keys.append(key["key"])
if missing_keys:
print(f"Missing keys in {metadata_file}: {missing_keys}")
print(
"Finished checking for missing keys in metadata files. If no output was printed, all metadata files are good."
)
if __name__ == "__main__":
main()