-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (57 loc) · 1.92 KB
/
main.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
import json
from flask import Flask
from flask import jsonify
import requests
from flask import Response
app = Flask(__name__)
@app.route("/quote/<symbol>", methods=['GET'])
def quote(symbol):
url = "https://yfapi.net/v6/finance/quote"
querystring = {
"symbols": symbol,
"region": "PT",
"lang": "en"
}
keyFile = open("apiKey.json")
apiKeyJson=json.load(keyFile)
apiKey=apiKeyJson["apiKey"]
headers = {
'X-API-KEY': apiKey,
'accept': "application/json",
'User-Agent': '',
'Accept-Encoding': '',
'Connection': ''
}
keyFile.close()
response = requests.request("GET", url, headers=headers, params=querystring)
jsonSTR = response.content
data = json.loads(jsonSTR)
if "message" in data:
r = Response(response='Daily request limit reached', status=429, mimetype="application/json")
print(429, 'Daily request limit reached')
return r
else:
quote = data["quoteResponse"]
result = quote["result"]
if len(result) > 0:
dictionary = {}
for i in result:
dictionary = {
"currency": i["currency"],
"shortMarket": i["exchange"],
"market": i["fullExchangeName"],
"shortName": i["shortName"],
"name": i["longName"],
"price": i["regularMarketPrice"],
"symbol": i["symbol"]
}
json_object = json.dumps(dictionary, indent=4)
r = Response(response=json_object, status=200, mimetype="application/json")
return r
else:
print(404, "Symbol not found")
r = Response(response="Symbol not found", status=404, mimetype="application/json")
return r
if __name__ == '__main__':
# run app in debug mode on port 5000
app.run(debug=True, port=5000)