From 1613785bb49215130ed9d36d3048139bb4ebaec9 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Wed, 24 Jul 2024 22:38:32 +0200 Subject: [PATCH] update cramer job --- app/cron_cramer_tracker.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/app/cron_cramer_tracker.py b/app/cron_cramer_tracker.py index 1224f04..68d717f 100644 --- a/app/cron_cramer_tracker.py +++ b/app/cron_cramer_tracker.py @@ -5,7 +5,6 @@ from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options -from webdriver_manager.chrome import ChromeDriverManager from dotenv import load_dotenv import sqlite3 @@ -13,7 +12,6 @@ def save_json(data, file_path): with open(file_path, 'w') as file: ujson.dump(data, file) - query_template = """ SELECT name, sector @@ -23,7 +21,6 @@ query_template = """ symbol = ? """ - def main(): # Load environment variables con = sqlite3.connect('stocks.db') @@ -32,24 +29,22 @@ def main(): # Set up the WebDriver options options = Options() - options.headless = True # Run in headless mode + options.add_argument("--headless") + options.add_argument("--no-sandbox") + options.add_argument("--disable-dev-shm-usage") # Initialize the WebDriver - driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) + driver = webdriver.Chrome(options=options) try: # Fetch the website driver.get(url) - # Find the table element table = driver.find_element(By.TAG_NAME, 'table') - # Extract the table HTML table_html = table.get_attribute('outerHTML') - # Use pandas to read the HTML table df = pd.read_html(table_html)[0] - # Rename the columns df = df.rename(columns={ 'Ticker': 'ticker', @@ -57,10 +52,8 @@ def main(): 'Date': 'date', 'Return Since': 'returnSince' }) - # Convert the DataFrame to JSON data = ujson.loads(df.to_json(orient='records')) - res = [] for item in data: symbol = item['ticker'] @@ -70,9 +63,10 @@ def main(): res.append({**item, 'name': db_data['name'].iloc[0], 'sector': db_data['sector'].iloc[0]}) except Exception as e: pass - + # Save the JSON data - save_json(res, 'json/cramer-tracker/data.json') + if len(res) > 0: + save_json(res, 'json/cramer-tracker/data.json') finally: # Ensure the WebDriver is closed @@ -80,4 +74,4 @@ def main(): con.close() if __name__ == '__main__': - main() + main() \ No newline at end of file