-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstock.py
58 lines (47 loc) · 1.45 KB
/
stock.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
import json
import logging
from pathlib import Path
import requests
import yfinance as yf
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) "
"Gecko/20100101 Firefox/99.0",
}
# Загрузка списка всех акций.
def load_stocks(file_name: str) -> list[dict[str, str]]:
path = Path(f"{Path.cwd()}/{file_name}")
if path.exists():
with path.open() as f:
return json.load(f)
return []
# Проверка на существование акции.
def check_stock(stock_name: str) -> bool:
try:
stock = yf.download(stock_name, period="1mo")
if stock["Close"][-2]:
return True
except Exception as e:
logging.exception(e)
return False
# Сохранение списка акций в json.
def save_stocks(file_name: str, stocks: list):
with Path(f"{Path.cwd()}/{file_name}").open("w") as f:
json.dump(stocks, f)
# Получение списка акций и их сохранение.
def get_all_stocks():
url = (
"https://api.nasdaq.com/api/screener/stocks?"
"tableonly=true&limit=6000&exchange=NASDAQ"
)
stocks = (
requests.get(
url,
headers=HEADERS,
)
.json()
.get("data")
.get("table")
.get("rows")
)
stocks = [{"symbol": i.get("symbol"), "name": i.get("name")} for i in stocks]
save_stocks("stocks.json", stocks)