Semi-PEP8
Make it PEP 8 compliant where it does not break the API
This commit is contained in:
parent
c2341d8989
commit
f506c44cdf
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#stockquotes - Python module to pull stock quotes from Yahoo! Finance
|
#stockquotes - Python module to pull stock quotes from Yahoo! Finance
|
||||||
#Copyright 2019 ScoopGracie. All rights reversed.
|
#Copyright 2020 ScoopGracie. All rights reversed.
|
||||||
#This is free and unencumbered software released into the public domain.
|
#This is free and unencumbered software released into the public domain.
|
||||||
|
|
||||||
#Anyone is free to copy, modify, publish, use, compile, sell, or
|
#Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||||
|
@ -25,52 +25,71 @@
|
||||||
#OTHER DEALINGS IN THE SOFTWARE.
|
#OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
from bs4 import BeautifulSoup as bs
|
from bs4 import BeautifulSoup as bs
|
||||||
import requests, datetime
|
import requests
|
||||||
|
import datetime
|
||||||
|
|
||||||
class StockDoesNotExistError(Exception):
|
class StockDoesNotExistError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class NetworkError(Exception):
|
class NetworkError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Stock:
|
class Stock:
|
||||||
def __init__(self, ticker):
|
def __init__(self, ticker):
|
||||||
try:
|
try:
|
||||||
r=requests.get('https://finance.yahoo.com/quote/' + ticker + '/history', headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3941.4 Safari/537.36'})
|
r=requests.get(
|
||||||
except:
|
'https://finance.yahoo.com/quote/{}/history'\
|
||||||
raise NetworkError()
|
.format(ticker),
|
||||||
if r.status_code is 302:
|
headers={
|
||||||
raise StockDoesNotExistError(ticker)
|
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64)' +
|
||||||
try:
|
' AppleWebKit/537.36 (KHTML, like Gecko)' +
|
||||||
soup = bs(r.text, features="lxml")
|
' Chrome/79.0.3941.4 Safari/537.36'
|
||||||
self.symbol = soup.h1.string.split('(')[1].split(')')[0]
|
})
|
||||||
self.name = soup.h1.string.split('(')[0].strip()
|
except:
|
||||||
rows = soup.table.tbody.find_all('tr')
|
raise NetworkError()
|
||||||
self.historical=[]
|
if r.status_code is 302:
|
||||||
for i in rows:
|
raise StockDoesNotExistError(ticker)
|
||||||
row=i.find_all('td')
|
try:
|
||||||
try:
|
soup = bs(r.text, features="lxml")
|
||||||
parsed = {
|
self.symbol = soup.h1.string.split('(')[1].split(')')[0]
|
||||||
"date" : datetime.datetime.strptime(
|
self.name = soup.h1.string.split('(')[0].strip()
|
||||||
row[0].span.string,
|
rows = soup.table.tbody.find_all('tr')
|
||||||
'%b %d, %Y'
|
self.historical=[]
|
||||||
),
|
for i in rows:
|
||||||
"open" : float(row[1].span.string.replace(',', '')),
|
row=i.find_all('td')
|
||||||
"high" : float(row[2].span.string.replace(',', '')),
|
try:
|
||||||
"low" : float(row[3].span.string.replace(',', '')),
|
parsed = {
|
||||||
"close" : float(row[4].span.string.replace(',', '')),
|
"date" : datetime.datetime.strptime(
|
||||||
"adjClose": float(row[5].span.string.replace(',', '')),
|
row[0].span.string,
|
||||||
"volume" : int(row[6].span.string.replace(',', ''))
|
'%b %d, %Y'
|
||||||
}
|
),
|
||||||
except:
|
"open" : float(row[1].span.string.replace(',', '')),
|
||||||
continue
|
"high" : float(row[2].span.string.replace(',', '')),
|
||||||
self.historical.append(parsed)
|
"low" : float(row[3].span.string.replace(',', '')),
|
||||||
topData = soup.find(id='quote-header-info')
|
"close" : float(row[4].span.string.replace(',', '')),
|
||||||
try:
|
"adjClose": float(row[5].span.string.replace(',', '')),
|
||||||
self.currentPrice = float(topData.findAll('span')[11].string.replace(',', ''))
|
"volume" : int(row[6].span.string.replace(',', ''))
|
||||||
rawChange = topData.findAll('span')[12].string
|
}
|
||||||
except IndexError:
|
except:
|
||||||
self.currentPrice = float(topData.findAll('span')[3].string.replace(',', ''))
|
continue
|
||||||
rawChange = topData.findAll('span')[4].string
|
self.historical.append(parsed)
|
||||||
|
topData = soup.find(id='quote-header-info')
|
||||||
self.increaseDollars = float(rawChange.split(' ')[0].replace(',', ''))
|
try:
|
||||||
self.increasePercent = float(rawChange.split(' ')[1].replace(',', '').replace('(', '').replace(')', '').replace('%', ''))
|
self.currentPrice = float(
|
||||||
except AttributeError as error:
|
topData.findAll('span')[11].string.replace(',', ''))
|
||||||
raise StockDoesNotExistError(ticker) from error
|
rawChange = topData.findAll('span')[12].string
|
||||||
|
except IndexError:
|
||||||
|
self.currentPrice = float(
|
||||||
|
topData.findAll('span')[3].string.replace(',', ''))
|
||||||
|
rawChange = topData.findAll('span')[4].string
|
||||||
|
|
||||||
|
self.increaseDollars = float(
|
||||||
|
rawChange.split(' ')[0].replace(',', ''))
|
||||||
|
self.increasePercent = float(
|
||||||
|
rawChange.split(' ')[1]\
|
||||||
|
.replace(',', '')\
|
||||||
|
.replace('(', '')\
|
||||||
|
.replace(')', '')\
|
||||||
|
.replace('%', ''))
|
||||||
|
except AttributeError as error:
|
||||||
|
raise StockDoesNotExistError(ticker) from error
|
||||||
|
|
Loading…
Reference in New Issue
Block a user