Skip to content

Commit

Permalink
Insert subs
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaserlang committed Feb 26, 2024
1 parent 3c2142c commit 2463f27
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions tbot/twitch_bot/tasks/count_subs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def count_subs():
FROM twitch_channels
WHERE
active="Y" AND
not isnull(twitch_scope);
not isnull(twitch_scope)
''')
logger.info('Counting subs')
for c in channels:
Expand All @@ -25,6 +25,7 @@ async def count_subs():
try:
subs = await get_subs(bot, c['channel_id'])
await _count_subs(bot, c['channel_id'], subs)
await insert_subs(bot, c['channel_id'], subs)
except Exception as e:
logger.exception(e)
finally:
Expand Down Expand Up @@ -78,4 +79,36 @@ async def _count_subs(bot, channel_id: str, subs: list):
gifted_sub_points=VALUES(gifted_sub_points),
primes=VALUES(primes),
updated_at=VALUES(updated_at)
''', (channel_id, self_subs, gifted_subs, primes, datetime.now(tz=timezone.utc)))
''', (channel_id, self_subs, gifted_subs, primes, datetime.now(tz=timezone.utc)))


async def insert_subs(bot, channel_id: str, subs: list):
data = []
updated_at = datetime.now(tz=timezone.utc)
for sub in subs:
data.append((
channel_id,
updated_at,
sub['user_id'],
sub['plan_name'],
sub['tier'],
sub['gifter_id'],
sub['is_gift'],
sub['total'],
))
await bot.db.executemany('''
INSERT INTO twitch_sub_log
(channel_id, updated_at, user_id, plan_name, tier, gifter_id, is_gift, total)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
updated_at=VALUES(updated_at),
user_id=VALUES(user_id),
plan_name=VALUES(plan_name),
tier=VALUES(tier),
gifter_id=VALUES(gifter_id),
is_gift=VALUES(is_gift),
total=VALUES(total)
''', data)
await bot.db.execute('''
delete from twitch_sub_log where channel_id=%s and updated_at < %s
''', (channel_id, updated_at))

0 comments on commit 2463f27

Please sign in to comment.