-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_file_date.py
45 lines (38 loc) · 1.09 KB
/
set_file_date.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
"""
loads a json representation of a playlist as it was returned by yt-dlp -J and
checks if there is any file in the specified folder, whose id (the part in square brackets in the filename)
is not part of the playlist. If so the file is deleted.
"""
import sys
import json
from pprint import pprint
import os
import re
if len(sys.argv) != 3:
raise ValueError('usage: remove_videos_not_in_playlist <folder> <playlist-json-file>')
folder = sys.argv[1]
jsonfile = sys.argv[2]
with open(jsonfile) as f:
d = json.load(f)
entries = d["entries"]
ids = []
for entry in entries:
try:
ids.append(entry["id"])
except:
# sometimes null or empty entries in playlist (maybe deleted videos)
# just skip
pass
# pprint(d)
print(ids)
files = os.listdir(folder)
for file in files:
print(file)
m = re.match(".*\[([a-zA-Z0-9_\-]+)\].*", file)
if m is not None:
fileId = m[1]
print(fileId)
if not fileId in ids:
fullpath = os.path.join(folder, file)
print("Deleting " + fullpath + "...")
os.remove(fullpath)