-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.py
60 lines (48 loc) · 1.68 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
from flask import Flask, request, jsonify, send_file
from deep_translator import GoogleTranslator
import requests
app = Flask(__name__)
def translate_text(text):
translator = GoogleTranslator(source='en', target='vi')
return translator.translate(text)
@app.route('/')
def index():
return send_file('index.html')
@app.route('/translate', methods=['POST'])
def translate():
data = request.json
text = data.get('text', '')
translation = translate_text(text)
return jsonify({'translation': translation})
@app.route('/useless-fact')
def useless_fact():
response = requests.get('https://uselessfacts.jsph.pl/random.json?language=en')
data = response.json()
return jsonify({'fact': data['text']})
@app.route('/number-fact')
def number_fact():
number = request.args.get('number', 'random')
response = requests.get(f'http://numbersapi.com/{number}')
return jsonify({'fact': response.text})
@app.route('/cat-fact')
def cat_fact():
response = requests.get('https://catfact.ninja/fact')
data = response.json()
return jsonify({'fact': data['fact']})
@app.route('/joke')
def joke():
response = requests.get('https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,sexist,explicit')
data = response.json()
if data['type'] == 'single':
joke = data['joke']
else:
joke = f"{data['setup']} {data['delivery']}"
return jsonify({'joke': joke})
@app.route('/dog-image')
def dog_image():
response = requests.get('https://dog.ceo/api/breeds/image/random')
data = response.json()
return jsonify({'image_url': data['message']})
if __name__ == '__main__':
app.run(debug=True)
app = app.wsgi_app