-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtelegram_handler.py
More file actions
65 lines (57 loc) · 2.69 KB
/
Copy pathtelegram_handler.py
File metadata and controls
65 lines (57 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging
import os
from typing import Optional, Dict, Any
from pyrogram import Client
from pyrogram.types import Audio
from mutagen.flac import Picture as FLACPicture
from mutagen.id3 import APIC
import mutagen
logger = logging.getLogger(__name__)
class TelegramChannelHandler:
def __init__(self, client: Client, channel_id: int):
self.client = client
self.channel_id = channel_id
def embed_metadata(self, file_path: str, track_meta: Dict[str, Any], art_path: Optional[str]) -> bool:
try:
audio = mutagen.File(file_path, easy=True)
if audio is None: raise ValueError("Mutagen could not recognize audio format.")
audio.delete()
if track_meta.get('title'): audio['title'] = track_meta['title']
if track_meta.get('artist'): audio['artist'] = track_meta['artist']
if track_meta.get('album'): audio['album'] = track_meta['album']
if track_meta.get('date'): audio['date'] = track_meta['date']
audio.save()
audio_full = mutagen.File(file_path)
if art_path and audio_full is not None:
with open(art_path, 'rb') as art_f:
art_data = art_f.read()
if file_path.lower().endswith('.flac'):
pic = FLACPicture()
pic.type = 3; pic.mime = 'image/jpeg'; pic.data = art_data
audio_full.add_picture(pic)
elif file_path.lower().endswith('.mp3'):
audio_full.tags.add(APIC(encoding=3, mime='image/jpeg', type=3, desc='Cover', data=art_data))
audio_full.save()
logger.info(f"Successfully embedded metadata for {os.path.basename(file_path)}")
return True
except Exception as e:
logger.error(f"METADATA EMBEDDING FAILED for {os.path.basename(file_path)}: {e}")
return False
async def upload_file(self, file_path: str, caption: str, track_meta: Dict[str, Any], thumb_path: Optional[str]) -> bool:
file_name = os.path.basename(file_path)
try:
# --- THIS IS THE FIX ---
# Set performer to an empty string to only show the title
await self.client.send_audio(
chat_id=self.channel_id,
audio=file_path,
caption=caption,
thumb=thumb_path,
title=track_meta.get('title', ''),
performer="", # <--- Hides the artist/performer name from display
file_name=file_name
)
return True
except Exception as e:
logger.error(f"Failed to upload {file_name}: {e}")
return False