Skip to content

Commit

Permalink
claim_drops_startup as request in issue comment https://github.com/Tk…
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkd-Alex committed Jan 29, 2021
1 parent 4300213 commit 5308e7c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ twitch_miner = TwitchChannelPointsMiner(
follow_raid=True, # Follow raid to obtain more points
watch_streak=True, # If a streamer go online change the priotiry of streamers array and catch the watch screak. Issue #11
drops_events=True, # If you want to auto claim game drops from Twitch inventory Issue #21
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)
Expand Down
5 changes: 5 additions & 0 deletions TwitchChannelPointsMiner/TwitchChannelPointsMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(
make_predictions: bool = True,
follow_raid: bool = True,
watch_streak: bool = False,
claim_drops_startup: bool = False,
drops_events: bool = False,
logger_settings: LoggerSettings = LoggerSettings(),
browser_settings: BrowserSettings = BrowserSettings(),
Expand All @@ -56,6 +57,7 @@ def __init__(
self.follow_raid = follow_raid
self.watch_streak = watch_streak
self.drops_events = drops_events
self.claim_drops_startup = claim_drops_startup
self.streamers = []
self.events_predictions = {}
self.minute_watcher_thread = None
Expand Down Expand Up @@ -90,6 +92,9 @@ def run(self, streamers: list = [], followers=False):

self.twitch.login()

if self.claim_drops_startup is True:
self.twitch.claim_all_drops_from_inventory()

# Clear streamers array
# Remove duplicate 3. Preserving Order: Use OrderedDict (askpython .com)
streamers = [streamer_name.lower().strip() for streamer_name in streamers]
Expand Down
28 changes: 24 additions & 4 deletions TwitchChannelPointsMiner/classes/Twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import random
import copy
import random

from pathlib import Path

Expand All @@ -20,6 +21,7 @@
StreamerDoesNotExistException,
TimeBasedDropNotFound,
)
from TwitchChannelPointsMiner.utils import get_streamer_index
from TwitchChannelPointsMiner.constants.twitch import CLIENT_ID, API, GQLOperations

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -143,22 +145,40 @@ def claim_bonus(self, streamer, claim_id, less_printing=False):
}
self.post_gql_request(json_data)

def claim_drop(self, streamer, drop_instance_id):
logger.info(f"Claiming the drop for {streamer}!", extra={"emoji": ":package:"})
def claim_drop(self, drop_instance_id, streamer=None):
if streamer is not None:
logger.info(
f"Claiming the drop for {streamer}!", extra={"emoji": ":package:"}
)
else:
logger.info(
f"Startup claim drop {drop_instance_id}", extra={"emoji": ":package:"}
)

json_data = copy.deepcopy(GQLOperations.DropsPage_ClaimDropRewards)
json_data["variables"] = {"input": {"dropInstanceID": drop_instance_id}}
self.post_gql_request(json_data)

def search_drop_in_inventory(self, streamer, drop_id):
response = self.post_gql_request(GQLOperations.Inventory)
inventory = response["data"]["currentUser"]["inventory"]
inventory = self.__get_inventory()
for campaign in inventory["dropCampaignsInProgress"]:
for drop in campaign["timeBasedDrops"]:
if drop["id"] == drop_id:
return drop["self"]
raise TimeBasedDropNotFound

def claim_all_drops_from_inventory(self, streamers):
inventory = self.__get_inventory()
for campaign in inventory["dropCampaignsInProgress"]:
for drop in campaign["timeBasedDrops"]:
if drop["self"]["dropInstanceID"] is not None:
self.claim_drop(drop["dropInstanceID"])
time.sleep(random.uniform(10, 30))

def __get_inventory(self):
response = self.post_gql_request(GQLOperations.Inventory)
return response["data"]["currentUser"]["inventory"]

# Load the amount of current points for a channel, check if a bonus is available
def load_channel_points_context(self, streamer, less_printing=False):
json_data = copy.deepcopy(GQLOperations.ChannelPointsContext)
Expand Down
2 changes: 1 addition & 1 deletion TwitchChannelPointsMiner/classes/WebSocketsPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ def on_message(ws, message):
)
if drop["dropInstanceID"] is not None:
ws.twitch.claim_drop(
ws.streamers[streamer_index],
drop["dropInstanceID"],
ws.streamers[streamer_index],
)
except TimeBasedDropNotFound:
logger.error(
Expand Down
1 change: 1 addition & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
follow_raid=True, # Follow raid to obtain more points
watch_streak=True, # If a streamer go online change the priotiry of streamers array and catch the watch screak. Issue #11
drops_events=True, # If you want to auto claim game drops from Twitch inventory Issue #21
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)
Expand Down

0 comments on commit 5308e7c

Please sign in to comment.