Skip to content

Commit

Permalink
Add message parsing target recording (where did dono come from)
Browse files Browse the repository at this point in the history
  • Loading branch information
Latent-Logic committed May 18, 2024
1 parent aa532db commit c65c8d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 3 additions & 0 deletions settings.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,20 @@ money = 1
[tips.msg.StreamElements]
# Example: StreamElements: User2 just tipped $25.00 emoteHeart
regex = "(?P<user>.*) just tipped \\$(?P<amount>[\\d.,]*) emoteHeart.*"
target = "StreamElements"

[tips.msg.KofiStreamBot]
# Example: New donation from User2 for $5.00: "{messsage}". Thank you!
regex = "New donation from (?P<user>.+) for \\$?(?P<amount>[\\d.,]+): .*"
target = "KoFi"

[bits]
min = 0.02
money = 0.01
[bits.msg.SoundAlerts]
# Example: SoundAlerts: User1 played Modem for 150 Bits
regex = "(?P<user>[^ ]*) played (?P<alert>.*) for (?P<amount>[^ ]*) Bits.*"
target = "SoundAlerts"

[subs]
count_multimonth = false
Expand Down
15 changes: 8 additions & 7 deletions test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,23 @@ async def on_message(msg: ChatMessage):
type=BITS,
amount=int(msg.bits),
)
for user, regex, target in SETTINGS.compiled_re:
for user, regex, dono_type, target in SETTINGS.compiled_re:
if msg.user.name.lower() == user.lower():
match = regex.match(msg.text)
if match:
log.info(f"in {msg.room.name}, {match['user']} sent {target}: {match['amount']}")
if target == BITS:
via = f" via {target}" if target else ""
log.info(f"in {msg.room.name}, {match['user']} sent {dono_type}{via}: {match['amount']}")
if dono_type == BITS:
amount = int(match["amount"].replace(",", ""))
elif target == TIPS:
elif dono_type == TIPS:
amount = float(match["amount"].replace(",", ""))
else:
raise ValueError(f"Unknown target from msg parsing {target}")
raise ValueError(f"Unknown target from msg parsing {dono_type}")
Donos().add_event(
ts=msg.sent_timestamp,
user=match["user"],
target=None,
type=target,
target=target,
type=dono_type,
amount=amount,
)

Expand Down
11 changes: 6 additions & 5 deletions twitch_dono_clock/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from datetime import datetime
from typing import Dict, List, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

import toml
from fastapi import HTTPException
Expand Down Expand Up @@ -75,6 +75,7 @@ def div_valid(self):

class SetRegex(BaseModel):
regex: str
target: Optional[str] = None

@property
def re(self) -> re.Pattern:
Expand Down Expand Up @@ -127,20 +128,20 @@ class Settings(BaseModel):
subs: SetSubs
fmt: SetFmt

_compiled_re: List[Tuple[str, re.Pattern, str]] = []
_compiled_re: List[Tuple[str, re.Pattern, str, Optional[str]]] = []

@model_validator(mode="after")
def compile_regex(self):
if self.end.max_minutes:
assert self.end.max_minutes > self.start.minutes, "end.max_minutes must be larger than start.minutes"
for user, obj in self.bits.msg.items():
self._compiled_re.append((user, obj.re, "bits"))
self._compiled_re.append((user, obj.re, "bits", obj.target))
for user, obj in self.tips.msg.items():
self._compiled_re.append((user, obj.re, "tips"))
self._compiled_re.append((user, obj.re, "tips", obj.target))
return self

@property
def compiled_re(self) -> List[Tuple[str, re.Pattern, str]]:
def compiled_re(self) -> List[Tuple[str, re.Pattern, str, Optional[str]]]:
return self._compiled_re

def get_value(self, type_name: str) -> Union[float, int]:
Expand Down

0 comments on commit c65c8d1

Please sign in to comment.