-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahoo_fiance.py
143 lines (116 loc) · 4.09 KB
/
yahoo_fiance.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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- coding: iso-8859-1 -*-. iso-8859-1
###### Import ########
import json
import re
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from enum import Enum
from pprint import pprint
from time import mktime
###### Variable ######
###### Function ######
###### Program #######
class YahooFinance:
SCRAPE_URL = "https://finance.yahoo.com/quote/"
BASE_URL = "https://query1.finance.yahoo.com/v7/finance"
USER_AGENT = "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11"
def __init__(self, stock):
self.stock = stock
self.header, self.crumb, self.cookies = self._get_cookie_crumb(self.stock)
def _get_cookie_crumb(self, stock):
url = f"{self.SCRAPE_URL}{stock}/history"
with requests.session():
header = {
'Connection': 'keep-alive',
'Expires': '-1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': self.USER_AGENT
}
website = requests.get(url, headers=header)
soup = BeautifulSoup(website.text, 'lxml')
crumb = re.findall('"CrumbStore":{"crumb":"(.+?)"}', str(soup))
return (header, crumb[0], website.cookies)
def convert_to_unix(self, date):
datum = datetime.strptime(date, '%d-%m-%Y')
return int(mktime(datum.timetuple()))
def load_csv_data(self, interval='1d', day_begin='01-02-2010', day_end='28-03-2020'):
day_begin_unix = self.convert_to_unix(day_begin)
day_end_unix = self.convert_to_unix(day_end)
url = f"{self.BASE_URL}/download/{self.stock}"
params = {
"period1": day_begin_unix,
"period2": day_end_unix,
"interval": interval,
"events": "history",
"crumb": self.crumb
}
website = requests.get(url, headers=self.header, cookies=self.cookies, params=params)
return website.text.split('\n')[:-1]
def quoteResponse(self):
url = f"{self.BASE_URL}/quote"
params = {
"symbols" : self.stock
}
website = requests.get(url, headers=self.header, cookies=self.cookies, params=params)
return website.json()
def optionChain(self):
url = f"{self.BASE_URL}/options/{self.stock}"
website = requests.get(url, headers=self.header, cookies=self.cookies)
return website.json()
def chart(self, params):
url = f"{self.BASE_URL}/chart/{self.stock}"
website = requests.get(url, headers=self.header, cookies=self.cookies, params=params.value)
return website.json()
def setStock(stock):
self.stock = stock
self.header, self.crumb, self.cookies = self._get_cookie_crumb(self.stock)
class ChartYahooFinance(Enum):
HistoricalPricesDaily = {
"range": "2y",
"interval": "1d",
"indicators": "quote",
"includeTimestamps": "true",
}
HistoricalPricesWeekly = {
"range": "5y",
"interval": "1wk",
"indicators": "quote",
"includeTimestamps": "true"
}
HistoricalPricesMonthly = {
"range": "max",
"interval": "1mo",
"indicators": "quote",
"includeTimestamps": "true"
}
IntradayPrices1m = {
"range": "1d",
"interval": "1m",
"indicators": "quote",
"includeTimestamps": "true"
}
IntradayPrices5m = {
"range": "5d",
"interval": "5m",
"indicators": "quote",
"includeTimestamps": "true"
}
IntradayPrices15m = {
"range": "5d",
"interval": "15m",
"indicators": "quote",
"includeTimestamps": "true"
}
IntradayPrices60m = {
"range": "1mo",
"interval": "60m",
"indicators": "quote",
"includeTimestamps": "true"
}
if __name__ == "__main__":
yt = YahooFinance("FB")
pprint(yt.chart(ChartYahooFinance.IntradayPrices1m))
print(ChartYahooFinance.IntradayPrices1m)