# Path: logviewer/parser.py
# def parse_log(fp, start=None):
#     def extract_time(match):
#         t = match.group('timestamp')
#         try:
#             # ISO 8601
#             timestamp = t.split('.')[0]
#             return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
#         except ValueError:
#             # RFC 2822
#             t = email.utils.parsedate(t)
#             if t is not None:
#             	t = time.mktime(t)
#             else:
#             	t = 0 # fail
#             return datetime.fromtimestamp(t)
# 
#     no = 0
#     for line in fp:
#         m = LINE_PATTERN.match(line)
#         if not m:
#             continue
# 
#         if 'PRIVMSG' in m.group('data'):
#             no += 1
# 
#             if start and no < start:
#                 continue
#             pm = PRIVMSG_PATTERN.match(m.group('data'))
#             if not pm:
#                 continue
# 
#             is_bot = not pm.group('nick')
#             pmm = PROXY_MSG_PATTERN.match(pm.group('text'))
#             if is_bot and pmm:
#                 nick = pmm.group('nick')
#                 text = pmm.group('text')
#                 is_bot = False
#             else:
#                 nick = pm.group('nick')
#                 text = pm.group('text')
# 
#             yield dict(type='privmsg', no=no,
#                 time=extract_time(m),
#                 nick=nick, text=text, is_bot=is_bot)
#         elif (m.group('dir') == '>>>' and start is None and
#               m.group('data').startswith('JOIN ') and
#               JOIN_PATTERN.match(m.group('data'))):
#             yield dict(type='join', time=extract_time(m), is_bot=True, no=-1)
# 
# Path: logviewer/util.py
# def irc_channels(config):


import time
import io
import glob
import os.path
from xml.sax.saxutils import escape
from flask import current_app
from logviewer.parser import parse_log
from logviewer import util
    nick = msg['nick']
    date = msg['time'].strftime('%Y%m%d')
    timestamp = int(time.mktime(msg['time'].timetuple()))
    lineno = msg['no']
    bot = msg['is_bot']

    print '<sphinx:document id="%s">' % id_
    print '<content>%s</content>' % content
    print '<channel>%s</channel>' % channel
    print '<no>%s</no>' % lineno
    print '<nick>%s</nick>' % nick
    print '<date>%s</date>' % date
    print '<time>%s</time>' % timestamp
    print '<bot>%d</bot>' % int(bot)  # 1 or 0
    print '</sphinx:document>'


def main():
    print '''<?xml version="1.0" encoding="utf-8"?>
<sphinx:docset>
<sphinx:schema>
    <sphinx:field name="content" attr="string"/>
    <sphinx:attr name="channel" type="string"/>
    <sphinx:attr name="no" type="int" bits="32"/>
    <sphinx:attr name="nick" type="string"/>
    <sphinx:attr name="date" type="int" bits="32"/>
    <sphinx:attr name="time" type="timestamp"/>
    <sphinx:attr name="bot" type="bool"/>
</sphinx:schema>'''
    log_dir = current_app.config['LOG_DIR']

# Path: logviewer/parser.py
# def parse_log(fp, start=None):
#     def extract_time(match):
#         t = match.group('timestamp')
#         try:
#             # ISO 8601
#             timestamp = t.split('.')[0]
#             return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
#         except ValueError:
#             # RFC 2822
#             t = email.utils.parsedate(t)
#             if t is not None:
#             	t = time.mktime(t)
#             else:
#             	t = 0 # fail
#             return datetime.fromtimestamp(t)
# 
#     no = 0
#     for line in fp:
#         m = LINE_PATTERN.match(line)
#         if not m:
#             continue
# 
#         if 'PRIVMSG' in m.group('data'):
#             no += 1
# 
#             if start and no < start:
#                 continue
#             pm = PRIVMSG_PATTERN.match(m.group('data'))
#             if not pm:
#                 continue
# 
#             is_bot = not pm.group('nick')
#             pmm = PROXY_MSG_PATTERN.match(pm.group('text'))
#             if is_bot and pmm:
#                 nick = pmm.group('nick')
#                 text = pmm.group('text')
#                 is_bot = False
#             else:
#                 nick = pm.group('nick')
#                 text = pm.group('text')
# 
#             yield dict(type='privmsg', no=no,
#                 time=extract_time(m),
#                 nick=nick, text=text, is_bot=is_bot)
#         elif (m.group('dir') == '>>>' and start is None and
#               m.group('data').startswith('JOIN ') and
#               JOIN_PATTERN.match(m.group('data'))):
#             yield dict(type='join', time=extract_time(m), is_bot=True, no=-1)
# 
# Path: logviewer/util.py
# def irc_channels(config):


# Path: logviewer/indexer.py
import time
import io
import glob
import os.path
from xml.sax.saxutils import escape
from flask import current_app
from logviewer.parser import parse_log
from logviewer import util

    nick = msg['nick']
    date = msg['time'].strftime('%Y%m%d')
    timestamp = int(time.mktime(msg['time'].timetuple()))
    lineno = msg['no']
    bot = msg['is_bot']

    print '<sphinx:document id="%s">' % id_
    print '<content>%s</content>' % content
    print '<channel>%s</channel>' % channel
    print '<no>%s</no>' % lineno
    print '<nick>%s</nick>' % nick
    print '<date>%s</date>' % date
    print '<time>%s</time>' % timestamp
    print '<bot>%d</bot>' % int(bot)  # 1 or 0
    print '</sphinx:document>'


def main():
    print '''<?xml version="1.0" encoding="utf-8"?>
<sphinx:docset>
<sphinx:schema>
    <sphinx:field name="content" attr="string"/>
    <sphinx:attr name="channel" type="string"/>
    <sphinx:attr name="no" type="int" bits="32"/>
    <sphinx:attr name="nick" type="string"/>
    <sphinx:attr name="date" type="int" bits="32"/>
    <sphinx:attr name="time" type="timestamp"/>
    <sphinx:attr name="bot" type="bool"/>
</sphinx:schema>'''
    log_dir = current_app.config['LOG_DIR']

    channels = util.irc_channels(current_app.config['IRC_CHANNELS'])