Coverage for src/retrospeedlab/lake/sources/streamelements.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-12-04 00:01 +0000

1from typing import Any 

2 

3from retrospeedlab.addresses import TWITCH_CHANNEL 

4from retrospeedlab.lake.chat.watch import IGNORED_CHATTERS 

5from retrospeedlab.lake.sources.http import client, get_json 

6from retrospeedlab.lake.sources.number import Number 

7from retrospeedlab.lake.sources.settings import StreamElementsSettings 

8 

9API = "https://api.streamelements.com/kappa/v2" 

10 

11 

12def human_amount(counts: list[dict[str, Any]], ignored: frozenset[str]) -> int: 

13 return sum(count["amount"] for count in counts if count["name"].lower() not in ignored) 

14 

15 

16def chat_numbers(chat: dict[str, Any], ignored: frozenset[str]) -> list[Number]: 

17 humans = [chatter for chatter in chat["chatters"] if chatter["name"].lower() not in ignored] 

18 return [ 

19 Number("chat_messages", "Chat messages", human_amount(chat["chatters"], ignored), "", "all time, without bots"), 

20 Number("chatters", "Chatters", len(humans), "", "all time, without bots"), 

21 Number("commands", "Commands", sum(command["amount"] for command in chat["commands"]), "", "all time"), 

22 ] 

23 

24 

25def session_numbers(data: dict[str, Any]) -> list[Number]: 

26 return [ 

27 Number("followers", "Followers", data["follower-total"]["count"], "", "all time"), 

28 Number("followers_month", "New followers", data["follower-month"]["count"], "", "this month"), 

29 Number("subscribers", "Subscribers", data["subscriber-total"]["count"], "", "all time"), 

30 Number("tips", "Tips", data["tip-count"]["count"], "", "all time"), 

31 Number("tip_amount", "Tipped", data["tip-total"]["amount"], "USD", "all time"), 

32 Number("cheers", "Bits cheered", data["cheer-total"]["amount"], "", "all time"), 

33 ] 

34 

35 

36def streamelements_numbers(settings: StreamElementsSettings) -> list[Number]: 

37 with client(API, {"Authorization": f"Bearer {settings.token}"}) as session: 

38 channel = get_json(session, "/channels/me", {})["_id"] 

39 data = get_json(session, f"/sessions/{channel}", {})["data"] 

40 chat = get_json(session, f"/chatstats/{TWITCH_CHANNEL}/stats", {}) 

41 return session_numbers(data) + chat_numbers(chat, IGNORED_CHATTERS)