-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix_dates.py
64 lines (50 loc) · 1.74 KB
/
fix_dates.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
""" Fix filename dates """
import os
import os.path
import re
import time
import arrow
from pony import orm
from publ import index, model
from publ.config import config
from app import app
def reindex():
with app.app_context():
print('Indexing', end='', flush=True)
index.scan_index(config.content_folder, False)
n = 0
while index.in_progress():
n += 1
if n % 5 == 0:
print('.', end='', flush=True)
time.sleep(0.1)
print('Done')
reindex()
fix_count = 0
with orm.db_session:
for entry in model.Entry.select(category='blog'):
dirname = os.path.dirname(entry.file_path)
basename, ext = os.path.splitext(os.path.basename(entry.file_path))
match = re.match(r'([0-9\-.A-Z]+[0-9A-Z])(.*)', basename)
if match:
status = model.PublishStatus(entry.status)
if status not in (model.PublishStatus.PUBLISHED, model.PublishStatus.SCHEDULED):
eid = status.name
else:
eid = entry.id
date = arrow.get(entry.local_date).format('YYYYMMDD')
if status == model.PublishStatus.HIDDEN:
prefix = f'HIDDEN-{entry.id}'
elif status == model.PublishStatus.DRAFT:
prefix = 'DRAFT'
else:
prefix = f'{date}-{eid}'
if prefix != match.group(1):
dest_name = f'{prefix}{match.group(2)}'
print(f'{entry.category} {basename} -> {dest_name}')
dest_path = os.path.join(dirname, dest_name + ext)
os.rename(entry.file_path, dest_path)
fix_count += 1
if fix_count > 0:
print(f'{fix_count} files renamed')
reindex()