-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
104 lines (95 loc) · 4.26 KB
/
controller.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
from concurrent import futures
from pathlib import Path
from threading import Lock
from ncm.api import CloudApi
from telegram import Update, Document, Message, InputFile, Audio
from telegram.ext import CallbackContext
import util
api = CloudApi()
executor = futures.ThreadPoolExecutor(max_workers=5)
cacheLock = Lock()
songLock = Lock()
downloadingSongs = dict[str, list[int]]()
song2file = dict[str, dict]()
def download_and_send(update: Update, context: CallbackContext, song_id: str, is_program: bool):
with cacheLock:
if song_id in song2file.keys():
dct = song2file[song_id]
context.bot.send_audio(chat_id=update.effective_chat.id, audio=Audio(
file_id=dct["id"],
file_unique_id=dct["unique_id"],
duration=dct["duration"]
))
return
with songLock:
started = song_id in downloadingSongs.keys()
if started:
if update.effective_chat.id in downloadingSongs[song_id]:
update.message.reply_text(text='已经在下载了...')
return
else:
downloadingSongs[song_id].append(update.effective_chat.id)
else:
downloadingSongs[song_id] = list()
downloadingSongs[song_id].append(update.effective_chat.id)
executor.submit(resolv_and_upload, update, context, song_id, is_program)
def resolv_and_upload(update: Update, context: CallbackContext, song_id: int, is_program: bool):
if song_id not in downloadingSongs.keys():
print("Possibly downloaded song, skip this request.")
return
print(f'Found Song ID: {song_id}')
message = update.message.reply_text(text=f'解析中...')
try:
if is_program:
song_info = api.get_program(song_id)
else:
song_info = util.get_song_info_by_id(song_id)
except Exception as e:
message.edit_text(f"无法获取歌曲信息!{e}")
del downloadingSongs[song_id]
return
if song_info:
message.edit_text(text=f'下载中...')
result: os.path = util.download_song_by_song(song_info, message, is_program)
# message.edit_text(text=f'Downloaded! Uploading now...')
io = Path(result).open('rb')
try:
if is_program:
audio: Message = context.bot.send_audio(chat_id=update.effective_chat.id, audio=InputFile(io)
, title=song_info['name']
, performer=song_info['dj']['nickname']
,
filename=f"{song_info['dj']['nickname']} ~ {song_info['name']}")
else:
audio: Message = context.bot.send_audio(chat_id=update.effective_chat.id, audio=InputFile(io)
, title=song_info['name']
, performer=song_info['artists'][0]['name'],
filename=f"{song_info['artists'][0]['name']} ~ {song_info['name']}")
message.delete()
# broadcast
with cacheLock:
song2file[str(song_id)] = {
"id": audio.audio.file_id,
"unique_id": audio.audio.file_unique_id,
"duration": audio.audio.duration
}
# print(repr(song2file))
# print(audio.audio.file_id)
# print(audio.audio.file_unique_id)
with songLock:
for chat_id in downloadingSongs[str(song_id)]:
if chat_id == update.effective_chat.id:
continue
else:
context.bot.send_audio(chat_id=chat_id, audio=audio.audio)
del downloadingSongs[str(song_id)]
except Exception as e:
io.close()
message.edit_text(f'下载失败. Error: {e}')
del downloadingSongs[str(song_id)]
cache_file: Path = Path(result)
os.remove(cache_file)
else:
message.edit_text(text=f'未找到曲目.')
del downloadingSongs[str(song_id)]