-
Notifications
You must be signed in to change notification settings - Fork 2
/
url.py
27 lines (24 loc) · 1012 Bytes
/
url.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
import json
from datetime import datetime
import os
import re
def main():
rootdir = '/home/snay2/blog/content/archive'
for folder, subs, files in os.walk(rootdir):
for filename in files:
lines = []
slug = fulldate = ''
print os.path.join(folder, filename)
with open(os.path.join(folder, filename), 'r') as src:
lines = src.readlines()
print lines[2]
slug = re.search('slug = "(.*)"\n', lines[2]).groups()[0]
fulldate = re.search('date = "(.*)"\n', lines[3]).groups()[0]
with open(os.path.join(folder, filename), 'w') as dest:
created = datetime.strptime(fulldate, "%Y-%m-%dT%H:%M:%S.000Z")
url = '/%d/%02d/%02d/%s' % (created.year, created.month, created.day, slug)
dest.writelines(lines[:3])
dest.write('url = "%s"\n' % url)
dest.writelines(lines[3:])
if __name__=='__main__':
main()