Skip to content

Commit

Permalink
Merge pull request #166 from rjra2611/issue-#165-add-timeout-when-fet…
Browse files Browse the repository at this point in the history
…ching-modules.json

add timeout and fetch once a day
  • Loading branch information
Martin-Molinero authored Sep 21, 2022
2 parents 1cf3808 + 072ef02 commit 2095292
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lean/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import os
import json
import time
import requests
from pathlib import Path

Expand All @@ -21,16 +22,18 @@
dirname = os.path.dirname(__file__)
file_path = os.path.join(dirname, f'../{file_name}')

# check if new file is avaiable online
# check if new file is available online
url = f"https://cdn.quantconnect.com/cli/{file_name}"
try:
res = requests.get(url)
if res.ok:
new_content = res.json()
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(new_content, f, ensure_ascii=False, indent=4)
# fetch if file not available or fetched before 1 day
if not os.path.exists(file_path) or (time.time() - os.path.getmtime(file_path) > 86400):
res = requests.get(url, timeout=5)
if res.ok:
new_content = res.json()
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(new_content, f, ensure_ascii=False, indent=4)
except Exception as e:
# No need to do anything if file isn't avaiable
# No need to do anything if file isn't available
pass

# check if file exists
Expand Down

0 comments on commit 2095292

Please sign in to comment.