#!/usr/bin/python3.4 import datetime import time import sys import os import shutil import subprocess from pprint import pprint from data_lib import load_data, decode_auto CWD = os.path.dirname(os.path.realpath(__file__)) def sort_data_by_date(item): return int(time.mktime(datetime.datetime.strptime(item['date'], '%d/%m/%y').timetuple())) def resize(in_path, out_path): subprocess.call(['convert', in_path, '-resize', '250', out_path]) def gen_previews(): img_dir = os.path.join(CWD, 'img') img_previews_dir = os.path.join(CWD, 'img_previews') if os.path.exists(img_previews_dir): shutil.rmtree(img_previews_dir) os.makedirs(img_previews_dir) for img in os.listdir(img_dir): img_path = os.path.join(img_dir, img) img_preview_path = os.path.join(img_previews_dir, img) resize(img_path, img_preview_path) def main(): data = load_data() data = sorted(data, key=sort_data_by_date) print("Generating previews (don't forget to git add them)...") gen_previews() print("Generating page...") buf = [] for post in data: cipher_type = post['type'] if 'type' in post else 1 decoded_text = decode_auto(post['text'], cipher_type) post_buf = '' post_buf += '**Дата**: %s\n\n' % post['date'] if 'pic' in post and post['pic']: # make sure it is a list pic = post['pic'] if isinstance(post['pic'], list) else [post['pic']] pic_buf = [] for p in pic: pic_buf.append('[![](./img_previews/%s)](./img/%s)' % (p, p)) post_buf += '**Пикрилейтед:**\n\n%s\n\n' % ''.join(pic_buf) if 'link' in post: # make sure it is a list link = post['link'] if isinstance(post['link'], list) else [post['link']] link_buf = [] for l in link: link_buf.append('[%s](%s)' % (l, l)) post_buf += '**Ссылки:** %s\n\n' % ', '.join(link_buf) if 'source' in post: post_buf += '**Источник:** %s' % post['source'] if 'source_link' in post: post_buf += ', [%s](%s)\n\n' % (post['source_link'], post['source_link']) else: post_buf += '\n\n' post_buf += '**Шифровка (тип %d)**: \n> %s\n\n' % (cipher_type, post['text']) post_buf += '**Расшифровка:**\n> %s' % decoded_text buf.append(post_buf) md = "\n\n\n\n---------\n\n\n\n".join(buf) with open(os.path.join(CWD, 'data.md'), 'w') as f: f.write(md) print("Done.") if __name__ == '__main__': sys.exit(main())