-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix_dates.py
60 lines (49 loc) · 1.68 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
""" Fix filename dates """
import time
import os
import os.path
from app import app
from publ import model, index
from publ.config import config
import re
import arrow
from pony import orm
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():
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:
reindex()