# Path: lib/model.py
# def now():
# def init_db():
# class Log(Base):
# class LogPage(Base):
# 
# Path: lib/export.py
# def export_chat(redis, sql, url):
#     log = sql.query(Log).filter(Log.url == url).scalar()
# 
#     if not log:
#         return
# 
#     # Create temp directory.
#     if not os.path.exists('tmp/' + log.url):
#         os.makedirs('tmp/' + log.url)
# 
#     # Create export pages.
#     for page in log.pages:
#         with codecs.open('tmp/' + log.url + '/' + str(page.number) + '.html', 'w', 'utf8') as f:
#             paginator = paginate.Page([], page=page.number, items_per_page=1, item_count=log.page_count, url=PageURL(log.url))
# 
#             lines = page.content.split('\n')[0:-1]
#             lines = filter(lambda x: x is not None, map(lambda _: parse_line(_, 0), lines))
# 
#             for line in lines:
#                 line['datetime'] = datetime.datetime.fromtimestamp(line['timestamp']/1000.0)
# 
#             f.write(log_template.render(
#                 lines=lines,
#                 paginator=paginator
#             ))
# 
#     # Copy static assets
#     shutil.copyfile('static/js/bbcode.js', 'tmp/' + log.url + '/bbcode.js')
#     shutil.copyfile('static/js/jquery.min.js', 'tmp/' + log.url + '/jquery.min.js')
#     shutil.copyfile('static/css/msparp.css', 'tmp/' + log.url + '/msparp.css')
#     shutil.copyfile('static/css/chat.css', 'tmp/' + log.url + '/chat.css')
# 
#     # Create export zip.
#     with zipfile.ZipFile('logs/' + log.url + ".zip", 'w', zipfile.ZIP_DEFLATED) as logzip:
#         zipdir('tmp/'+log.url, logzip, log.url)
# 
#     # Cleanup the temporary files.
#     shutil.rmtree('tmp/' + log.url, ignore_errors=True)


from redis import Redis
from lib.model import sm
from lib.export import export_chat
import time
import datetime
import os
#!/usr/bin/python



if __name__ == '__main__':

    if not os.path.exists("logs"):
        os.makedirs("logs")

    redis = Redis(host=os.environ['REDIS_HOST'], port=int(os.environ['REDIS_PORT']), db=int(os.environ['REDIS_DB']))
    sql = sm()

    current_time = datetime.datetime.now()

    while True:
        if datetime.datetime.now().minute != current_time.minute:
            # Export chats.
            for chat in redis.smembers('export-queue'):
                export_chat(redis, sql, chat)
                redis.srem('export-queue', chat)
                redis.set('chat.' + chat + '.exported', 1)
                redis.expire('chat.' + chat + '.exported', 86400)  # 86400 seconds = 1 day


# Path: lib/model.py
# def now():
# def init_db():
# class Log(Base):
# class LogPage(Base):
# 
# Path: lib/export.py
# def export_chat(redis, sql, url):
#     log = sql.query(Log).filter(Log.url == url).scalar()
# 
#     if not log:
#         return
# 
#     # Create temp directory.
#     if not os.path.exists('tmp/' + log.url):
#         os.makedirs('tmp/' + log.url)
# 
#     # Create export pages.
#     for page in log.pages:
#         with codecs.open('tmp/' + log.url + '/' + str(page.number) + '.html', 'w', 'utf8') as f:
#             paginator = paginate.Page([], page=page.number, items_per_page=1, item_count=log.page_count, url=PageURL(log.url))
# 
#             lines = page.content.split('\n')[0:-1]
#             lines = filter(lambda x: x is not None, map(lambda _: parse_line(_, 0), lines))
# 
#             for line in lines:
#                 line['datetime'] = datetime.datetime.fromtimestamp(line['timestamp']/1000.0)
# 
#             f.write(log_template.render(
#                 lines=lines,
#                 paginator=paginator
#             ))
# 
#     # Copy static assets
#     shutil.copyfile('static/js/bbcode.js', 'tmp/' + log.url + '/bbcode.js')
#     shutil.copyfile('static/js/jquery.min.js', 'tmp/' + log.url + '/jquery.min.js')
#     shutil.copyfile('static/css/msparp.css', 'tmp/' + log.url + '/msparp.css')
#     shutil.copyfile('static/css/chat.css', 'tmp/' + log.url + '/chat.css')
# 
#     # Create export zip.
#     with zipfile.ZipFile('logs/' + log.url + ".zip", 'w', zipfile.ZIP_DEFLATED) as logzip:
#         zipdir('tmp/'+log.url, logzip, log.url)
# 
#     # Cleanup the temporary files.
#     shutil.rmtree('tmp/' + log.url, ignore_errors=True)


# Path: exporter.py
from redis import Redis
from lib.model import sm
from lib.export import export_chat
import time
import datetime
import os

#!/usr/bin/python



if __name__ == '__main__':

    if not os.path.exists("logs"):
        os.makedirs("logs")

    redis = Redis(host=os.environ['REDIS_HOST'], port=int(os.environ['REDIS_PORT']), db=int(os.environ['REDIS_DB']))
    sql = sm()

    current_time = datetime.datetime.now()

    while True:
        if datetime.datetime.now().minute != current_time.minute:
            # Export chats.
            for chat in redis.smembers('export-queue'):
                export_chat(redis, sql, chat)
                redis.srem('export-queue', chat)
                redis.set('chat.' + chat + '.exported', 1)
                redis.expire('chat.' + chat + '.exported', 86400)  # 86400 seconds = 1 day


        current_time = datetime.datetime.now()