Skip to content

Commit f55d37c

Browse files
committed
feat: implement hooks for fake_useragent and tls_client to manage data files and dependencies
1 parent e657308 commit f55d37c

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

KickViewerBOT.spec

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,8 @@ block_cipher = None
55
import os
66
import sys
77

8-
# Try to get tls_client dependencies path
8+
# Note: tls_client binaries are now handled by hooks/hook-tls_client.py
99
tls_client_binaries = []
10-
try:
11-
import tls_client
12-
tls_client_deps = os.path.join(os.path.dirname(tls_client.__file__), 'dependencies')
13-
14-
# Only add binaries that actually exist
15-
potential_binaries = [
16-
'tls-client-32.dll',
17-
'tls-client-64.dll',
18-
'tls-client-amd64.so',
19-
'tls-client-arm64.dylib',
20-
'tls-client-arm64.so',
21-
'tls-client-x86.dylib',
22-
'tls-client-x86.so',
23-
]
24-
25-
for binary in potential_binaries:
26-
binary_path = os.path.join(tls_client_deps, binary)
27-
if os.path.exists(binary_path):
28-
tls_client_binaries.append((binary_path, 'tls_client/dependencies'))
29-
print(f"Found tls_client binary: {binary}")
30-
31-
# Also include the entire dependencies folder as data
32-
if os.path.exists(tls_client_deps):
33-
print(f"Adding tls_client dependencies folder: {tls_client_deps}")
34-
except ImportError as e:
35-
print(f"Warning: Could not import tls_client: {e}")
3610

3711

3812
# Use the current Python environment's site-packages (works for venv and CI)
@@ -44,20 +18,24 @@ datas = [
4418
('backend', '.'),
4519
]
4620

47-
# Add fake_useragent data files if they exist
48-
fake_useragent_data = os.path.join(site_packages, 'fake_useragent', 'data')
49-
if os.path.exists(fake_useragent_data):
50-
datas.append((fake_useragent_data, 'fake_useragent/data'))
51-
52-
# Add tls_client dependencies folder if it exists
21+
# Add fake_useragent data files manually (hooks don't always catch data files)
5322
try:
54-
import tls_client
55-
tls_client_deps_dir = os.path.join(os.path.dirname(tls_client.__file__), 'dependencies')
56-
if os.path.exists(tls_client_deps_dir):
57-
datas.append((tls_client_deps_dir, 'tls_client/dependencies'))
58-
print(f"Added tls_client dependencies to datas: {tls_client_deps_dir}")
59-
except:
60-
pass
23+
import fake_useragent
24+
fake_useragent_base = os.path.dirname(fake_useragent.__file__)
25+
fake_useragent_data = os.path.join(fake_useragent_base, 'data')
26+
27+
if os.path.exists(fake_useragent_data):
28+
# Add the entire data directory with its contents
29+
datas.append((fake_useragent_data, 'fake_useragent/data'))
30+
print(f"[OK] Added fake_useragent data: {fake_useragent_data}")
31+
32+
# Also add py.typed if it exists
33+
py_typed = os.path.join(fake_useragent_base, 'py.typed')
34+
if os.path.exists(py_typed):
35+
datas.append((py_typed, 'fake_useragent'))
36+
37+
except Exception as e:
38+
print(f"[WARNING] Could not add fake_useragent data: {e}")
6139

6240
a = Analysis(
6341
['backend/main.py'],
@@ -103,7 +81,12 @@ a = Analysis(
10381
# Streaming
10482
'streamlink',
10583
'streamlink.plugins',
84+
# fake_useragent and its submodules
10685
'fake_useragent',
86+
'fake_useragent.data',
87+
'fake_useragent.utils',
88+
'fake_useragent.fake',
89+
'fake_useragent.errors',
10790
# TLS Client
10891
'tls_client',
10992
'tls_client.dependencies',
@@ -120,9 +103,9 @@ a = Analysis(
120103
'engineio.async_drivers',
121104
'engineio.async_drivers.gevent',
122105
],
123-
hookspath=[],
106+
hookspath=['hooks'],
124107
hooksconfig={},
125-
runtime_hooks=[],
108+
runtime_hooks=['hooks/runtime-hook-fake_useragent.py'],
126109
excludes=[],
127110
cipher=block_cipher,
128111
noarchive=False,

hooks/hook-fake_useragent.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from PyInstaller.utils.hooks import collect_all
2+
3+
# Collect all data files, submodules, and binaries from fake_useragent
4+
datas, binaries, hiddenimports = collect_all('fake_useragent')

hooks/hook-tls_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from PyInstaller.utils.hooks import collect_all, collect_dynamic_libs
2+
3+
# Collect all data files, submodules, and binaries from tls_client
4+
datas, binaries, hiddenimports = collect_all('tls_client')
5+
6+
# Also collect dynamic libraries
7+
binaries += collect_dynamic_libs('tls_client')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Runtime hook for fake_useragent to fix data file location in PyInstaller builds.
3+
"""
4+
import os
5+
import sys
6+
7+
# In PyInstaller onefile mode, temporary files are extracted to sys._MEIPASS
8+
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
9+
# Patch fake_useragent to look in the correct location
10+
import fake_useragent
11+
12+
# Get the temporary extraction path
13+
meipass = sys._MEIPASS
14+
15+
# Update the data path for fake_useragent
16+
fake_ua_data_dir = os.path.join(meipass, 'fake_useragent', 'data')
17+
18+
# Monkey patch the package path if needed
19+
if os.path.exists(fake_ua_data_dir):
20+
print(f"[PyInstaller] fake_useragent data found at: {fake_ua_data_dir}")
21+
# Store the correct path for fake_useragent to find
22+
fake_useragent.utils.DATA_DIR = fake_ua_data_dir

0 commit comments

Comments
 (0)