Created
March 3, 2021 04:05
-
-
Save pwang2/90fbca85b9c121b17fe7cb358c70670d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from datetime import date, datetime | |
from functools import cache | |
from http.client import HTTPConnection | |
import pytz | |
from azure.storage.blob import BlobServiceClient, __version__ | |
CONTAINER_NAME = "overrides" | |
CONSTR_FROM = "" | |
CONSTR_TO = "" | |
conn = HTTPConnection("prod-imq1", 19351) | |
@cache | |
def get_sec_fya(sid) -> tuple[str, datetime]: | |
conn.request("GET", f'/security-info?id={str(sid)}') | |
response = conn.getresponse() | |
result = json.load(response) | |
bbid = result['BBID'] | |
if not isinstance(result['FYA'], str): | |
print(sid, "FYA missing") | |
date_fya = datetime.strptime(result['FYA'], "%Y-%m-%d") | |
localtz = pytz.timezone("America/Chicago") | |
return (bbid, localtz.localize(date_fya)) | |
def get_prev_fya(d): | |
try: | |
return d.replace(year=d.year - 1) | |
except ValueError: | |
return d + (date(d.year - 1, 1, 1) - date(d.year, 1, 1)) | |
try: | |
read_bclient = BlobServiceClient.from_connection_string(CONSTR_FROM) | |
read_cclient = read_bclient.get_container_client(CONTAINER_NAME) | |
read_blob_list = read_cclient.list_blobs() | |
write_bclient = BlobServiceClient.from_connection_string(CONSTR_TO) | |
write_cclient = write_bclient.get_container_client(CONTAINER_NAME) | |
for b in read_blob_list: | |
name: str = b.name | |
sec_id = name.split("/")[1] | |
saved_time: datetime = b.creation_time | |
blob_client = read_cclient.get_blob_client(b.name) | |
streamdld = blob_client.download_blob() | |
override = json.loads(streamdld.readall()) | |
(sec_name, sec_fya) = get_sec_fya(sec_id) | |
if saved_time < sec_fya: | |
prev_fya = get_prev_fya(sec_fya) | |
override["FYASnapshot"] = datetime.strftime(prev_fya, "%Y-%m-%d") | |
serialized = json.dumps(override, sort_keys=True, indent=4) | |
write_cclient.upload_blob(name, serialized, overwrite=True) | |
except Exception as ex: | |
print('Exception:') | |
print(ex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment