2017-07-04 10:38:07 +00:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
2017-07-05 09:59:09 +00:00
|
|
|
from string import Template
|
2017-07-04 10:38:07 +00:00
|
|
|
|
|
|
|
from parse import derived_link_info
|
2017-07-05 21:33:51 +00:00
|
|
|
from config import (
|
|
|
|
INDEX_TEMPLATE,
|
|
|
|
INDEX_ROW_TEMPLATE,
|
|
|
|
ARCHIVE_PERMISSIONS,
|
|
|
|
ANSI,
|
|
|
|
chmod_file,
|
|
|
|
)
|
2017-07-04 10:38:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dump_index(links, service):
|
|
|
|
"""create index.html file for a given list of links and service"""
|
|
|
|
|
2017-07-04 11:26:46 +00:00
|
|
|
with open(INDEX_TEMPLATE, 'r', encoding='utf-8') as f:
|
2017-07-04 10:38:07 +00:00
|
|
|
index_html = f.read()
|
|
|
|
|
|
|
|
# TODO: refactor this out into index_template.html
|
2017-07-04 11:26:46 +00:00
|
|
|
with open(INDEX_ROW_TEMPLATE, 'r', encoding='utf-8') as f:
|
2017-07-04 10:38:07 +00:00
|
|
|
link_html = f.read()
|
|
|
|
|
|
|
|
article_rows = '\n'.join(
|
2017-07-05 09:59:09 +00:00
|
|
|
Template(link_html).substitute(**derived_link_info(link)) for link in links
|
2017-07-04 10:38:07 +00:00
|
|
|
)
|
|
|
|
|
2017-07-04 11:24:03 +00:00
|
|
|
template_vars = {
|
|
|
|
'num_links': len(links),
|
|
|
|
'date_updated': datetime.now().strftime('%Y-%m-%d'),
|
|
|
|
'time_updated': datetime.now().strftime('%Y-%m-%d %H:%M'),
|
|
|
|
'rows': article_rows,
|
|
|
|
}
|
2017-07-04 10:38:07 +00:00
|
|
|
|
2017-07-04 10:42:31 +00:00
|
|
|
with open(os.path.join(service, 'index.html'), 'w', encoding='utf-8') as f:
|
2017-07-05 21:33:51 +00:00
|
|
|
f.write(Template(index_html).substitute(**template_vars))
|
|
|
|
|
|
|
|
chmod_file(service, permissions=ARCHIVE_PERMISSIONS)
|
|
|
|
|
|
|
|
print('[+] [{}] Created archive index with {}{}{} links.'.format(
|
|
|
|
datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
ANSI['green'],
|
|
|
|
len(links),
|
|
|
|
ANSI['reset'],
|
|
|
|
))
|