Skip to content

Commit

Permalink
Merge pull request #59 from LeagueOfPoro/stability
Browse files Browse the repository at this point in the history
fix: Stability improvements [BREAKING CHANGE]
  • Loading branch information
LeagueOfPoro authored Feb 5, 2023
2 parents 5bc0ebc + 92e23bf commit 57ce997
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 49 deletions.
45 changes: 23 additions & 22 deletions Browser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from Exceptions.NoAccessTokenException import NoAccessTokenException
from Exceptions.RateLimitException import RateLimitException
from Match import Match
import cloudscraper
from pprint import pprint
from bs4 import BeautifulSoup
from datetime import datetime
import threading
from time import sleep
from time import sleep, time
from Config import Config
from Exceptions.StatusCodeAssertException import StatusCodeAssertException
import pickle
from pathlib import Path
import jwt


class Browser:
Expand Down Expand Up @@ -96,24 +98,18 @@ def login(self, username: str, password: str, refreshLock) -> bool:
"Referrer": "https://lolesports.com"}

# This requests sometimes returns 404
for i in range(5):
resAccessToken = self.client.get(
"https://account.rewards.lolesports.com/v1/session/token", headers=headers)
if resAccessToken.status_code == 200:
break
else:
sleep(1)

resAccessToken = self.client.get(
"https://account.rewards.lolesports.com/v1/session/token", headers=headers)
# Currently unused but the call might be important server-side
resPasToken = self.client.get(
"https://account.rewards.lolesports.com/v1/session/clientconfig/rms", headers=headers)
if resAccessToken.status_code == 200:
self.maintainSession()
#self.maintainSession()
self.__dumpCookies()
return True
return False

def refreshTokens(self):
def refreshSession(self):
"""
Refresh access and entitlement tokens
"""
Expand All @@ -130,17 +126,11 @@ def refreshTokens(self):

def maintainSession(self):
"""
Periodically maintain the session by refreshing the tokens
Periodically maintain the session by refreshing the access_token
"""
self.refreshTimer = threading.Timer(
Browser.SESSION_REFRESH_INTERVAL, self.refreshTokens)
self.refreshTimer.start()

def stopMaintaininingSession(self):
"""
Stops refreshing the tokens
"""
self.refreshTimer.cancel()
if self.__needSessionRefresh():
self.log.debug("Refreshing session.")
self.refreshSession()

def getLiveMatches(self):
"""
Expand Down Expand Up @@ -196,10 +186,21 @@ def checkNewDrops(self, lastCheckTime):
res = self.client.get("https://account.service.lolesports.com/fandom-account/v1/earnedDrops?locale=en_GB&site=LOLESPORTS", headers=headers)
resJson = res.json()
return [drop for drop in resJson if lastCheckTime <= drop["unlockedDateMillis"]]
except KeyError:
except (KeyError, TypeError):
self.log.debug("Drop check failed")
return []

def __needSessionRefresh(self) -> bool:
if "access_token" not in self.client.cookies.get_dict():
raise NoAccessTokenException()

res = jwt.decode(self.client.cookies.get_dict()["access_token"], options={"verify_signature": False})
timeLeft = res['exp'] - int(time())
self.log.debug(f" s until session expires.")
if timeLeft < 600:
return True
return False

def __sendWatch(self, match: Match) -> object:
"""
Sends watch event for a match
Expand Down
5 changes: 5 additions & 0 deletions Exceptions/NoAccessTokenException.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from Exceptions.CapsuleFarmerEvolvedException import CapsuleFarmerEvolvedException

class NoAccessTokenException(CapsuleFarmerEvolvedException):
def __init__(self):
super().__init__(f"There's no authentication access_token in cookies.")
13 changes: 6 additions & 7 deletions FarmThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ def run(self):
self.stats.updateStatus(self.account, "[green]LIVE")
self.stats.resetLoginFailed(self.account)
while True:
self.browser.maintainSession()
self.browser.getLiveMatches()
dropsAvailable = self.browser.sendWatchToLive()
self.browser.sendWatchToLive()
newDrops = []
if self.browser.liveMatches:
liveMatchesStatus = []
for m in self.browser.liveMatches.values():
# Color code "drops available"
# status = dropsAvailable.get(m.league, False)
# if status:
# liveMatchesStatus.append(f"[green]{m.league}[/]")
# else:
liveMatchesStatus.append(f"{m.league}")
self.log.debug(f"{', '.join(liveMatchesStatus)}")
liveMatchesMsg = f"{', '.join(liveMatchesStatus)}"
Expand All @@ -60,8 +56,11 @@ def run(self):
sleep(Browser.STREAM_WATCH_INTERVAL)
else:
self.log.error(f"Login for {self.account} FAILED!")
self.stats.updateStatus(self.account, "[red]LOGIN FAILED")
self.stats.addLoginFailed(self.account)
if self.stats.getFailedLogins(self.account) < 3:
self.stats.updateStatus(self.account, "[red]LOGIN FAILED - WILL RETRY SOON")
else:
self.stats.updateStatus(self.account, "[red]LOGIN FAILED")
except Exception:
self.log.exception(f"Error in {self.account}. The program will try to recover.")

Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ httpx = {extras = ["http2"], version = "*"}
beautifulsoup4 = "*"
pyyaml = "*"
rich = "*"
pyjwt = "*"

[dev-packages]
autopep8 = "*"
Expand Down
57 changes: 37 additions & 20 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import argparse
from rich import print
from pathlib import Path
from time import sleep

from Stats import Stats
from VersionManager import VersionManager


CURRENT_VERSION = 1.2

def init() -> tuple[Logger, Config]:
Expand Down Expand Up @@ -57,6 +59,9 @@ def main(log: Logger, config: Config):
for account in config.accounts:
if account not in farmThreads:
if stats.getFailedLogins(account) < 3:
if stats.getFailedLogins(account) > 0:
log.debug("Sleeping {account} before retrying login.")
sleep(30)
log.info(f"Starting a thread for {account}.")
thread = FarmThread(log, config, account, stats, locks)
thread.daemon = True
Expand Down

0 comments on commit 57ce997

Please sign in to comment.