Skip to content

Commit

Permalink
modified: .github/workflows/ci.yml
Browse files Browse the repository at this point in the history
	modified:   twitch_helix_client.py
  • Loading branch information
Revulate committed Oct 16, 2024
1 parent c75aa6d commit 3dddb66
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
24 changes: 10 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ jobs:
- name: Set up Python Virtual Environment and Install Dependencies
run: |
if [ ! -d "venv" ] && [ ! -d "env" ]; then
if [ ! -d "venv" ]; then
python3.11 -m venv venv
fi
if [ -d "venv" ]; then
source venv/bin/activate
elif [ -d "env" ]; then
source env/bin/activate
fi
source venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
Expand Down Expand Up @@ -69,15 +65,10 @@ jobs:
git clone --branch main $REPO_URL .
fi
if [ ! -d "venv" ] && [ ! -d "env" ]; then
if [ ! -d "venv" ]; then
python3.11 -m venv venv
fi
if [ -d "venv" ]; then
source venv/bin/activate
elif [ -d "env" ]; then
source env/bin/activate
fi
source venv/bin/activate
pip install -r requirements.txt
# Create .env file (using existing secrets)
Expand All @@ -103,11 +94,14 @@ jobs:
NUULS_API_KEY=${{ secrets.NUULS_API_KEY }}
GOOGLE_SHEET_ID=${{ secrets.GOOGLE_SHEET_ID }}
GOOGLE_CREDENTIALS_FILE=credentials.json
TOKEN_EXPIRY=${{ secrets.TOKEN_EXPIRY }}
EOF
# Ensure correct file permissions for credentials.json
echo "$GOOGLE_CREDENTIALS_JSON" > credentials.json
chmod 600 credentials.json
# Install Node.js and PM2 if not present
if ! command -v node &> /dev/null; then
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
Expand All @@ -117,6 +111,7 @@ jobs:
sudo npm install -g pm2
fi
# Create or update PM2 ecosystem config
cat <<EOF > ecosystem.config.js
module.exports = {
apps: [{
Expand All @@ -143,7 +138,8 @@ jobs:
python migrate.py
fi
pm2 start ecosystem.config.js --update-env
# Start or restart the bot using PM2
pm2 start ecosystem.config.js --update-env || pm2 restart ecosystem.config.js --update-env
pm2 save
sudo pm2 startup systemd -u $USER --hp $HOME
Expand Down
38 changes: 22 additions & 16 deletions twitch_helix_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ def load_tokens(self):
self.token_expiry = datetime.datetime.fromisoformat(expiry) if expiry else None

def save_tokens(self):
set_key(".env", "ACCESS_TOKEN", self.oauth_token)
set_key(".env", "REFRESH_TOKEN", self.refresh_token)
set_key(".env", "TOKEN_EXPIRY", self.token_expiry.isoformat() if self.token_expiry else "")
# Ensure there are no trailing newlines or extra quotes
access_token = self.oauth_token.strip()
refresh_token = self.refresh_token.strip()
expiry = self.token_expiry.isoformat() if self.token_expiry else ""

set_key(".env", "ACCESS_TOKEN", access_token)
set_key(".env", "REFRESH_TOKEN", refresh_token)
set_key(".env", "TOKEN_EXPIRY", expiry)

async def get_authorization_url(self, scopes):
state = os.urandom(16).hex()
Expand Down Expand Up @@ -87,20 +92,21 @@ async def refresh_oauth_token(self):
"grant_type": "refresh_token",
"refresh_token": self.refresh_token,
}
await self.ensure_session()
try:
async with self.session.post(self.TOKEN_URL, data=params) as response:
if response.status == 200:
data = await response.json()
self.oauth_token = data["access_token"]
self.refresh_token = data.get("refresh_token", self.refresh_token)
self.token_expiry = datetime.datetime.now() + datetime.timedelta(seconds=data["expires_in"])
self.save_tokens()
log_info("OAuth token refreshed successfully")
return True
else:
log_error(f"Failed to refresh token: {await response.text()}")
return False
async with aiohttp.ClientSession() as session:
async with session.post(self.TOKEN_URL, data=params) as response:
if response.status == 200:
data = await response.json()
self.oauth_token = data["access_token"]
self.refresh_token = data.get("refresh_token", self.refresh_token)
self.token_expiry = datetime.datetime.now() + datetime.timedelta(seconds=data["expires_in"])
self.save_tokens()
load_dotenv(override=True) # Force reloading updated tokens
log_info("OAuth token refreshed successfully")
return True
else:
log_error(f"Failed to refresh token: {await response.text()}")
return False
except Exception as e:
log_error(f"Error during token refresh: {e}", exc_info=True)
return False
Expand Down

0 comments on commit 3dddb66

Please sign in to comment.