-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatesort.py
More file actions
executable file
·72 lines (54 loc) · 1.98 KB
/
datesort.py
File metadata and controls
executable file
·72 lines (54 loc) · 1.98 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
#!/usr/bin/python3
import os
import time
import re
import json
relevantre = re.compile ("^(.*\\])\\.(mp4|webm|mkv|mp3|m4a)$")
new_filename = 'new.m3u'
new_content = []
new_update = False
if os.path.isfile(new_filename):
with open(new_filename, 'r') as infile:
new_content = infile.read().strip().split('\n')
for filename in os.listdir():
relevantmatch = relevantre.match(filename)
if relevantmatch:
stub = relevantmatch.group(1)
desc = "%s.description" % (stub,)
info = "%s.info.json" % (stub,)
if os.path.isfile(info):
with open(info, 'r') as infile:
jsdata = json.load(infile)
ytdate = jsdata['upload_date']
dateyr = ytdate[0:4]
datemn = ytdate[4:6]
datedt = ytdate[6:8]
else:
stats = os.lstat(filename)
date = time.gmtime(stats.st_mtime)
dateyr = "%04d" % (date.tm_year,)
datemn = "%02d" % (date.tm_mon,)
datedt = "%02d" % (date.tm_mday,)
target = os.path.join(dateyr, datemn, datedt)
moveto = os.path.join(target, filename)
print ("For", filename, ":")
print (" We will create", target)
print (" And move the file to", moveto)
new_content.append(moveto)
new_update = True
os.makedirs(target, exist_ok = True)
os.rename(filename, moveto)
if os.path.isfile(desc):
moveto = os.path.join(target, desc)
print (" And move", desc)
print (" To", moveto)
os.rename(desc, moveto)
if os.path.isfile(info):
moveto = os.path.join(target, info)
print (" And move", info)
print (" To", moveto)
os.rename(info, moveto)
print ("", flush=True)
if new_update:
with open(new_filename, "w") as outfile:
outfile.write("\n".join(new_content[-10:]) + '\n')