#!/usr/bin/python3
import re
import os
from fnmatch import fnmatch
from datetime import datetime, timedelta

startTime = datetime.now()

def convert_timestamp(timestamp):
	return datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")


fnm_parse_pattern = re.compile('(?<=\/irc\.)(?P<network>.*?)(?=\.#)\.(?P<channel>.*?)(?=\.weechatlog$)')
webm_pattern      = re.compile('https?:\/\/\S*?\/\S*(?<!:\/\/www)\.webm')
timestamp_pattern = re.compile('\d\d(\d\d-?){3}\s(\d\d:?){3}')
nick_pattern      = re.compile('(?<=\d{4}-\d\d-\d\d\s\d\d:\d\d:\d\d\s)(?P<mode>[\+%@&~])?(?P<nick>\S*)')
# Note that the nick pattern is not perfect.
# For example: ircnet can have nicks starting with '~' , while on rizon that is an user mode on a channel

log_dir = str('/home/zertap/.weechat/logs')

log_files  = list()
webm_links = list()

for file_ in os.listdir(log_dir):
	if fnmatch(file_, 'irc.*.#*.weechatlog'):
		log_files.append(os.path.join(log_dir, file_))

for log_file in log_files:
	try:
		network, channel = fnm_parse_pattern.search(log_file).group('network', 'channel')
		with open(log_file) as log:
			for line in log:
				result = webm_pattern.search(line)

				if result:
					timestamp = timestamp_pattern.match(line).group(0)
					timestamp = convert_timestamp(timestamp)
					nick      = nick_pattern.search(line).group('nick')
					links     = webm_pattern.findall(line)

					for link in links:
						webm_links.append((network, channel, timestamp, nick, link))
	except:
		pass
		

webm_links.sort(key=lambda x: x[2]) # Sorts the list of tuples by the third element of each tuple.

seen      = set()
uniq_lnks = list()
for network, channel, timestamp, nick, link in webm_links:
	if link not in seen:
		uniq_lnks.append((network, channel, timestamp, nick, link))
		seen.add(link)

uniq_lnks = uniq_lnks[::-1] # flips the list.

n_links = int(len(webm_links))
n_links_displayed = int(len(uniq_lnks))
n_dupes = int(n_links - n_links_displayed)
prcnt_dupes = float(n_dupes*100/n_links)

# HTML Generation --->

head = list()
head.append('<title>WebM links</title>')
head.append('<script type="text/javascript" src="script/jquery-latest.js"></script>')
head.append('<script type="text/javascript" src="script/jquery.tablesorter.min.js"></script>')
head.append('<script type="text/javascript">$(document).ready(function(){$("#linktable").tablesorter();});</script>')
head.append('<link rel="stylesheet" type="text/css" href="style.css">')
head = '\n'.join(head)

link_table = list()
link_table.append('<table id="linktable" class="tablesorter">')
link_table.append('<thead><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr></thead>' % ('network', 'channel', 'timestamp', 'nick', 'link'))
link_table.append('<tbody>')
for item in uniq_lnks:
	network, channel, timestamp, nick, link = item
	link_table.append('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><a href="%s">%s</a></td></tr>' % (network, channel, timestamp, nick, link, link))
link_table.append('</tbody></table>')
link_table = str('\n'.join(link_table))

info = list()
info.append('<span id="info-header">WebM links parsed from zertap\'s IRC logs</span><br>')
info.append('<span class="note">Note that all times are in EEST</span><br>')
info.append('<span class="info-span">Out of <span class="highlight">%s links</span>, <span class="highlight">%s (%s%%) duplicates</span> were found, leaving us <span class="highlight">%s links</span> to display.</span><br>' % (str(n_links), str(n_dupes), str('%.2f' % prcnt_dupes), str(n_links_displayed)))
info.insert(2, '<span class="info-span">Generated at <span class="highlight">%s</span></span><br>' % (str(datetime.now())[:-7]))
info.insert(2, '<span class="info-span">This data was gathered and page generated in <span class="highlight">%s seconds.</span></span><br>' % ((datetime.now()-startTime).total_seconds()))
info.insert(0, '<div id="info" class="info">')
info.insert(3, '<div id="info-stats-container"><div id="info-stats">')
info.append('</div><div id="info-marker"></div></div></div>')
info = '\n'.join(info)

print('<html><head>%s</head><body>%s<br>%s</body>' % (head, info, link_table))