Skip to content

Commit

Permalink
The user_id can't be None, double check before return If It's None pl…
Browse files Browse the repository at this point in the history
…ease do a gql request
  • Loading branch information
Tkd-Alex committed Sep 30, 2021
1 parent 5f6c755 commit 3c31015
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
5 changes: 3 additions & 2 deletions TwitchChannelPointsMiner/TwitchChannelPointsMiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,11 @@ def run(self, streamers: list = [], blacklist: list = [], followers=False):
)

# Subscribe to community-points-user. Get update for points spent or gains
user_id = self.twitch.twitch_login.get_user_id()
self.ws_pool.submit(
PubsubTopic(
"community-points-user-v1",
user_id=self.twitch.twitch_login.get_user_id(),
user_id=user_id,
)
)

Expand All @@ -252,7 +253,7 @@ def run(self, streamers: list = [], blacklist: list = [], followers=False):
self.ws_pool.submit(
PubsubTopic(
"predictions-user-v1",
user_id=self.twitch.twitch_login.get_user_id(),
user_id=user_id,
)
)

Expand Down
6 changes: 3 additions & 3 deletions TwitchChannelPointsMiner/classes/Twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,12 @@ def send_minute_watched_events(self, streamers, priority, chunk_size=3):
drop.has_preconditions_met is not False
and drop.is_printable is True
):
# print("=" * 125)
logger.info(
f"{streamers[index]} is streaming {streamers[index].stream}"
)
logger.info(f"Campaign: {campaign}")
logger.info(f"Drop: {drop}")
logger.info(f"{drop.progress_bar()}")
# print("=" * 125)

except requests.exceptions.ConnectionError as e:
logger.error(f"Error while trying to send minute watched: {e}")
Expand Down Expand Up @@ -534,7 +532,9 @@ def __get_campaigns_details(self, campaigns):
}

response = self.post_gql_request(json_data)
result += list(map(lambda x: x["data"]["user"]["dropCampaign"], response))
for r in response:
if r["data"]["user"] is not None:
result.append(r["data"]["user"]["dropCampaign"])
return result

def __sync_campaigns(self, campaigns):
Expand Down
41 changes: 26 additions & 15 deletions TwitchChannelPointsMiner/classes/TwitchLogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,7 @@ def check_login(self):
if self.token is None:
return False

json_data = copy.deepcopy(GQLOperations.ReportMenuItem)
json_data["variables"] = {"channelLogin": self.username}
response = self.session.post(GQLOperations.url, json=json_data)

if response.status_code == 200:
json_response = response.json()
if (
"data" in json_response
and "user" in json_response["data"]
and json_response["data"]["user"]["id"] is not None
):
self.user_id = json_response["data"]["user"]["id"]
self.login_check_result = True

self.login_check_result = self.__set_user_id()
return self.login_check_result

def save_cookies(self, cookies_file):
Expand All @@ -211,6 +198,7 @@ def get_cookie_value(self, key):
if cookie["name"] == key:
if cookie["value"] is not None:
return cookie["value"]
return None

def load_cookies(self, cookies_file):
if os.path.isfile(cookies_file):
Expand All @@ -219,7 +207,30 @@ def load_cookies(self, cookies_file):
raise WrongCookiesException("There must be a cookies file!")

def get_user_id(self):
return int(self.get_cookie_value("persistent").split("%")[0])
persistent = self.get_cookie_value("persistent")
user_id = (
int(persistent.split("%")[0]) if persistent is not None else self.user_id
)
if user_id is None:
if self.__set_user_id() is True:
return self.user_id
return user_id

def __set_user_id(self):
json_data = copy.deepcopy(GQLOperations.ReportMenuItem)
json_data["variables"] = {"channelLogin": self.username}
response = self.session.post(GQLOperations.url, json=json_data)

if response.status_code == 200:
json_response = response.json()
if (
"data" in json_response
and "user" in json_response["data"]
and json_response["data"]["user"]["id"] is not None
):
self.user_id = json_response["data"]["user"]["id"]
return True
return False

def get_auth_token(self):
return self.get_cookie_value("auth-token")
1 change: 0 additions & 1 deletion TwitchChannelPointsMiner/classes/TwitchWebSocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def listen(self, topic, auth_token=None):
data = {"topics": [str(topic)]}
if topic.is_user_topic() and auth_token is not None:
data["auth_token"] = auth_token

nonce = create_nonce()
self.send({"type": "LISTEN", "nonce": nonce, "data": data})

Expand Down
1 change: 0 additions & 1 deletion TwitchChannelPointsMiner/classes/WebSocketsPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ def handle_reconnection(ws):
# Why not create a new ws on the same array index? Let's try.
self = ws.parent_pool
self.ws[ws.index] = self.__new(ws.index) # Create a new connection.
# self.ws[ws.index].topics = ws.topics

self.__start(ws.index) # Start a new thread.
time.sleep(30)
Expand Down

0 comments on commit 3c31015

Please sign in to comment.