-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
45 lines (36 loc) · 2.12 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
from flask import Flask, jsonify
from flask_restx import Api, Resource
from forex_scraper.fetch_html_source import fetch_html_source
from forex_scraper.forex_scraper import ForexScraper
from selenium.common.exceptions import TimeoutException
app = Flask(__name__)
api = Api(app, version='1.0.0', title='Forex Rest API', description='API that scrapes latest infomation from '
'https://www.investing.com/currencies/single'
'-currency-crosses about following '
'currencies:\n1. EUR/USD\n2. USD/EUR\n3. '
'EUR/JYP\n4. EUR/GBP\n5. EUR/AUD\n6. EUR/CAD\n7. '
'EUR/CHF\n8. EUR/SEK\n9. EUR/NZD\n10. '
'EUR/INR\n\nIt scrapes following information:\n- '
'Title\n- Bid\n- Ask\n- High\n- Low\n- Change\n- '
'Change Percent\n- Timestamp')
@api.route('/api/forexInfo/')
class ForexInfo(Resource):
def get(self):
"""Handles GET requests to the API
Returns:
JSON: Returns json response to the GET requests
"""
try:
forex = ForexScraper()
except NotADirectoryError:
return jsonify({'Message': 'Please make sure you have chromedriver installed and the path to the driver is'
' correct'})
except TimeoutException:
return jsonify({'Message': 'Request timeout (Invensting.com servers took too much time to respond with '
'requested information)'})
except:
return jsonify({'Message': 'Something went wrong'})
return jsonify(forex.get_info())
api.add_resource(ForexInfo, '/api/forexInfo/')
if __name__ == '__main__':
app.run()