-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlaskAPI.py
More file actions
143 lines (128 loc) · 4.09 KB
/
Copy pathFlaskAPI.py
File metadata and controls
143 lines (128 loc) · 4.09 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from flask import Flask
from flask import request
import requests
from bs4 import BeautifulSoup
import json
from urllib.request import Request, urlopen
app = Flask(__name__)
@app.route('/', methods=['GET'])
def get_stock_info():
stock = str(request.args.get('stock')).strip().upper()
range = str(request.args.get('range')).strip()
input_status = "VALID"
#print(stock)
if stock == "NONE" or stock == "":
input_status = "INVALID"
return {'input-status' : input_status}
#ACCESSING STOCK DATA
url = f'https://finance.yahoo.com/quote/{stock}?p={stock}'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/114.0'}
response = requests.get(url=url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
market_price = ""
market_change = ""
market_percentage = ""
for row in soup.find_all('fin-streamer'):
if row["data-field"]=="regularMarketPrice" and row["data-symbol"]==stock:
if row.contents[0].name == 'span':
market_price = row.contents[0].contents[0]
else:
market_price = row.contents[0]
if row["data-field"]=="regularMarketChange" and row["data-symbol"]==stock:
if row.contents[0].name == 'span':
market_change = row.contents[0].contents[0]
else:
market_change = row.contents[0]
if row["data-field"]=="regularMarketChangePercent" and row["data-symbol"]==stock:
if row.contents[0].name == 'span':
market_percentage = row.contents[0].contents[0]
else:
market_percentage = row.contents[0]
#ACCESSING GRAPH DATA
graph_url = ""
valid_ranges = ["1d", "5d", "1mo", "6mo", "1y", "5y", "ytd"]
for rng in valid_ranges:
if rng == range:
interval = ""
if(range == "1d"):
interval = "2m"
if(range == "5d"):
interval = "15m"
if(range == "1mo"):
interval = "1h"
if(range == "6mo"):
interval = "1d"
if(range == "1y"):
interval = "1d"
if(range == "5y"):
interval = "1wk"
if(range == "ytd"):
interval = "3mo"
graph_url = f"https://query1.finance.yahoo.com/v8/finance/chart/{stock}?region=US&lang=en-US&includePrePost=false&interval={interval}&useYfid=true&range={range}&corsDomain=finance.yahoo.com&.tsrc=finance"
if graph_url != "":
data_request = Request(graph_url, headers=headers)
page = urlopen(data_request)
graph_json = page.read().decode("utf-8")
#ACCESSING TABLE DATA
table_json = {}
table_data = soup.find_all('td')
table_headers = [
"Previous Close",
"Open",
"Bid",
"Ask",
"Day's Range",
"52 Week Range",
"Volume",
"Avg. Volume",
"Market Cap",
"Beta (5Y Monthly)",
"PE Ratio (TTM)",
"EPS (TTM)",
"Earnings Date",
"Forward Dividend & Yield",
"Ex-Dividend Date",
"1y Target Est"
]
table_values = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
}
counter = 0
for td in table_data:
if td['class'] == "Ta(end)":
table_values[counter] = td.contents
print(table_values[counter])
counter += 1
#FIX TABLE
#RETURN JSON
return {
'input-status' : input_status,
'url' : url,
'headers' : headers,
'market-price' : market_price,
'market-change' : market_change,
'market-percentage' : market_percentage,
'graph-url' : graph_url,
'interval' : interval,
'range' : range,
'graph-json' : graph_json,
'table-json' : table_json
}
if __name__ == "__main__":
app.run(port=5000)
#app.run(host="192.168.86.84", port=5000)