-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitchStats.py
54 lines (46 loc) · 2.05 KB
/
TwitchStats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import datetime
import time
from pymongo import MongoClient
from TwitchStatsThread import TwitchStatsThread
class TwitchStats:
def __init__(self, username, auth_token, db_uri, db_name):
print("[+] creating TwitchStats bot")
self._username = username
self._auth_token = auth_token
self._db = MongoClient(db_uri)
self._db_conn = self._db[db_name]
self._threads = { }
def add_channel(self, channel):
if channel not in self._threads:
print("[+] adding channel:", channel)
self._threads[channel] = TwitchStatsThread(channel, self._username, self._auth_token)
self._threads[channel].start()
else:
print("[!] error: channel alread added:", channel)
def remove_channel(self, channel):
if channel in self._threads:
print("[+] remove channel:", channel)
self._threads[channel].terminate()
del self._threads[channel]
def loop(self, delay=60):
cur_day = datetime.datetime.now().day
while True:
time.sleep(delay)
for th in self._threads:
bans, deleted, msgs = 0, 0, 0
data = self._threads[th].poll_ban()
if len(data) > 0:
bans = len(data)
self._db_conn["bans"].insert_many(data)
data = self._threads[th].poll_deleted()
if len(data) > 0:
deleted = len(data)
self._db_conn["deleted"].insert_many(data)
data_msgs = self._threads[th].poll_messages()
if data_msgs["msgs"] > 0:
self._db_conn["messages"].insert_one(data_msgs)
print("[-] [%s] poll data: %d users, %d messages, %d bans, %d deleted" % (
th, len(data_msgs["logins"]), data_msgs["msgs"], bans, deleted))
if cur_day != datetime.datetime.now().day:
print("[-] [%s] new day. pruning messages")
cur_day = datetime.datetime.now().day