-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for importing token from browser without needing St…
…reamlabs to be installed
- Loading branch information
Showing
6 changed files
with
193 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ dist/* | |
*/__pycache__/* | ||
.env | ||
__pycache__/* | ||
cookies.json | ||
.venv/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
|
||
import requests | ||
|
||
|
||
class Stream: | ||
def __init__(self, token): | ||
self.s = requests.session() | ||
self.s.headers.update({ | ||
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) StreamlabsDesktop/1.16.7 Chrome/114.0.5735.289 Electron/25.9.3 Safari/537.36", | ||
"authorization": f"Bearer {token}" | ||
}) | ||
|
||
def search(self, game): | ||
if not game: | ||
return [] | ||
url = f"https://streamlabs.com/api/v5/slobs/tiktok/info?category={game}" | ||
info = self.s.get(url).json() | ||
info["categories"].append({"full_name": "Other", "game_mask_id": ""}) | ||
return info["categories"] | ||
|
||
def start(self, title, category): | ||
url = "https://streamlabs.com/api/v5/slobs/tiktok/stream/start" | ||
files=( | ||
('title', (None, title)), | ||
('device_platform', (None, 'win32')), | ||
('category', (None, category)), | ||
) | ||
response = self.s.post(url, files=files).json() | ||
try: | ||
self.id = response["id"] | ||
return response["rtmp"], response["key"] | ||
except KeyError: | ||
return None, None | ||
|
||
def end(self): | ||
url = f"https://streamlabs.com/api/v5/slobs/tiktok/stream/{self.id}/end" | ||
response = self.s.post(url).json() | ||
return response["success"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import gzip | ||
import seleniumwire.undetected_chromedriver as uc | ||
import json | ||
import requests | ||
from urllib.parse import urlparse, parse_qs | ||
import hashlib | ||
import os | ||
import base64 | ||
|
||
class TokenRetriever: | ||
CLIENT_KEY = "awdjaq9ide8ofrtz" | ||
REDIRECT_URI = "https://streamlabs.com/tiktok/auth" | ||
STATE = "" | ||
SCOPE = "user.info.basic,live.room.info,live.room.manage,user.info.profile,user.info.stats" | ||
STREAMLABS_API_URL = "https://streamlabs.com/api/v5/auth/data" | ||
|
||
def __init__(self, cookies_file='cookies.json'): | ||
self.code_verifier = self.generate_code_verifier() | ||
self.code_challenge = self.generate_code_challenge(self.code_verifier) | ||
self.streamlabs_auth_url = ( | ||
f"https://streamlabs.com/m/login?force_verify=1&external=mobile&skip_splash=1&tiktok&code_challenge={self.code_challenge}" | ||
) | ||
self.cookies_file = cookies_file | ||
self.driver = None | ||
|
||
@staticmethod | ||
def generate_code_verifier(): | ||
return base64.urlsafe_b64encode(os.urandom(64)).decode('utf-8').rstrip('=') | ||
|
||
@staticmethod | ||
def generate_code_challenge(code_verifier): | ||
sha256 = hashlib.sha256() | ||
sha256.update(code_verifier.encode('utf-8')) | ||
return base64.urlsafe_b64encode(sha256.digest()).decode('utf-8').rstrip('=') | ||
|
||
def load_cookies(self, driver): | ||
if os.path.exists(self.cookies_file): | ||
with open(self.cookies_file, 'r') as f: | ||
cookies = json.load(f) | ||
for cookie in cookies: | ||
driver.add_cookie(cookie) | ||
|
||
def retrieve_token(self): | ||
self.driver = uc.Chrome() | ||
self.driver.get("https://www.tiktok.com") # Load a page first before setting cookies | ||
self.load_cookies(self.driver) | ||
self.driver.get(self.streamlabs_auth_url) | ||
|
||
try: | ||
request = self.driver.wait_for_request('https://www.tiktok.com/passport/open/web/auth/v2/', timeout=600) | ||
if request: | ||
decompressed_body = gzip.decompress(request.response.body) | ||
response_body = decompressed_body.decode('utf-8') | ||
data = json.loads(response_body) | ||
|
||
redirect_url = data.get('redirect_url') | ||
if redirect_url: | ||
with requests.session() as s: | ||
s.get(redirect_url) | ||
parsed_url = urlparse(redirect_url) | ||
auth_code = parse_qs(parsed_url.query).get('code', [None])[0] | ||
else: | ||
print("No redirect_url found in the response.") | ||
return None | ||
else: | ||
print("No request intercepted or timeout reached.") | ||
return None | ||
|
||
finally: | ||
try: | ||
self.driver.close() | ||
except Exception as e: | ||
print(f"Error closing browser: {e}") | ||
|
||
if auth_code: | ||
try: | ||
import time | ||
token_request_url = f"{self.STREAMLABS_API_URL}?code_verifier={self.code_verifier}&code={auth_code}" | ||
time.sleep(3) # Wait a few seconds before sending the request | ||
response = requests.get(token_request_url).json() | ||
if response["success"]: | ||
return response["data"]["oauth_token"] | ||
except: | ||
print("Failed to obtain token.") | ||
return None | ||
|
||
print("Failed to obtain authorization code.") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
requests>=2.31.0 | ||
pyinstaller>=6.6.0 | ||
setuptools>=69.5.1 | ||
setuptools>=69.5.1 | ||
selenium-wire>=5.1.0 | ||
undetected_chromedriver>=3.5.5 | ||
blinker==1.7.0 |