forked from gnpashi/discussion_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscussion_generator.py
More file actions
executable file
·56 lines (43 loc) · 1.24 KB
/
discussion_generator.py
File metadata and controls
executable file
·56 lines (43 loc) · 1.24 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
import requests
import json
from gtts import gTTS
from io import BytesIO
from pygame import mixer
import time
from pynput.keyboard import Key, Listener
print("press 'o' key to generate a new discussion")
print("press 'p' to stop")
mixer.init()
mp3_fp = BytesIO()
def article():
global mp3_fp
mixer.music.unload()
mp3_fp.close()
url = 'https://en.wikipedia.org/api/rest_v1/page/random/summary'
headers = {'user-agent': 'random-discussions/0.0.1'}
response = requests.get(url, headers=headers)
json_response = json.loads(response.text)
mytext = json_response["title"] + ". " + json_response["extract"]
print(mytext)
print("\n")
language = 'en'
tts_result = gTTS(text=mytext, lang=language, slow=False)
mp3_fp = BytesIO()
tts_result.write_to_fp(mp3_fp)
mixer.music.load(mp3_fp, "discussion.mp3")
mixer.music.play()
def on_press(key):
if hasattr(key, 'char'):
if key.char == "o":
article()
if key.char == "p":
mixer.music.stop()
def on_release(key):
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()