implement cookie support

This commit is contained in:
2024-06-14 15:25:33 +00:00
parent 81cb93e6dc
commit 36a01c5923
2 changed files with 80 additions and 41 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@ venv/
__pycache__/ __pycache__/
.env .env
*.db *.db
cookies.txt

View File

@@ -1,16 +1,55 @@
import os import os
import logging import logging
import vrchatapi from vrchatapi import ApiClient as _ApiClient
from vrchatapi.api import authentication_api, groups_api from vrchatapi import GroupsApi, Configuration, AuthenticationApi
from http.cookiejar import LWPCookieJar
from vrchatapi.exceptions import UnauthorizedException, ApiException from vrchatapi.exceptions import UnauthorizedException, ApiException
from vrchatapi.models.two_factor_auth_code import TwoFactorAuthCode from vrchatapi.models.two_factor_auth_code import TwoFactorAuthCode
from vrchatapi.models.two_factor_email_code import TwoFactorEmailCode from vrchatapi.models.two_factor_email_code import TwoFactorEmailCode
from totp import get_totp from totp import get_totp
# Modded ApiClient with cookie support
# Credit: Katsi on the VRC API Community Discord
class ApiClient(_ApiClient):
def save_cookies(self):
# validate env variable and create cookie jar
filename = os.getenv("COOKIE_FILE")
if filename is not None:
cookie_jar = LWPCookieJar(filename=filename)
else:
raise ValueError("COOKIE_FILE env variable not set")
# load cookies from API client into memory
for cookie in self.rest_client.cookie_jar:
cookie_jar.set_cookie(cookie)
# save cookies
logging.info("Attempting to save cookies")
cookie_jar.save()
def load_cookies(self):
# validate env variable and create cookie jar
filename = os.getenv("COOKIE_FILE")
if filename is not None:
cookie_jar = LWPCookieJar(filename=filename)
else:
raise ValueError("COOKIE_FILE env variable not set")
# load existing cookies if possible, else create a new cookie jar
try:
logging.info("Attempting to load cookies")
cookie_jar.load()
except FileNotFoundError:
logging.info("Attempting to save cookies")
cookie_jar.save()
return
# transfer cookies from our session
for cookie in cookie_jar:
self.rest_client.cookie_jar.set_cookie(cookie)
def get_group_instances(): def get_group_instances():
group_instances = [] group_instances = []
configuration = vrchatapi.Configuration( configuration = Configuration(
# Get username/password from env variables # Get username/password from env variables
username = os.getenv("USER_NAME"), username = os.getenv("USER_NAME"),
password = os.getenv("USER_PASSWORD"), password = os.getenv("USER_PASSWORD"),
@@ -18,41 +57,40 @@ def get_group_instances():
group_id = os.getenv("GROUP_ID") group_id = os.getenv("GROUP_ID")
with vrchatapi.ApiClient(configuration) as api_client: with ApiClient(configuration) as api_client:
# Set our User-Agent as per VRChat Usage Policy and call the Auth API # Set our User-Agent as per VRChat Usage Policy and call the Auth API
api_client.user_agent = "GroupInstanceLogger/0.1alpha me@williamtpeebles.com" api_client.user_agent = "GroupInstanceLogger/0.1alpha me@williamtpeebles.com"
auth_api = authentication_api.AuthenticationApi(api_client)
# Attempt to load cookies
api_client.load_cookies()
auth_api = AuthenticationApi(api_client)
try: try:
# Calling getCurrentUser on Authentication API logs you in if the user isn't already logged in. # Calling getCurrentUser on Authentication API logs you in if the user isn't already logged in.
current_user = auth_api.get_current_user() current_user = auth_api.get_current_user()
api_client.save_cookies() # Save cookies after successful login
except UnauthorizedException as e: except UnauthorizedException as e:
handle_auth_exception(auth_api, e) handle_auth_exception(auth_api, e)
current_user = auth_api.get_current_user() current_user = auth_api.get_current_user()
api_client.save_cookies() # Save cookies after successful login
# Call groups APIs
groups_api_instance = vrchatapi.GroupsApi(api_client)
try:
api_response = groups_api_instance.get_group_instances(group_id) # Get group
for instance in api_response:
group_instances.append(instance)
return group_instances
except ApiException as e:
print("Exception when calling GroupsApi->get_group: %s\n" % e)
logging.info(f"Logged in as: {current_user.display_name}") logging.info(f"Logged in as: {current_user.display_name}")
# Call groups APIs
groups_api_instance = GroupsApi(api_client)
try: try:
api_response = groups_api.GroupsApi(api_client),get_group_instances(group_id) api_response = groups_api_instance.get_group_instances(group_id) # Get group instances
group_instances.extend(api_response) for instance in api_response:
group_instances.append(instance)
except ApiException as e: except ApiException as e:
logging.error(f"Exception when calling GroupsApi->get_group_instances: {e}") logging.error(f"Exception when calling GroupsApi->get_group_instances: {e}")
except Exception as e: except Exception as e:
logging.error(f"Unexpected Error: {e}") logging.error(f"Unexpected Error: {e}")
return group_instances return group_instances
def handle_auth_exception(auth_api, e): def handle_auth_exception(auth_api, e):
if e.status == 200: if e.status == 200:
if "Email 2 Factor Authentication" in e.reason: if "Email 2 Factor Authentication" in e.reason: