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 @@ -145,4 +145,4 @@ chromedriver*
cookies/*
logs/*
screenshots/*
htmls/*
htmls/*
4 changes: 4 additions & 0 deletions TwitchChannelPointsMiner/TwitchChannelPointsMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ def end(self, signum, frame):
if self.twitch_browser is not None:
self.twitch_browser.browser.quit()

for streamer in self.streamers:
if streamer is not None:
Tkd-Alex marked this conversation as resolved.
Show resolved Hide resolved
streamer.leave_chat()

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

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

from TwitchChannelPointsMiner.classes.entities.Chat import Chat

logger = logging.getLogger(__name__)


class ChatSettings(object):
def __init__(
self,
username: str = None,
token: str = None,
):
self.username = username
self.token = token

Tkd-Alex marked this conversation as resolved.
Show resolved Hide resolved

class TwitchChat(Process):
Tkd-Alex marked this conversation as resolved.
Show resolved Hide resolved
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
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


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

# Create IRC bot connection
server = "irc.chat.twitch.tv"
port = 6667
irc.bot.SingleServerIRCBot.__init__(
Tkd-Alex marked this conversation as resolved.
Show resolved Hide resolved
self, [(server, 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)
20 changes: 20 additions & 0 deletions TwitchChannelPointsMiner/classes/entities/Streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from TwitchChannelPointsMiner.classes.entities.Bet import BetSettings
from TwitchChannelPointsMiner.classes.entities.Stream import Stream
from TwitchChannelPointsMiner.classes.Settings import Settings
from TwitchChannelPointsMiner.classes.TwitchChat import ChatSettings, TwitchChat
from TwitchChannelPointsMiner.constants.twitch import URL
from TwitchChannelPointsMiner.utils import _millify

Expand All @@ -18,12 +19,14 @@ def __init__(
claim_drops: bool = None,
watch_streak: bool = None,
bet: BetSettings = None,
chat_client: ChatSettings = None,
Tkd-Alex marked this conversation as resolved.
Show resolved Hide resolved
):
self.make_predictions = make_predictions
self.follow_raid = follow_raid
self.claim_drops = claim_drops
self.watch_streak = watch_streak
self.bet = bet
self.chat_client = chat_client

def default(self):
for name in ["make_predictions", "follow_raid", "claim_drops", "watch_streak"]:
Expand All @@ -48,6 +51,7 @@ def __init__(self, username, settings=None):
self.channel_points = 0
self.minute_watched_requests = None
self.viewer_is_mod = False
self.chat = None

self.stream = Stream()

Expand All @@ -71,6 +75,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!", extra={"emoji": ":sleeping:"})

Expand All @@ -81,6 +86,7 @@ def set_online(self):
self.stream.init_watch_streak()

logger.info(f"{self} is Online!", extra={"emoji": ":partying_face:"})
self.join_chat()

def print_history(self):
return ", ".join(
Expand All @@ -101,3 +107,17 @@ def update_history(self, reason_code, earned):

def stream_up_elapsed(self):
return self.stream_up == 0 or ((time.time() - self.stream_up) > 120)

def leave_chat(self):
if self.chat is not None:
self.chat.terminate()

def join_chat(self):
if self.settings.chat_client is not None:
self.chat = TwitchChat(
self.settings.chat_client.username,
self.settings.chat_client.token,
self.username,
)
logger.info(f"Connecting to {self.username}'s chat!")
self.chat.start()
105 changes: 57 additions & 48 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,66 @@
from TwitchChannelPointsMiner.classes.entities.Bet import Strategy, BetSettings
from TwitchChannelPointsMiner.classes.entities.Streamer import Streamer, StreamerSettings
from TwitchChannelPointsMiner.classes.TwitchBrowser import Browser, BrowserSettings
from TwitchChannelPointsMiner.classes.TwitchChat import TwitchChat, ChatSettings

twitch_miner = TwitchChannelPointsMiner(
username="your-twitch-username",
claim_drops_startup=False, # If you want to auto claim all drops from Twitch inventory on startup
logger_settings=LoggerSettings(
save=True, # If you want to save logs in file (suggested)
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 to print emoji. Set to false if you have a problem
less=False # If you think that the logs are too much verborse set this to True
),
browser_settings=BrowserSettings(
browser=Browser.FIREFOX, # Choose if you want to use Chrome or Firefox as browser
show=False, # Show the browser during bet else headless mode
do_screenshot=False, # Do screenshot during the bet
),
streamer_settings=StreamerSettings(
make_predictions=True, # If you want to Bet / Make prediction
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
bet=BetSettings(
strategy=Strategy.SMART, # Choose you strategy!
percentage=5, # Place the x% of your channel points
percentage_gap=20, # Gap difference between outcomesA and outcomesB (for SMART stragegy)
max_points=50000, # If the x percentage of your channel points is gt bet_max_points set this value
def main():
twitch_miner = TwitchChannelPointsMiner(
username="your-twitch-username",
claim_drops_startup=False, # If you want to auto claim all drops from Twitch inventory on startup
logger_settings=LoggerSettings(
save=True, # If you want to save logs in file (suggested)
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 to print emoji. Set to false if you have a problem
less=False # If you think that the logs are too much verborse set this to True
),
browser_settings=BrowserSettings(
browser=Browser.FIREFOX, # Choose if you want to use Chrome or Firefox as browser
show=False, # Show the browser during bet else headless mode
do_screenshot=False, # Do screenshot during the bet
),
streamer_settings=StreamerSettings(
make_predictions=True, # If you want to Bet / Make prediction
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
bet=BetSettings(
strategy=Strategy.SMART, # Choose you strategy!
percentage=5, # Place the x% of your channel points
percentage_gap=20, # Gap difference between outcomesA and outcomesB (for SMART stragegy)
max_points=50000, # If the x percentage of your channel points is gt bet_max_points set this value
),
chat_client=ChatSettings( # Can be removed for mining channelpoints only
username="your-twitch-username",
token="oauth-token-without-prefix", # generate on https://twitchapps.com/tmi/ - without "oauth:" prefix!
)
)
)
)

# You can customize the settings for each streamer. If not settings was provided the script will use the streamer_settings from TwitchChannelPointsMiner.
# If no streamer_settings provided in TwitchChannelPointsMiner the script will use default settings.
# The streamers array can be a String -> username or Streamer instance.
# You can customize the settings for each streamer. If not settings was provided the script will use the streamer_settings from TwitchChannelPointsMiner.
# If no streamer_settings provided in TwitchChannelPointsMiner the script will use default settings.
# The streamers array can be a String -> username or Streamer instance.

# The settings priority are: settings in mine function, settings in TwitchChannelPointsMiner instance, default settings.
# For example if in the mine function you don't provide any value for 'make_prediction' but you have set it on TwitchChannelPointsMiner instance the script will take the value from here.
# If you haven't set any value even in the instance the default one will be used
# The settings priority are: settings in mine function, settings in TwitchChannelPointsMiner instance, default settings.
# For example if in the mine function you don't provide any value for 'make_prediction' but you have set it on TwitchChannelPointsMiner instance the script will take the value from here.
# If you haven't set any value even in the instance the default one will be used

twitch_miner.mine(
[
Streamer("streamer-username01", settings=StreamerSettings(make_predictions=True , follow_raid=False , claim_drops=True , watch_streak=True , bet=BetSettings(strategy=Strategy.SMART , percentage=5 , percentage_gap=20 , max_points=234 ) )),
Streamer("streamer-username02", settings=StreamerSettings(make_predictions=False , follow_raid=True , claim_drops=False , bet=BetSettings(strategy=Strategy.PERCENTAGE , percentage=5 , percentage_gap=20 , max_points=1234 ) )),
Streamer("streamer-username03", settings=StreamerSettings(make_predictions=True , follow_raid=False , watch_streak=True , bet=BetSettings(strategy=Strategy.SMART , percentage=5 , percentage_gap=30 , max_points=50000 ) )),
Streamer("streamer-username04", settings=StreamerSettings(make_predictions=False , follow_raid=True , watch_streak=True )),
Streamer("streamer-username05", settings=StreamerSettings(make_predictions=True , follow_raid=True , claim_drops=True , watch_streak=True , bet=BetSettings(strategy=Strategy.HIGH_ODDS , percentage=7 , percentage_gap=20 , max_points=90 ) )),
Streamer("streamer-username06"),
Streamer("streamer-username07"),
Streamer("streamer-username08"),
"streamer-username09",
"streamer-username10",
"streamer-username11"
], # Array of streamers (order = priority)
followers=False # Automatic download the list of your followers (unable to set custom settings for you followers list)
)
twitch_miner.mine(
[
Streamer("streamer-username01", settings=StreamerSettings(make_predictions=True , follow_raid=False , claim_drops=True , watch_streak=True , bet=BetSettings(strategy=Strategy.SMART , percentage=5 , percentage_gap=20 , max_points=234 ) )),
Streamer("streamer-username02", settings=StreamerSettings(make_predictions=False , follow_raid=True , claim_drops=False , bet=BetSettings(strategy=Strategy.PERCENTAGE , percentage=5 , percentage_gap=20 , max_points=1234 ) )),
Streamer("streamer-username03", settings=StreamerSettings(make_predictions=True , follow_raid=False , watch_streak=True , bet=BetSettings(strategy=Strategy.SMART , percentage=5 , percentage_gap=30 , max_points=50000 ) )),
Streamer("streamer-username04", settings=StreamerSettings(make_predictions=False , follow_raid=True , watch_streak=True )),
Streamer("streamer-username05", settings=StreamerSettings(make_predictions=True , follow_raid=True , claim_drops=True , watch_streak=True , bet=BetSettings(strategy=Strategy.HIGH_ODDS , percentage=7 , percentage_gap=20 , max_points=90 ) )),
Streamer("streamer-username06"),
Streamer("streamer-username07"),
Streamer("streamer-username08"),
"streamer-username09",
"streamer-username10",
"streamer-username11"
], # Array of streamers (order = priority)
followers=False # Automatic download the list of your followers (unable to set custom settings for you followers list)
)

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ python-dateutil
emoji
millify
pre-commit
irc