Fix runtime errors: numpy and libtorrent issues

- Replaced numpy dependency with built-in struct module in gemini client
- Added proper error handling for missing libtorrent in torrent_leech
- Fixed TorrentLeecher initialization to handle missing dependencies gracefully
This commit is contained in:
overspend1
2025-07-25 20:29:50 +02:00
parent 8fe355ed0c
commit be6103bc85
2 changed files with 16 additions and 6 deletions

View File

@@ -1,8 +1,8 @@
import io import io
import logging import logging
import wave import wave
import struct
import numpy as np
from google.genai.client import AsyncClient, Client from google.genai.client import AsyncClient, Client
from google.genai.types import Blob, GenerateContentResponse from google.genai.types import Blob, GenerateContentResponse
from pyrogram.enums import ParseMode from pyrogram.enums import ParseMode
@@ -109,8 +109,10 @@ class Response:
n_samples = len(pcm) // (sample_width * channels) n_samples = len(pcm) // (sample_width * channels)
duration = n_samples / rate duration = n_samples / rate
dtype = {1: np.int8, 2: np.int16, 4: np.int32}[sample_width] # Convert PCM data to samples without numpy
samples = np.frombuffer(pcm, dtype=dtype) format_map = {1: 'b', 2: 'h', 4: 'i'}
format_char = format_map[sample_width]
samples = struct.unpack(f'<{n_samples}{format_char}', pcm[:n_samples * sample_width])
chunk_size = max(1, len(samples) // 80) chunk_size = max(1, len(samples) // 80)
@@ -119,7 +121,7 @@ class Response:
int( int(
min( min(
255, 255,
np.abs(samples[i : i + chunk_size]).mean() sum(abs(samples[i + j]) for j in range(min(chunk_size, len(samples) - i))) / chunk_size
/ (2 ** (8 * sample_width - 1)) / (2 ** (8 * sample_width - 1))
* 255, * 255,
) )

View File

@@ -30,6 +30,11 @@ except ImportError:
class TorrentLeecher: class TorrentLeecher:
def __init__(self): def __init__(self):
if not LIBTORRENT_AVAILABLE or lt is None:
self.session = None
self.active_torrents = {}
return
self.session = lt.session() self.session = lt.session()
self.session.listen_on(6881, 6891) self.session.listen_on(6881, 6891)
self.active_torrents: Dict[str, Dict] = {} self.active_torrents: Dict[str, Dict] = {}
@@ -38,7 +43,7 @@ class TorrentLeecher:
def add_torrent(self, torrent_data: bytes, download_path: Path) -> str: def add_torrent(self, torrent_data: bytes, download_path: Path) -> str:
"""Add torrent to session and return info hash""" """Add torrent to session and return info hash"""
if not LIBTORRENT_AVAILABLE: if not LIBTORRENT_AVAILABLE or self.session is None:
raise Exception("libtorrent not available - install python-libtorrent") raise Exception("libtorrent not available - install python-libtorrent")
info = lt.torrent_info(torrent_data) info = lt.torrent_info(torrent_data)
@@ -139,7 +144,10 @@ class TorrentLeecher:
del self.active_torrents[info_hash] del self.active_torrents[info_hash]
torrent_leecher = TorrentLeecher() try:
torrent_leecher = TorrentLeecher()
except Exception:
torrent_leecher = None
async def download_torrent_file(url: str) -> bytes: async def download_torrent_file(url: str) -> bytes: