bugfixing
This commit is contained in:
parent
0500b1ca64
commit
6ae10d48c2
@ -28,6 +28,32 @@ def delete_files_in_directory(directory):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to delete {file_path}. Reason: {e}")
|
print(f"Failed to delete {file_path}. Reason: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def check_existing_file(ticker, folder_name):
|
||||||
|
file_path = f"json/earnings/{folder_name}/{ticker}.json"
|
||||||
|
still_new = False
|
||||||
|
if os.path.exists(file_path):
|
||||||
|
try:
|
||||||
|
with open(file_path, 'r') as file:
|
||||||
|
existing_data = ujson.load(file)
|
||||||
|
date_obj = datetime.strptime(existing_data['date'], "%Y-%m-%d")
|
||||||
|
if date_obj.tzinfo is None:
|
||||||
|
date_obj = date_obj.replace(tzinfo=pytz.UTC)
|
||||||
|
|
||||||
|
if folder_name == 'surprise':
|
||||||
|
if date_obj >= N_days_ago:
|
||||||
|
still_new = True
|
||||||
|
elif folder_name == 'next':
|
||||||
|
if date_obj >= today:
|
||||||
|
still_new = True
|
||||||
|
|
||||||
|
if still_new == False:
|
||||||
|
os.remove(file_path)
|
||||||
|
print(f"Deleted file for {ticker}.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing existing file for {ticker}: {e}")
|
||||||
|
|
||||||
|
|
||||||
async def save_json(data, symbol, dir_path):
|
async def save_json(data, symbol, dir_path):
|
||||||
file_path = os.path.join(dir_path, f"{symbol}.json")
|
file_path = os.path.join(dir_path, f"{symbol}.json")
|
||||||
async with aiofiles.open(file_path, 'w') as file:
|
async with aiofiles.open(file_path, 'w') as file:
|
||||||
@ -37,7 +63,6 @@ async def get_data(session, ticker):
|
|||||||
querystring = {"token": api_key, "parameters[tickers]": ticker}
|
querystring = {"token": api_key, "parameters[tickers]": ticker}
|
||||||
try:
|
try:
|
||||||
async with session.get(url, params=querystring, headers=headers) as response:
|
async with session.get(url, params=querystring, headers=headers) as response:
|
||||||
if response.status == 200:
|
|
||||||
data = ujson.loads(await response.text())['earnings']
|
data = ujson.loads(await response.text())['earnings']
|
||||||
|
|
||||||
# Filter for future earnings
|
# Filter for future earnings
|
||||||
@ -63,8 +88,10 @@ async def get_data(session, ticker):
|
|||||||
}
|
}
|
||||||
await save_json(res_list, symbol, 'json/earnings/next')
|
await save_json(res_list, symbol, 'json/earnings/next')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#print(e)
|
print(e)
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
check_existing_file(ticker, "next")
|
||||||
|
|
||||||
# Filter for past earnings within the last 20 days
|
# Filter for past earnings within the last 20 days
|
||||||
recent_dates = [item for item in data if N_days_ago <= ny_tz.localize(datetime.strptime(item["date"], "%Y-%m-%d")) <= today]
|
recent_dates = [item for item in data if N_days_ago <= ny_tz.localize(datetime.strptime(item["date"], "%Y-%m-%d")) <= today]
|
||||||
@ -89,11 +116,13 @@ async def get_data(session, ticker):
|
|||||||
'date': date,
|
'date': date,
|
||||||
}
|
}
|
||||||
await save_json(res_list, symbol, 'json/earnings/surprise')
|
await save_json(res_list, symbol, 'json/earnings/surprise')
|
||||||
except:
|
|
||||||
pass
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#print(e)
|
print(e)
|
||||||
pass
|
else:
|
||||||
|
check_existing_file(ticker, "surprise")
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
#pass
|
||||||
|
|
||||||
async def run(stock_symbols):
|
async def run(stock_symbols):
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
@ -102,14 +131,11 @@ async def run(stock_symbols):
|
|||||||
await f
|
await f
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Delete old files in the directories
|
|
||||||
delete_files_in_directory("json/earnings/next")
|
|
||||||
delete_files_in_directory("json/earnings/surprise")
|
|
||||||
|
|
||||||
con = sqlite3.connect('stocks.db')
|
con = sqlite3.connect('stocks.db')
|
||||||
cursor = con.cursor()
|
cursor = con.cursor()
|
||||||
cursor.execute("PRAGMA journal_mode = wal")
|
cursor.execute("PRAGMA journal_mode = wal")
|
||||||
cursor.execute("SELECT DISTINCT symbol FROM stocks WHERE symbol NOT LIKE '%.%'")
|
cursor.execute("SELECT DISTINCT symbol FROM stocks WHERE symbol NOT LIKE '%.%' AND symbol NOT LIKE '%-%'")
|
||||||
stock_symbols = [row[0] for row in cursor.fetchall()]
|
stock_symbols = [row[0] for row in cursor.fetchall()]
|
||||||
con.close()
|
con.close()
|
||||||
asyncio.run(run(stock_symbols))
|
asyncio.run(run(stock_symbols))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user