-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
78 lines (70 loc) · 2.77 KB
/
app.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
from flask import Flask, request
import json
import re
import newspaper
from gtts import gTTS
import hashlib
from langdetect import detect
app = Flask(__name__, static_url_path='/static', static_folder='static/')
def check_url(url):
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return re.match(regex, url) is not None
@app.route("/")
def homepage():
return "Welcome to BrowsEar!"
@app.route("/text/")
def gettext():
form = """
<form action="/text2speech/" name="textform" method="post">
<textarea id="textforspeech" class="text" cols="86" rows ="20" name="textforspeech"></textarea>
<br><br>
<input type="submit" value="Talk to me" class="submitButton">
</form>
"""
return form
@app.route("/text2speech/", methods=["POST"])
def text2speech():
the_text = request.form['textforspeech']
text_lang = detect(the_text)
try:
tts = gTTS(the_text, lang=text_lang)
except:
return "Sorry. The language of the article is not supported!"
m = hashlib.md5()
m.update(bytes(the_text, "utf-8"))
mp3filename = m.hexdigest()
mp3savepath = f"./static/{mp3filename}.mp3"
tts.save(mp3savepath)
mp3url = f"{request.url_root}static/{mp3filename}.mp3"
mp3href=f'<a href="{mp3url}"> Listen here! </a>'
mp3player=f'<video controls="" autoplay="" name="media"><source src="{mp3url}" type="audio/mpeg"></video>'
return f"{mp3player} <br> <br> <a href='/text/'> Listen other text </a>"
@app.route("/listen/<path:articleurl>")
def listen(articleurl):
if not check_url(articleurl):
return "Error: Wrong url!", 400
article = newspaper.Article(articleurl)
article.download()
article.parse()
text_lang = detect(article.text)
try:
tts = gTTS(article.text, lang=text_lang)
except:
return "Sorry. The language of the article is not supported!"
# generate filename
m = hashlib.md5()
m.update(bytes(articleurl, "utf-8"))
mp3filename = m.hexdigest()
mp3savepath = f"./static/{mp3filename}.mp3"
tts.save(mp3savepath)
mp3url = f"{request.url_root}static/{mp3filename}.mp3"
mp3href=f'<a href="{mp3url}"> Listen here! </a>'
#mp3player=f'<audio controls> <source src="{mp3savepath}" type="audio/mpeg"> </audio>'
mp3player=f'<video controls="" autoplay="" name="media"><source src="{mp3url}" type="audio/mpeg"></video>'
return f"Article url: <a href='{articleurl}'> {article.title} </a> <br> <br> Article text: {article.text} <br> <br> Listen here: <br> {mp3player}"