#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import json
from collections import OrderedDict
from argument_parser import ArgumentParser
from urllib.parse import unquote
from datetime import datetime
import logging
import os


class HtmlOutputer(object):
    def __init__(self):
        self.args = ArgumentParser()
        self.file_count = 0
        self.log_file = self.args.update_html_log_path + datetime.today().strftime("%Y-%m-%d") + ".txt"
        if not os.path.exists(self.log_file):
            os.mknod(self.log_file)
        self.log_file_handler = open(self.log_file, "a+", encoding='utf-8')

    def collect_data(self, html, item_url, title):
        if html is None:
            return
        self.log_file_handler.write("{0}\t{1}\n".format(title, item_url))
        self.output_html(html, title) 

    def output_html(self, html, title):
        page_split = self.file_count // 100000
        if not os.path.exists(self.args.update_html_output_path + str(page_split)):
            os.mkdir(self.args.update_html_output_path + str(page_split))
        result_file = open(self.args.update_html_output_path + str(page_split) + "/" + 
                           title + ".html", "w", encoding='UTF-8')
        result_file.write(html)
        result_file.close()
        self.file_count += 1
