Semi-PEP8

Make it PEP 8 compliant where it does not break the API
This commit is contained in:
scoopgracie 2020-04-20 07:24:37 -07:00
parent c2341d8989
commit f506c44cdf

View File

@ -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,15 +25,26 @@
#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(
'https://finance.yahoo.com/quote/{}/history'\
.format(ticker),
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64)' +
' AppleWebKit/537.36 (KHTML, like Gecko)' +
' Chrome/79.0.3941.4 Safari/537.36'
})
except: except:
raise NetworkError() raise NetworkError()
if r.status_code is 302: if r.status_code is 302:
@ -64,13 +75,21 @@ class Stock:
self.historical.append(parsed) self.historical.append(parsed)
topData = soup.find(id='quote-header-info') topData = soup.find(id='quote-header-info')
try: try:
self.currentPrice = float(topData.findAll('span')[11].string.replace(',', '')) self.currentPrice = float(
topData.findAll('span')[11].string.replace(',', ''))
rawChange = topData.findAll('span')[12].string rawChange = topData.findAll('span')[12].string
except IndexError: except IndexError:
self.currentPrice = float(topData.findAll('span')[3].string.replace(',', '')) self.currentPrice = float(
topData.findAll('span')[3].string.replace(',', ''))
rawChange = topData.findAll('span')[4].string rawChange = topData.findAll('span')[4].string
self.increaseDollars = float(rawChange.split(' ')[0].replace(',', '')) self.increaseDollars = float(
self.increasePercent = float(rawChange.split(' ')[1].replace(',', '').replace('(', '').replace(')', '').replace('%', '')) rawChange.split(' ')[0].replace(',', ''))
self.increasePercent = float(
rawChange.split(' ')[1]\
.replace(',', '')\
.replace('(', '')\
.replace(')', '')\
.replace('%', ''))
except AttributeError as error: except AttributeError as error:
raise StockDoesNotExistError(ticker) from error raise StockDoesNotExistError(ticker) from error