Skip to content

Commit 37a4adc

Browse files
committed
add sentence_mapper and get existing sentences
1 parent 9e3f334 commit 37a4adc

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ __pycache__/
66
# C extensions
77
*.so
88

9-
# clips
9+
# local data
1010
clips/
11+
sentence_mapper.json
1112

1213
# Distribution / packaging
1314
.Python

src/bot.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99
import os
1010
from random import choice
1111
import asyncio
12+
import json
1213

1314
last = None
1415

15-
bot = commands.Bot(command_prefix=['clip!', 'c!'])
16+
try:
17+
with open('../sentence_mapper.json', 'r') as f:
18+
sentence_mapper = json.load(f)
19+
except:
20+
print('Please create sentence_mapper.json')
21+
exit()
22+
23+
24+
bot = commands.Bot(command_prefix=['clip!', 'c!', '?'])
1625

1726
@bot.event
1827
async def on_ready():
@@ -26,14 +35,17 @@ async def play(ctx, *args):
2635
if ctx.author.voice is None:
2736
print("Connect to voice, idiot.")
2837
return
29-
file = '../clips/' + args[0] + '.mp3'
38+
if args[0] not in sentence_mapper.keys():
39+
print("No this sentence in sentence_mapper")
40+
await ctx.reply("Not this sentence in list")
41+
return
42+
file = '../clips/' + sentence_mapper[args[0]]
3043
if not path.exists(file):
31-
print(os.getcwd())
3244
print("No such file (" + args[0] + ".mp3)")
3345
await ctx.reply(args[0] + " does not exists.")
3446
return
3547
global last
36-
last = args[0]
48+
last = sentence_mapper[args[0]]
3749
user_channel = ctx.author.voice.channel
3850

3951
voice_client = None
@@ -50,6 +62,11 @@ async def play(ctx, *args):
5062
except AttributeError:
5163
pass
5264

65+
@bot.command(aliases=["ls"])
66+
async def get_existing_sentences(ctx):
67+
global sentence_mapper
68+
await ctx.reply("```python\n Existing sentences\n{}```".format('\n'.join([sentence.strip() for sentence in sentence_mapper.keys()])))
69+
5370
@bot.command(aliases=["r"])
5471
async def random(ctx):
5572
r = choice(listdir("../clips"))

url2mp3.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import requests
22
import re
3+
import os
34
from tqdm import tqdm
5+
import json
6+
7+
os.makedirs('clips', exist_ok=True)
48

59
raw_sounds = requests.get('https://prophet-button.netlify.app/')
6-
sounds = re.findall(r'src=\"(.*\.mp3)\"', raw_sounds.text)
10+
extracted_data = re.findall(r'src=\"(.*\.mp3)\".*\">([^\"<]*)<', raw_sounds.text)
11+
12+
sentence_mapper = dict()
13+
for (filename, sentence) in tqdm(extracted_data):
14+
sentence_mapper[sentence.strip()] = filename
15+
r = requests.get(f'https://prophet-button.netlify.app/sound/{filename}')
16+
with open(f'clips/{filename}', 'wb') as f:
17+
f.write(r.content)
718

8-
for sound in tqdm(sounds):
9-
r = requests.get(f'https://prophet-button.netlify.app/sound/{sound}')
10-
with open(f'clips/{sound}', 'wb') as f:
11-
f.write(r.content)
19+
with open('sentence_mapper.json', 'w') as f:
20+
json.dump(sentence_mapper, f)

0 commit comments

Comments
 (0)