Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions db_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
class DatabaseLogger:
conn = None
cursor = None
senders_whitelist = None

def __init__(self, host, name, user, password):
def __init__(self, host, name, user, password, senders_whitelist=None):
self.conn = psycopg2.connect(host=host, dbname=name, user=user, password=password)
self.conn.autocommit = True
self.cursor = self.conn.cursor()
self.senders_whitelist = senders_whitelist

def close(self):
self.cursor.close()
Expand All @@ -24,8 +26,9 @@ def log_chat(self, sender, message, channel):
return

try:
self.cursor.execute("INSERT INTO chat_log (sender, message, channel, date) VALUES (%s, %s, %s, %s)",
(sender, message, channel, current_time_in_milli()))
if (self.senders_whitelist is not None and sender in self.senders_whitelist) or (self.senders_whitelist is None):
self.cursor.execute("INSERT INTO chat_log (sender, message, channel, date) VALUES (%s, %s, %s, %s)",
(sender, message, channel, current_time_in_milli()))
except psycopg2.DataError as e:
print e
print message
Expand Down
23 changes: 19 additions & 4 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ class TwitchManager:
SECONDS_BETWEEN_UPDATE_STREAMS = 60
SECONDS_BETWEEN_CREATE_BOTS = 15

def __init__(self, channels_amount, channels, log_filename=None):
def __init__(self, channels_amount, channels, log_filename=None, users_whitelist=None):
self.bots = []
self.channels_amount = channels_amount
self.log_filename = log_filename
self.channels = channels
self.db_logger = DatabaseLogger(settings.DATABASE['HOST'],
if settings.USERS is not None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have been more explicit here. You don't actually need the if here (neither the else and the contents inside it). In case settings.USERS is None, you will automatically be passing None as the last argument as well.

self.db_logger = DatabaseLogger(settings.DATABASE['HOST'],
settings.DATABASE['NAME'],
settings.DATABASE['USER'],
settings.DATABASE['PASSWORD'],
settings.USERS)
else:
self.db_logger = DatabaseLogger(settings.DATABASE['HOST'],
settings.DATABASE['NAME'],
settings.DATABASE['USER'],
settings.DATABASE['PASSWORD'])
Expand All @@ -33,10 +40,18 @@ def _create_bot(self, name, channels):
settings.IRC['NICK'],
settings.IRC['PASSWORD'],
self.log_filename)
bot_db_logger = DatabaseLogger(settings.DATABASE['HOST'],
if settings.USERS is not None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

bot_db_logger = DatabaseLogger(settings.DATABASE['HOST'],
settings.DATABASE['NAME'],
settings.DATABASE['USER'],
settings.DATABASE['PASSWORD'])
settings.DATABASE['PASSWORD'],
settings.USERS)
else:
bot_db_logger = DatabaseLogger(settings.DATABASE['HOST'],
settings.DATABASE['NAME'],
settings.DATABASE['USER'],
settings.DATABASE['PASSWORD'])

bot = TwitchBot(name, conn, bot_db_logger, Queue.Queue(), self.log_filename)
bot.daemon = True
bot.connect_and_join_channels(channels)
Expand Down
9 changes: 8 additions & 1 deletion settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ DATABASE = {

API = {
'CLIENTID': 'YOUR_CLIENT_ID'
}
}

# Uncomment to only log certain users.
#USERS = [
# 'yourusername',
# 'friendsname'
#]