Last active
August 29, 2015 14:05
-
-
Save nucular/cf386d9028604eb88424 to your computer and use it in GitHub Desktop.
tipstat (Python 2.7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
try: | |
from urllib2 import urlopen | |
from urllib import urlencode | |
except ImportError: | |
from urllib.request import urlopen | |
from urllib.parse import urlencode | |
import argparse, datetime, re | |
from tabulate import tabulate | |
incoming = 0 | |
outgoing = 0 | |
tippers = {} | |
tippees = {} | |
def parse(line, nick, mode): | |
global incoming, outgoing, tippers, tippees | |
_, n, msg = line.split(" ", 2) | |
if n != "<Doger>": return | |
m = re.match(r"Such ([\w\<\-\[\]\\\^\{\}]+) tipped much \xc6\x89(\d+) to (\w+)\! \(to claim /msg Doger help\) \[\w+\]", | |
msg, re.U) | |
if m: | |
g = m.groups() | |
fr = g[0] | |
to = g[2] | |
amt = float(g[1]) | |
if (mode == 1 or mode == 2) and fr.lower() == nick.lower(): | |
outgoing += amt | |
if to in tippees: | |
tippees[to][0] += amt | |
tippees[to][1] += 1 | |
else: | |
tippees[to] = [amt, 1] | |
elif (mode == 0 or mode == 2) and to.lower() == nick.lower(): | |
incoming += amt | |
if fr in tippers: | |
tippers[fr][0] += amt | |
tippers[fr][1] += 1 | |
else: | |
tippers[fr] = [amt, 1] | |
def tipstat(channel, head, tail, nick, mode=2, sorting=0, reverse=False, local=False): | |
global tippers, tippees, outgoing, incoming | |
title = "Tips " + (["to", "from", "to and from"][mode]) + ": " + nick | |
print(title) | |
print("=" * len(title)) | |
print("Start (UTC): " + head.strftime("%Y.%m.%d %H:%M:%S")) | |
print("End (UTC): " + tail.strftime("%Y.%m.%d %H:%M:%S")) | |
print("") | |
if not local: | |
rep = urlopen("http://mniip.com/irc/log/?" | |
+ urlencode({ | |
"channel": channel.replace("#", "~"), | |
"head": head.strftime("%Y.%m.%d-%H:%M:%S"), | |
"tail": tail.strftime("%Y.%m.%d-%H:%M:%S"), | |
"grep": "Doger" | |
}) + "&raw") | |
with open("temp.txt", "wt") as f: | |
f.write(rep.read()) | |
rep.close() | |
f = open("temp.txt", "rt") | |
for i in f: | |
parse(i, nick, mode) | |
if (mode == 0 or mode == 2): print("Incoming: \xc6\x89{:,}".format(incoming)) | |
if (mode == 1 or mode == 2): print("Outgoing: \xc6\x89{:,}".format(outgoing)) | |
print("Net: \xc6\x89{:,}".format(incoming - outgoing)) | |
skey = lambda o: o[sorting] | |
if (mode == 0 or mode == 2) and len(tippers) > 0: | |
print("") | |
tippers = sorted([ | |
( | |
i, | |
tippers[i][0], | |
tippers[i][1], | |
"{:.3f}".format((tippers[i][0] / incoming) * 100) | |
) | |
for i in tippers | |
], key=skey, reverse=reverse) | |
print("Tippers (incoming)") | |
print(tabulate(tippers, headers=["Nick", "Amount", "Tips", "%"], tablefmt="rst")) | |
if (mode == 1 or mode == 2) and len(tippees) > 0: | |
print("") | |
tippees = sorted([ | |
( | |
i, | |
tippees[i][0], | |
tippees[i][1], | |
"{:.3f}".format((tippees[i][0] / outgoing) * 100) | |
) | |
for i in tippees | |
], key=skey, reverse=reverse) | |
print("Tippees (outgoing)") | |
print(tabulate(tippees, headers=["Nick", "Amount", "Tips", "%"], tablefmt="rst")) | |
print("\n\n(https://gist.github.com/nucular/cf386d9028604eb88424)") | |
f.close() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Fetches logs from mniip's server and counts public Doger tips to and from a nick between two given times.", | |
epilog="Dates are expected in UTC, in the following format: YYYY.MM.DD-HH:MM:SS") | |
parser.add_argument("nick", metavar="NICK", type=str, help="the target nickname") | |
parser.add_argument("--channel", type=str, default="#dogecoin", help="the logged channel, defaults to #dogecoin") | |
parser.add_argument("--head", type=str, help="the start date/time, defaults to 30 days before tail") | |
parser.add_argument("--tail", type=str, help="the end date/time, defaults to now") | |
parser.add_argument("--mode", type=str, choices=["in", "out", "both"], default="both", help="whether to log incoming/outgoing tips, defaults to both") | |
parser.add_argument("--sort", type=str, choices=["nick", "amount", "tips"], default="amount", help="sort tipper/tippee tables by header, defaults to amount") | |
parser.add_argument("--reverse", action="store_true", help="reverse tipper/tippee tables sorting") | |
parser.add_argument("--local", action="store_true", help="don't fetch logs but instead use temp.txt in the current folder, ignore head/tail dates") | |
ns = parser.parse_args() | |
if not ns.tail: | |
ns.tail = datetime.datetime.now() | |
else: | |
ns.tail = datetime.datetime.strptime(ns.tail, "%Y.%m.%d-%H:%M:%S") | |
if not ns.head: | |
ns.head = ns.tail - datetime.timedelta(days=30) | |
else: | |
ns.head = datetime.datetime.strptime(ns.head, "%Y.%m.%d-%H:%M:%S") | |
ns.mode = {"in": 0, "out": 1, "both": 2}[ns.mode] | |
ns.sort = {"nick": 0, "amount": 1, "tips": 2}[ns.sort] | |
tipstat(ns.channel, ns.head, ns.tail, ns.nick, ns.mode, ns.sort, | |
ns.reverse, ns.local) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment