Skip to content

Commit

Permalink
pubsub to drops for get realt-time update. Create a method for claim …
Browse files Browse the repository at this point in the history
…drops. Insert the game name (if drops are enabled) in minute_watched request. Ref #21
  • Loading branch information
Tkd-Alex committed Jan 28, 2021
1 parent 6e1f74c commit a72a327
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions TwitchChannelPointsMiner/TwitchChannelPointsMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def run(self, streamers: list = [], followers=False):
PubsubTopic(
"community-points-user-v1",
user_id=self.twitch.twitch_login.get_user_id(),
),
PubsubTopic(
"user-drop-events",
user_id=self.twitch.twitch_login.get_user_id(),
)
]
if self.make_predictions is True:
Expand Down
65 changes: 59 additions & 6 deletions TwitchChannelPointsMiner/classes/Twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,25 @@ def login(self):
self.twitch_login.set_token(self.twitch_login.get_auth_token())

def update_minute_watched_event_request(self, streamer):
streamer_info = self.get_stream_info(streamer)
event_properties = {
"channel_id": streamer.channel_id,
"broadcast_id": self.get_broadcast_id(streamer),
"broadcast_id": streamer_info["stream"]["id"],
"player": "site",
"user_id": self.twitch_login.get_user_id(),
}

# First debbuging phase
logger.info(f"{streamer} - Title: {streamer_info['broadcastSettings']['title']}")
if streamer_info['broadcastSettings']['game'] != {}:
logger.info(f"{streamer} - Game: {streamer_info['broadcastSettings']['game']['displayName']}")

tags = [tag['localizedName'] for tag in streamer_info['stream']['tags']]
logger.info(f"{streamer} - Views: {streamer_info['stream']['viewersCount']}, Tags {tags}")
if "c2542d6d-cd10-4532-919b-3d19f30a768b" in [tag['id'] for tag in streamer_info['stream']['tags']]:
logger.info(f"{streamer} - Drops are enabled for this stream! ")
event_properties["game"] = streamer_info['broadcastSettings']['game']['name']

minute_watched = [{"event": "minute-watched", "properties": event_properties}]
json_event = json.dumps(minute_watched, separators=(",", ":"))
streamer.minute_watched_requests = RequestInfo(
Expand Down Expand Up @@ -111,16 +124,36 @@ def get_broadcast_id(self, streamer):
else:
raise StreamerIsOfflineException

def get_stream_info(self, streamer):
json_data = {
"operationName": "VideoPlayerStreamInfoOverlayChannel",
"variables": {
"channel": streamer.username
},
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "a5f2e34d626a9f4f5c0204f910bab2194948a9502089be558bb6e779a9e1b3d2"
}
}
}
response = self.post_gql_request(json_data)
if response["data"]["user"]["stream"] is None:
raise StreamerIsOfflineException
else:
return response["data"]["user"]

def check_streamer_online(self, streamer):
if time.time() < streamer.offline_at + 60:
return

if streamer.is_online is False:
try:
self.update_minute_watched_event_request(streamer)
except StreamerIsOfflineException:
try:
self.update_minute_watched_event_request(streamer)
except StreamerIsOfflineException:
if streamer.is_online is True:
streamer.set_offline()
else:
else:
if streamer.is_online is False:
streamer.set_online()

def claim_bonus(self, streamer, claim_id, less_printing=False):
Expand All @@ -143,6 +176,26 @@ def claim_bonus(self, streamer, claim_id, less_printing=False):
}
self.post_gql_request(json_data)

def claim_drop(self, streamer, drop_id, less_printing=False):
if less_printing is False:
logger.info(
f"Claiming the drop for {streamer}!", extra={"emoji": ":gift:"}
)

json_data = {
"operationName": "DropsPage_ClaimDropRewards",
"variables": {
"input": {"dropInstanceID": drop_id}
},
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "2f884fa187b8fadb2a49db0adc033e636f7b6aaee6e76de1e2bba9a7baf0daf6",
}
},
}
self.post_gql_request(json_data)

# 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 = {
Expand Down
3 changes: 3 additions & 0 deletions TwitchChannelPointsMiner/classes/WebSocketsPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def on_message(ws, message):
ws.last_message_timestamp = message.timestamp
ws.last_message_type_channel = message.identifier

if message.topic == "user-drop-events":
logger.info(f"Drop update: {message.strip()}")

streamer_index = get_streamer_index(ws.streamers, message.channel_id)
if streamer_index != -1:
try:
Expand Down

0 comments on commit a72a327

Please sign in to comment.