-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_music_bot.py
139 lines (109 loc) · 3.81 KB
/
scan_music_bot.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# impoorteren libraries
import os
import json
from steamship import Steamship, TaskState
import telebot
from serpapi import GoogleSearch
import requests
import urllib
import subprocess
import time
from dotenv import load_dotenv
load_dotenv()
bot = telebot.TeleBot(os.getenv("BOT_TOKEN"))
@bot.message_handler(commands=["start", "hello"])
def send_welcome(message):
bot.reply_to(message, "Insert the audio of your song!")
bot.infinity_polling()
@bot.message_handler(content_types=["voice"])
def telegram_bot(message, bot_token=["BOT_TOKEN"]):
# insert audio
file_info = bot.get_file(message.voice.file_id)
# extract telegram's url of the audio
audio_url = "https://api.telegram.org/file/bot{}/{}".format(
bot_token, file_info.file_path
)
bot.infinity_polling()
def convert_oga_to_mp3(audio_url):
file = requests.get(audio_url)
urllib.request.urlretrieve(audio_url, "audio.oga")
subprocess.run(["ffmpeg", "-i", "audio.oga", "audio.mp3"])
data = open("audio.mp3", "rb")
def get_url_mp3(bot_token, message):
bot.reply_to(message, "Searching song...")
data = open("audio.mp3", "rb")
ret_msg = bot.send_voice(message.chat.id, data)
file_info = bot.get_file(ret_msg.voice.file_id)
audio_url = "https://api.telegram.org/file/bot{}/{}".format(
bot_token, file_info.file_path
)
return audio_url
@bot.message_handler(content_types=["voice"])
def telegram_bot(message, bot_token=["BOT_TOKEN"]):
# insert audio
file_info = bot.get_file(message.voice.file_id)
# extract telegram's url of the audio
audio_url = "https://api.telegram.org/file/bot{}/{}".format(
bot_token, file_info.file_path
)
convert_oga_to_mp3(audio_url)
audio_url = get_url_mp3(bot_token, message)
bot.infinity_polling()
# transcribe audio with Steamship's API
def transcribe_audio(audio_url: str, ship: Steamship):
instance = ship.use("audio-markdown", "audio-markdown-crows-v27")
transcribe_task = instance.invoke("transcribe_url", url=audio_url)
task_id = transcribe_task["task_id"]
status = transcribe_task["status"]
retries = 0
while retries <= 100 and status != TaskState.succeeded:
response = instance.invoke("get_markdown", task_id=task_id)
status = response["status"]
if status == TaskState.failed:
print(f"[FAILED] {response}['status_message']")
break
print(f"[Try {retries}] Transcription {status}.")
if status == TaskState.succeeded:
break
time.sleep(2)
retries += 1
markdown = response["markdown"]
return markdown
@bot.message_handler(content_types=["voice"])
def telegram_bot(message, bot_token=os.getenv("BOT_TOKEN")):
client = Steamship(api_key=os.getenv("STEAM_API_KEY"))
...
markdown = transcribe_audio(audio_url, client)
bot.reply_to(message, markdown)
bot.infinity_polling()
## search the trascription on Google using SerpAPI
def search_words(words):
params_serp = {
"q": words,
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"api_key": ["API_KEY"],
}
search = GoogleSearch(params_serp)
results = search.get_dict()
if "organic_results" in results.keys():
if "title" in results["organic_results"][0].keys():
return results["organic_results"][0]["title"]
else:
return ""
else:
return ""
@bot.message_handler(content_types=["voice"])
def telegram_bot(message, bot_token=["BOT_TOKEN"]):
...
client = Steamship(api_key=["STEAM_API_KEY"])
markdown = transcribe_audio(audio_url, client)
bot.reply_to(message, markdown)
result = search_words(markdown)
...
if result == "":
bot.reply_to(message, "Song not found!")
else:
bot.reply_to(message, result)
bot.infinity_polling()