-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor update function to be reusable
- Loading branch information
Ru Chern Chong
committed
May 7, 2024
1 parent
1f2bf06
commit 7c810ae
Showing
6 changed files
with
44 additions
and
131 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MONGODB_URI= | ||
MONGODB_DB_NAME= |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,23 @@ | ||
import boto3 | ||
from dotenv import load_dotenv | ||
|
||
from updater import update | ||
|
||
load_dotenv() | ||
|
||
|
||
COLLECTION_NAME: str = "cars" | ||
ZIP_FILE_NAME: str = "Monthly New Registration of Cars by Make.zip" | ||
ZIP_URL: str = ( | ||
f"https://datamall.lta.gov.sg/content/dam/datamall/datasets/Facts_Figures/Vehicle Registration/{ZIP_FILE_NAME}" | ||
) | ||
|
||
print(f"ZIP_URL {ZIP_URL}") | ||
import asyncio | ||
import updater | ||
from typing import List | ||
|
||
|
||
async def main(): | ||
result = await update() | ||
|
||
if __name__ == "__main__": | ||
# Create a DynamoDB table | ||
my_table = create_dynamodb_table("cars") | ||
|
||
# Put the item into the table | ||
put_item_into_table(my_table, result) | ||
|
||
|
||
def create_dynamodb_table(table_name): | ||
# Create a DynamoDB resource | ||
dynamodb = boto3.resource("dynamodb") | ||
|
||
# Create the table | ||
table = dynamodb.create_table( | ||
TableName=table_name, | ||
KeySchema=[ | ||
{"AttributeName": "id", "KeyType": "HASH"}, # Partition key | ||
{"AttributeName": "timestamp", "KeyType": "RANGE"}, # Sort key | ||
], | ||
AttributeDefinitions=[ | ||
{"AttributeName": "id", "AttributeType": "S"}, # String data type | ||
{ | ||
"AttributeName": "timestamp", | ||
"AttributeType": "N", # Number data type | ||
}, | ||
], | ||
ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}, | ||
collection_name: str = "cars" | ||
zip_file_name: str = "Monthly New Registration of Cars by Make.zip" | ||
zip_url: str = ( | ||
f"https://datamall.lta.gov.sg/content/dam/datamall/datasets/Facts_Figures/Vehicle Registration/{zip_file_name}" | ||
) | ||
key_fields: List[str] = ["month"] | ||
|
||
# Wait until the table exists | ||
table.meta.client.get_waiter("table_exists").wait(TableName=table_name) | ||
|
||
print(f"DynamoDB Table {table_name} created") | ||
|
||
return table | ||
await updater.main( | ||
collection_name=collection_name, | ||
zip_url=zip_url, | ||
zip_file_name=zip_file_name, | ||
key_fields=key_fields, | ||
) | ||
|
||
|
||
def put_item_into_table(table, item_data): | ||
# Put an item into the DynamoDB table | ||
table.put_item(Item=item_data) | ||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import asyncio | ||
import updater | ||
from typing import List | ||
|
||
|
||
async def main(): | ||
collection_name: str = "coe" | ||
zip_file_name: str = "COE Bidding Results.zip" | ||
zip_url: str = ( | ||
f"https://datamall.lta.gov.sg/content/dam/datamall/datasets/Facts_Figures/Vehicle Registration/{zip_file_name}" | ||
) | ||
key_fields: List[str] = ["month", "bidding_no"] | ||
|
||
await updater.main( | ||
collection_name=collection_name, | ||
zip_url=zip_url, | ||
zip_file_name=zip_file_name, | ||
key_fields=key_fields, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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