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:
@@ -1,8 +1,8 @@
|
||||
import io
|
||||
import logging
|
||||
import wave
|
||||
import struct
|
||||
|
||||
import numpy as np
|
||||
from google.genai.client import AsyncClient, Client
|
||||
from google.genai.types import Blob, GenerateContentResponse
|
||||
from pyrogram.enums import ParseMode
|
||||
@@ -109,8 +109,10 @@ class Response:
|
||||
n_samples = len(pcm) // (sample_width * channels)
|
||||
duration = n_samples / rate
|
||||
|
||||
dtype = {1: np.int8, 2: np.int16, 4: np.int32}[sample_width]
|
||||
samples = np.frombuffer(pcm, dtype=dtype)
|
||||
# Convert PCM data to samples without numpy
|
||||
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)
|
||||
|
||||
@@ -119,7 +121,7 @@ class Response:
|
||||
int(
|
||||
min(
|
||||
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))
|
||||
* 255,
|
||||
)
|
||||
|
||||
@@ -30,6 +30,11 @@ except ImportError:
|
||||
|
||||
class TorrentLeecher:
|
||||
def __init__(self):
|
||||
if not LIBTORRENT_AVAILABLE or lt is None:
|
||||
self.session = None
|
||||
self.active_torrents = {}
|
||||
return
|
||||
|
||||
self.session = lt.session()
|
||||
self.session.listen_on(6881, 6891)
|
||||
self.active_torrents: Dict[str, Dict] = {}
|
||||
@@ -38,7 +43,7 @@ class TorrentLeecher:
|
||||
|
||||
def add_torrent(self, torrent_data: bytes, download_path: Path) -> str:
|
||||
"""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")
|
||||
|
||||
info = lt.torrent_info(torrent_data)
|
||||
@@ -139,7 +144,10 @@ class TorrentLeecher:
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user