From 602be593598fa9112cdd0eb5d7f3863a9edc4e89 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Tue, 2 Feb 2021 11:55:39 +0100 Subject: [PATCH] Save in a .json file the balance history --- .gitignore | 3 ++- .../TwitchChannelPointsMiner.py | 5 +++++ TwitchChannelPointsMiner/classes/WebSocketsPool.py | 7 +++++-- .../classes/entities/Streamer.py | 13 +++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 0a444db..1bd3575 100644 --- a/.gitignore +++ b/.gitignore @@ -145,4 +145,5 @@ chromedriver* cookies/* logs/* screenshots/* -htmls/* \ No newline at end of file +htmls/* +analytics/* diff --git a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py index c0b4623..6c03f2b 100644 --- a/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py +++ b/TwitchChannelPointsMiner/TwitchChannelPointsMiner.py @@ -2,6 +2,7 @@ import copy import logging +import os import random import signal import sys @@ -10,6 +11,7 @@ import time import uuid from collections import OrderedDict from datetime import datetime +from pathlib import Path from TwitchChannelPointsMiner.classes.entities.PubsubTopic import PubsubTopic from TwitchChannelPointsMiner.classes.entities.Streamer import ( @@ -52,6 +54,9 @@ class TwitchChannelPointsMiner: # Default values for all streamers streamer_settings: StreamerSettings = StreamerSettings(), ): + Settings.analytics_path = os.path.join(Path().absolute(), "analytics") + Path(Settings.analytics_path).mkdir(parents=True, exist_ok=True) + self.username = username # Set as globally config diff --git a/TwitchChannelPointsMiner/classes/WebSocketsPool.py b/TwitchChannelPointsMiner/classes/WebSocketsPool.py index 1a46173..c4b125e 100644 --- a/TwitchChannelPointsMiner/classes/WebSocketsPool.py +++ b/TwitchChannelPointsMiner/classes/WebSocketsPool.py @@ -128,11 +128,14 @@ class WebSocketsPool: if streamer_index != -1: try: if message.topic == "community-points-user-v1": + if message.type in ["points-earned", "points-spent"]: + balance = message.data["balance"]["balance"] + ws.streamers[streamer_index].channel_points = balance + ws.streamers[streamer_index].persistent_history() + if message.type == "points-earned": earned = message.data["point_gain"]["total_points"] reason_code = message.data["point_gain"]["reason_code"] - balance = message.data["balance"]["balance"] - ws.streamers[streamer_index].channel_points = balance logger.info( f"+{earned} → {ws.streamers[streamer_index]} - Reason: {reason_code}.", extra={"emoji": ":rocket:"}, diff --git a/TwitchChannelPointsMiner/classes/entities/Streamer.py b/TwitchChannelPointsMiner/classes/entities/Streamer.py index 6eef727..4794510 100644 --- a/TwitchChannelPointsMiner/classes/entities/Streamer.py +++ b/TwitchChannelPointsMiner/classes/entities/Streamer.py @@ -1,5 +1,8 @@ +import json import logging +import os import time +from threading import Lock from TwitchChannelPointsMiner.classes.entities.Bet import BetSettings from TwitchChannelPointsMiner.classes.entities.Stream import Stream @@ -57,6 +60,8 @@ class Streamer(object): self.streamer_url = f"{URL}/{self.username}" self.chat_url = f"{URL}/popout/{self.username}/chat?popout=" + self.mutex = Lock() + def __repr__(self): return f"Streamer(username={self.username}, channel_id={self.channel_id}, channel_points={_millify(self.channel_points)})" @@ -101,3 +106,11 @@ class Streamer(object): def stream_up_elapsed(self): return self.stream_up == 0 or ((time.time() - self.stream_up) > 120) + + def persistent_history(self): + fname = os.path.join(Settings.analytics_path, f"{self.username}.json") + with self.mutex: + data = json.load(open(fname)) if os.path.isfile(fname) else [] + data.append([time.time, self.channel_points]) + with open(fname, "w") as outfile: + json.dump(data, outfile, indent=4)