Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Apr 1, 2024
1 parent ab2f038 commit 130d26c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
16 changes: 7 additions & 9 deletions apiserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
" ".join([STOCKS, INDICES, CRYPTOS, CURRENCIES, COMMODITIES])
)

lastUpdatedTime = time.time()
last_updated_time = time.time()


class apiHandler(http.server.BaseHTTPRequestHandler):
Expand All @@ -70,13 +70,11 @@ def do_GET(self):
{**crypto_status, **currency_status, **commodity_status}
)
elif self.path == "/health":
if time.time() - lastUpdatedTime > 1200:
if time.time() - last_updated_time > 1200:
self.send_response(500)
else:
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
return
self.send_header("Content-type", "application/json")
self.end_headers()
return
else:
message = {"message": "Not Found"}
response = json.dumps(message)
Expand Down Expand Up @@ -221,8 +219,8 @@ def update_status(symbols):
with open(MAPPING[symbols][1], "w") as file:
json.dump(MAPPING[symbols][0], file)

global lastUpdatedTime
lastUpdatedTime = time.time()
global last_updated_time
last_updated_time = time.time()


if __name__ == "__main__":
Expand Down
24 changes: 12 additions & 12 deletions novel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

UNAVAILABLE_IPS = ["154.95.36.199"]

lastUpdatedTime = time.time()
last_updated_time = time.time()


class HealthCheckHandler(http.server.BaseHTTPRequestHandler):
Expand All @@ -46,10 +46,10 @@ def do_GET(self):
response = json.dumps(filtered_titles)
self.wfile.write(response.encode("utf-8"))
elif self.path == "/health":
global lastUpdatedTime
global loopTime
global last_updated_time
global loop_time

if time.time() - lastUpdatedTime > loopTime:
if time.time() - last_updated_time > loop_time:
self.send_response(500)
else:
self.send_response(200)
Expand Down Expand Up @@ -110,18 +110,18 @@ def start_health_server():
]
i = 0
j = 0
loopTime = len(books) * 5 * 60
sleepInterval = loopTime / len(books)
loop_time = len(books) * 5 * 60
sleep_interval = loop_time / len(books)

titles = dict()
if os.path.exists(CACHE_PATH):
with open(CACHE_PATH, "r") as file:
titles = json.load(file)
booknames = set(name for _, name in books)
booknames_previous = set(name + "previous" for _, name in books)
book_name = set(name for _, name in books)
book_name_previous = set(name + "previous" for _, name in books)
dict_copy = titles.copy()
for book in dict_copy.keys():
if book not in booknames and book not in booknames_previous:
if book not in book_name and book not in book_name_previous:
del titles[book]

headers = {
Expand Down Expand Up @@ -193,12 +193,12 @@ def get_book_title(url, proxy=None):
print(
f"Error occurred during iteration {index} on line {e.__traceback__.tb_lineno}"
)
time.sleep(sleepInterval)
time.sleep(sleep_interval)
if index == len(proxies) - 1:
raise e

lastUpdatedTime = time.time()
time.sleep(sleepInterval)
last_updated_time = time.time()
time.sleep(sleep_interval)
i = (i + 1) % len(books)

except Exception as e:
Expand Down

0 comments on commit 130d26c

Please sign in to comment.