Save in a .json file the balance history

This commit is contained in:
Alessandro Maggio
2021-02-02 11:55:39 +01:00
parent e13cd4b46b
commit 602be59359
4 changed files with 25 additions and 3 deletions

3
.gitignore vendored
View File

@@ -145,4 +145,5 @@ chromedriver*
cookies/*
logs/*
screenshots/*
htmls/*
htmls/*
analytics/*

View File

@@ -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

View File

@@ -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:"},

View File

@@ -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)