refactor dividend cron job
This commit is contained in:
parent
93325bca50
commit
c43c2ef4d6
@ -7,24 +7,14 @@ from selenium.webdriver.chrome.service import Service
|
|||||||
from webdriver_manager.chrome import ChromeDriverManager
|
from webdriver_manager.chrome import ChromeDriverManager
|
||||||
from selenium.webdriver.chrome.options import Options
|
from selenium.webdriver.chrome.options import Options
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
def save_json(data, file_path):
|
def save_json(data, file_path):
|
||||||
with open(file_path, 'w') as file:
|
with open(file_path, 'w') as file:
|
||||||
ujson.dump(data, file)
|
ujson.dump(data, file)
|
||||||
|
|
||||||
query_template = """
|
|
||||||
SELECT
|
|
||||||
name, sector
|
|
||||||
FROM
|
|
||||||
stocks
|
|
||||||
WHERE
|
|
||||||
symbol = ?
|
|
||||||
"""
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
con = sqlite3.connect('stocks.db')
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
url = os.getenv('DIVIDEND_ARISTOCRATS')
|
url = os.getenv('DIVIDEND_ARISTOCRATS')
|
||||||
|
|
||||||
@ -53,7 +43,7 @@ def main():
|
|||||||
'Company Name': 'name',
|
'Company Name': 'name',
|
||||||
'Stock Price': 'price',
|
'Stock Price': 'price',
|
||||||
'% Change': 'changesPercentage',
|
'% Change': 'changesPercentage',
|
||||||
'Div. Yield': 'dividiendYield',
|
'Div. Yield': 'dividendYield',
|
||||||
'Years': 'years'
|
'Years': 'years'
|
||||||
})
|
})
|
||||||
df = df.drop(columns=['No.'])
|
df = df.drop(columns=['No.'])
|
||||||
@ -63,21 +53,28 @@ def main():
|
|||||||
for item in data:
|
for item in data:
|
||||||
symbol = item['symbol']
|
symbol = item['symbol']
|
||||||
try:
|
try:
|
||||||
item['changesPercentage'] = round(float(item['changesPercentage'].replace('%','')),2)
|
with open(f"json/quote/{symbol}.json") as file:
|
||||||
item['dividiendYield'] = round(float(item['dividiendYield'].replace('%','')),2)
|
quote_data = ujson.load(file)
|
||||||
db_data = pd.read_sql_query(query_template, con, params=(symbol,))
|
|
||||||
res.append({**item,'sector': db_data['sector'].iloc[0]})
|
item['changesPercentage'] = round(quote_data['changesPercentage'],2)
|
||||||
|
item['price'] = round(quote_data['price'],2)
|
||||||
|
item['dividendYield'] = round(float(item['dividendYield'].replace('%','')),2)
|
||||||
|
res.append({**item})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Save the JSON data
|
# Save the JSON data
|
||||||
if len(res) > 0:
|
if len(res) > 0:
|
||||||
save_json(res, 'json/stocks-list/dividend-aristocrats.json')
|
res = sorted(res, key=lambda x: x['years'], reverse=True)
|
||||||
|
for rank, item in enumerate(res, start=1):
|
||||||
|
item['rank'] = rank
|
||||||
|
|
||||||
|
save_json(res, 'json/dividends/list/dividend-aristocrats.json')
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# Ensure the WebDriver is closed
|
# Ensure the WebDriver is closed
|
||||||
driver.quit()
|
driver.quit()
|
||||||
con.close()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
@ -7,24 +7,14 @@ from selenium.webdriver.chrome.service import Service
|
|||||||
from webdriver_manager.chrome import ChromeDriverManager
|
from webdriver_manager.chrome import ChromeDriverManager
|
||||||
from selenium.webdriver.chrome.options import Options
|
from selenium.webdriver.chrome.options import Options
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
def save_json(data, file_path):
|
def save_json(data, file_path):
|
||||||
with open(file_path, 'w') as file:
|
with open(file_path, 'w') as file:
|
||||||
ujson.dump(data, file)
|
ujson.dump(data, file)
|
||||||
|
|
||||||
query_template = """
|
|
||||||
SELECT
|
|
||||||
name, sector
|
|
||||||
FROM
|
|
||||||
stocks
|
|
||||||
WHERE
|
|
||||||
symbol = ?
|
|
||||||
"""
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
con = sqlite3.connect('stocks.db')
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
url = os.getenv('DIVIDEND_KINGS')
|
url = os.getenv('DIVIDEND_KINGS')
|
||||||
|
|
||||||
@ -53,7 +43,7 @@ def main():
|
|||||||
'Company Name': 'name',
|
'Company Name': 'name',
|
||||||
'Stock Price': 'price',
|
'Stock Price': 'price',
|
||||||
'% Change': 'changesPercentage',
|
'% Change': 'changesPercentage',
|
||||||
'Div. Yield': 'dividiendYield',
|
'Div. Yield': 'dividendYield',
|
||||||
'Years': 'years'
|
'Years': 'years'
|
||||||
})
|
})
|
||||||
df = df.drop(columns=['No.'])
|
df = df.drop(columns=['No.'])
|
||||||
@ -63,21 +53,28 @@ def main():
|
|||||||
for item in data:
|
for item in data:
|
||||||
symbol = item['symbol']
|
symbol = item['symbol']
|
||||||
try:
|
try:
|
||||||
item['changesPercentage'] = round(float(item['changesPercentage'].replace('%','')),2)
|
with open(f"json/quote/{symbol}.json") as file:
|
||||||
item['dividiendYield'] = round(float(item['dividiendYield'].replace('%','')),2)
|
quote_data = ujson.load(file)
|
||||||
db_data = pd.read_sql_query(query_template, con, params=(symbol,))
|
|
||||||
res.append({**item,'sector': db_data['sector'].iloc[0]})
|
item['changesPercentage'] = round(quote_data['changesPercentage'],2)
|
||||||
|
item['price'] = round(quote_data['price'],2)
|
||||||
|
item['dividendYield'] = round(float(item['dividendYield'].replace('%','')),2)
|
||||||
|
res.append({**item})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Save the JSON data
|
# Save the JSON data
|
||||||
if len(res) > 0:
|
if len(res) > 0:
|
||||||
save_json(res, 'json/stocks-list/dividend-kings.json')
|
res = sorted(res, key=lambda x: x['years'], reverse=True)
|
||||||
|
for rank, item in enumerate(res, start=1):
|
||||||
|
item['rank'] = rank
|
||||||
|
|
||||||
|
save_json(res, 'json/dividends/list/dividend-kings.json')
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# Ensure the WebDriver is closed
|
# Ensure the WebDriver is closed
|
||||||
driver.quit()
|
driver.quit()
|
||||||
con.close()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
@ -3750,7 +3750,7 @@ async def get_dividend_kings():
|
|||||||
headers={"Content-Encoding": "gzip"}
|
headers={"Content-Encoding": "gzip"}
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
with open(f"json/stocks-list/dividend-kings.json", 'rb') as file:
|
with open(f"json/dividends/list/dividend-kings.json", 'rb') as file:
|
||||||
res = orjson.loads(file.read())
|
res = orjson.loads(file.read())
|
||||||
except:
|
except:
|
||||||
res = []
|
res = []
|
||||||
@ -3778,7 +3778,7 @@ async def get_dividend_kings():
|
|||||||
headers={"Content-Encoding": "gzip"}
|
headers={"Content-Encoding": "gzip"}
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
with open(f"json/stocks-list/dividend-aristocrats.json", 'rb') as file:
|
with open(f"json/dividends/list/dividend-aristocrats.json", 'rb') as file:
|
||||||
res = orjson.loads(file.read())
|
res = orjson.loads(file.read())
|
||||||
except:
|
except:
|
||||||
res = []
|
res = []
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user