Skip to content
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

Connect user to chat to increase watchtime #47

Merged
merged 10 commits into from
Mar 12, 2021
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,4 @@ chromedriver*
cookies/*
logs/*
screenshots/*
htmls/*
htmls/*
11 changes: 11 additions & 0 deletions TwitchChannelPointsMiner/TwitchChannelPointsMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from TwitchChannelPointsMiner.classes.Exceptions import StreamerDoesNotExistException
from TwitchChannelPointsMiner.classes.Settings import Priority, Settings
from TwitchChannelPointsMiner.classes.Twitch import Twitch
from TwitchChannelPointsMiner.classes.TwitchChat import TwitchChat
from TwitchChannelPointsMiner.classes.WebSocketsPool import WebSocketsPool
from TwitchChannelPointsMiner.logger import LoggerSettings, configure_loggers
from TwitchChannelPointsMiner.utils import (
Expand Down Expand Up @@ -184,6 +185,12 @@ def run(self, streamers: list = [], blacklist: list = [], followers=False):
self.twitch.viewer_is_mod(streamer)
if streamer.viewer_is_mod is True:
streamer.settings.make_predictions = False
if streamer.settings.join_chat is True:
streamer.irc_chat = TwitchChat(
self.username,
self.twitch.twitch_login.get_auth_token(),
streamer.username,
)

self.original_streamers = copy.deepcopy(self.streamers)

Expand Down Expand Up @@ -267,6 +274,10 @@ def run(self, streamers: list = [], blacklist: list = [], followers=False):
def end(self, signum, frame):
logger.info("CTRL+C Detected! Please wait just a moment!")

for streamer in self.streamers:
if streamer.irc_chat is not None:
streamer.leave_chat()

self.running = self.twitch.running = False
self.ws_pool.end()

Expand Down
25 changes: 25 additions & 0 deletions TwitchChannelPointsMiner/classes/TwitchChat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging
from threading import Thread

from TwitchChannelPointsMiner.classes.entities.Chat import Chat

logger = logging.getLogger(__name__)


class TwitchChat(Thread):
def __deepcopy__(self, memo):
return None

def __init__(self, username, token, channel):
super(TwitchChat, self).__init__()
self.token = token
self.username = username
self.channel = channel

def run(self):
# Create IRC bot connection
try:
IRC = Chat(self.username, self.token, self.channel)
IRC.start()
except KeyboardInterrupt:
None # dont handle KeyboardInterruption here
8 changes: 4 additions & 4 deletions TwitchChannelPointsMiner/classes/WebSocketsPool.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
import logging
import random
import threading
import time
from threading import Thread, Timer

from dateutil import parser

Expand Down Expand Up @@ -67,7 +67,7 @@ def __new(self, index):
)

def __start(self, index):
thread_ws = threading.Thread(target=lambda: self.ws[index].run_forever())
thread_ws = Thread(target=lambda: self.ws[index].run_forever())
thread_ws.daemon = True
thread_ws.name = f"WebSocket #{self.ws[index].index}"
thread_ws.start()
Expand Down Expand Up @@ -99,7 +99,7 @@ def run():
)
WebSocketsPool.handle_reconnection(ws)

thread_ws = threading.Thread(target=run)
thread_ws = Thread(target=run)
thread_ws.daemon = True
thread_ws.start()

Expand Down Expand Up @@ -261,7 +261,7 @@ def on_message(ws, message):
current_tmsp
)

place_bet_thread = threading.Timer(
place_bet_thread = Timer(
start_after,
ws.twitch.make_predictions,
(ws.events_predictions[event_id],),
Expand Down
18 changes: 18 additions & 0 deletions TwitchChannelPointsMiner/classes/entities/Chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import irc.bot

from TwitchChannelPointsMiner.constants import IRC, IRC_PORT


class Chat(irc.bot.SingleServerIRCBot):
def __init__(self, username, token, channel):
self.token = token
self.channel = "#" + channel

# Create IRC bot connection
irc.bot.SingleServerIRCBot.__init__(
Tkd-Alex marked this conversation as resolved.
Show resolved Hide resolved
self, [(IRC, IRC_PORT, "oauth:" + token)], username, username
)

def on_welcome(self, c, e):
# You must request specific capabilities before you can use them
c.join(self.channel)
27 changes: 24 additions & 3 deletions TwitchChannelPointsMiner/classes/entities/Streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class StreamerSettings(object):
"claim_drops",
"watch_streak",
"bet",
"join_chat",
]

def __init__(
Expand All @@ -26,22 +27,30 @@ def __init__(
claim_drops: bool = None,
watch_streak: bool = None,
bet: BetSettings = None,
join_chat: bool = True,
):
self.make_predictions = make_predictions
self.follow_raid = follow_raid
self.claim_drops = claim_drops
self.watch_streak = watch_streak
self.bet = bet
self.join_chat = join_chat

def default(self):
for name in ["make_predictions", "follow_raid", "claim_drops", "watch_streak"]:
for name in [
"make_predictions",
"follow_raid",
"claim_drops",
"watch_streak",
"join_chat",
]:
if getattr(self, name) is None:
setattr(self, name, True)
if self.bet is None:
self.bet = BetSettings()

def __repr__(self):
return f"BetSettings(make_predictions={self.make_predictions}, follow_raid={self.follow_raid}, claim_drops={self.claim_drops}, watch_streak={self.watch_streak}, bet={self.bet})"
return f"BetSettings(make_predictions={self.make_predictions}, follow_raid={self.follow_raid}, claim_drops={self.claim_drops}, watch_streak={self.watch_streak}, bet={self.bet}, join_chat={self.join_chat})"


class Streamer(object):
Expand All @@ -56,7 +65,7 @@ class Streamer(object):
"channel_points",
"minute_watched_requests",
"viewer_is_mod",
"stream",
"irc_chat" "stream",
"raid",
"history",
"streamer_url",
Expand All @@ -73,6 +82,7 @@ def __init__(self, username, settings=None):
self.channel_points = 0
self.minute_watched_requests = None
self.viewer_is_mod = False
self.irc_chat = None

self.stream = Stream()

Expand All @@ -95,6 +105,7 @@ def set_offline(self):
if self.is_online is True:
self.offline_at = time.time()
self.is_online = False
self.leave_chat()

logger.info(
f"{self} is Offline!",
Expand All @@ -109,6 +120,7 @@ def set_online(self):
self.online_at = time.time()
self.is_online = True
self.stream.init_watch_streak()
self.join_chat()

logger.info(
f"{self} is Online!",
Expand Down Expand Up @@ -146,3 +158,12 @@ def drops_condition(self):
and self.stream.drops_tags is True
and self.stream.campaigns_ids != []
)

def leave_chat(self):
if self.settings.join_chat is True and self.irc_chat is not None:
self.irc_chat.terminate()

def join_chat(self):
if self.settings.join_chat is True and self.irc_chat is not None:
logger.info(f"Connecting to {self.username}'s chat!")
self.irc_chat.start()
2 changes: 2 additions & 0 deletions TwitchChannelPointsMiner/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Twitch endpoints
URL = "https://www.twitch.tv"
API = "https://api.twitch.tv"
IRC = "irc.chat.twitch.tv"
IRC_PORT = 6667
WEBSOCKET = "wss://pubsub-edge.twitch.tv/v1"
CLIENT_ID = "kimne78kx3ncx6brgo4mv6wki5h1ko"
DROP_ID = "c2542d6d-cd10-4532-919b-3d19f30a768b"
Expand Down
1 change: 1 addition & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
follow_raid=True, # Follow raid to obtain more points
claim_drops=True, # We can't filter rewards base on stream. Set to False for skip viewing counter increase and you will never obtain a drop reward from this script. Issue #21
watch_streak=True, # If a streamer go online change the priotiry of streamers array and catch the watch screak. Issue #11
join_chat=True, # Join irc chat
bet=BetSettings(
strategy=Strategy.SMART, # Choose you strategy!
percentage=5, # Place the x% of your channel points
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ python-dateutil
emoji
millify
pre-commit
irc
colorama