Skip to content

Printing colorful EventPrediction results - and more! #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ twitch_miner = TwitchChannelPointsMiner(
console_level=logging.INFO, # Level of logs - use logging.DEBUG for more info)
file_level=logging.DEBUG, # Level of logs - If you think the log file it's too big, use logging.INFO
emoji=True, # On Windows, we have a problem printing emoji. Set to false if you have a problem
less=False # If you think that the logs are too verbose, set this to True
less=False, # If you think that the logs are too verbose, set this to True
colored=True # If you want to print colored text
),
streamer_settings=StreamerSettings(
make_predictions=True, # If you want to Bet / Make prediction
Expand Down Expand Up @@ -249,8 +250,9 @@ Make sure to write the streamers array in order of priority from left to right.
| `save` | bool | True | If you want to save logs in file (suggested) |
| `less` | bool | False | Reduce the logging format and message verbosity [#10](https://github.com/Tkd-Alex/Twitch-Channel-Points-Miner-v2/issues/10) |
| `console_level` | level | logging.INFO | Level of logs in terminal - Use logging.DEBUG for more helpful messages. |
| `file_level` | level | logging.DEBUG | Level of logs in file save - If you think the log file it's too big, use logging.INFO |
| `emoji` | bool | For Windows is False else True | On Windows, we have a problem printing emoji. Set to false if you have a problem |
| `file_level` | level | logging.DEBUG | Level of logs in file save - If you think the log file it's too big, use logging.INFO |
| `emoji` | bool | For Windows is False else True | On Windows, we have a problem printing emoji. Set to false if you have a problem |
| `colored` | bool | True | If you want to print colored text |
### StreamerSettings
| Key | Type | Default | Description |
|-------------------- |------------- |-------------------------------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down
3 changes: 3 additions & 0 deletions TwitchChannelPointsMiner/TwitchChannelPointsMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
internet_connection_available,
set_default_settings,
)
from colorama import init

init()

# Suppress:
# - chardet.charsetprober - [feed]
Expand Down
8 changes: 7 additions & 1 deletion TwitchChannelPointsMiner/classes/WebSocketsPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
import time

from colorama import Fore
from dateutil import parser

from TwitchChannelPointsMiner.classes.entities.EventPrediction import EventPrediction
Expand Down Expand Up @@ -309,7 +310,12 @@ def on_message(ws, message):
points_prefix = "+" if points_gained >= 0 else ""
logger.info(
f"{ws.events_predictions[event_id]} - Result: {result_type}, Gained: {points_prefix}{_millify(points_gained)}",
extra={"emoji": ":bar_chart:"},
extra={
"emoji": ":bar_chart:",
"color": Fore.GREEN
if result_type == "WIN"
else Fore.RED,
},
)
ws.events_predictions[event_id].final_result = {
"type": event_result["type"],
Expand Down
17 changes: 13 additions & 4 deletions TwitchChannelPointsMiner/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from pathlib import Path

import emoji
from colorama import Style

from TwitchChannelPointsMiner.utils import remove_emoji


class EmojiFormatter(logging.Formatter):
def __init__(self, *, fmt, datefmt=None, print_emoji=True):
class GlobalFormatter(logging.Formatter):
def __init__(self, *, fmt, datefmt=None, print_emoji=True, print_colored=False):
self.print_emoji = print_emoji
self.print_colored = print_colored
logging.Formatter.__init__(self, fmt=fmt, datefmt=datefmt)

def format(self, record):
Expand All @@ -36,6 +38,9 @@ def format(self, record):
# Full remove using a method from utils.
record.msg = remove_emoji(record.msg)

if self.print_colored and hasattr(record, "color"):
record.msg = f"{record.color}{record.msg}{Style.RESET_ALL}"

return super().format(record)


Expand All @@ -47,12 +52,14 @@ def __init__(
console_level: int = logging.INFO,
file_level: int = logging.DEBUG,
emoji: bool = platform.system() != "Windows",
colored: bool = False,
):
self.save = save
self.less = less
self.console_level = console_level
self.file_level = file_level
self.emoji = emoji
self.colored = colored


def configure_loggers(username, settings):
Expand All @@ -62,7 +69,7 @@ def configure_loggers(username, settings):
console_handler = logging.StreamHandler()
console_handler.setLevel(settings.console_level)
console_handler.setFormatter(
EmojiFormatter(
GlobalFormatter(
fmt=(
"%(asctime)s - %(levelname)s - [%(funcName)s]: %(message)s"
if settings.less is False
Expand All @@ -72,6 +79,7 @@ def configure_loggers(username, settings):
"%d/%m/%y %H:%M:%S" if settings.less is False else "%d/%m %H:%M:%S"
),
print_emoji=settings.emoji,
print_colored=settings.colored,
)
)
root_logger.addHandler(console_handler)
Expand All @@ -85,10 +93,11 @@ def configure_loggers(username, settings):
)
file_handler = logging.FileHandler(logs_file, "w", "utf-8")
file_handler.setFormatter(
EmojiFormatter(
GlobalFormatter(
fmt="%(asctime)s - %(levelname)s - %(name)s - [%(funcName)s]: %(message)s",
datefmt="%d/%m/%y %H:%M:%S",
print_emoji=settings.emoji,
print_colored=settings.colored,
)
)
file_handler.setLevel(settings.file_level)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ python-dateutil
emoji
millify
pre-commit
colorama