Skip to content

Commit

Permalink
Added logic to upload <wspr_spot_data.csv> to Google Drive.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexara-prime-ai committed Jun 4, 2024
1 parent 67430a3 commit 9d3fa0e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.terraform
*.tfstate
.env
service_account.json
# Added by cargo

/target
Expand Down
9 changes: 6 additions & 3 deletions hyper/hyper/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class CONSTANTS:
FILE_PATH = "./tableau/data"
FILE_NAME = "wspr_spots.csv"
FILE_PATH = "./tableau/data"
FILE_NAME = "wspr_spot_data.csv"
FULL_PATH = FILE_PATH + "/" + FILE_NAME
SCOPES = ["https://www.googleapis.com/auth/drive"]
SERVICE_ACCOUNT_FILE = "service_account.json"
PARENT_FOLDER_ID = "1uVnscmoxu91XT1LMnZoOOn3iCTirlEII"
33 changes: 33 additions & 0 deletions hyper/hyper/drive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import constants
from google.oauth2 import service_account
from googleapiclient.discovery import build


def authenticate():
try:
print("\n[Authenticating] <wspr_cdk> service user...")

credentials = service_account.Credentials.from_service_account_file(
constants.SERVICE_ACCOUNT_FILE, scopes=constants.SCOPES
)

return credentials
except Exception as e:
print("\n[ERROR] -> Failed to [Authenticate] <wspr_cdk> service user: \n", e)


def upload_to_drive(file_path):
try:
credentials = authenticate()
service = build("drive", "v3", credentials=credentials)

file_metadata = {
"name": "wspr_spot_data",
"parents": [constants.PARENT_FOLDER_ID],
}

print("[Uploading] file to Google Drive...\n")

service.files().create(body=file_metadata, media_body=file_path).execute()
except Exception as e:
print("\n[ERROR] -> Failed to upload to Google Drive: \n", e)
14 changes: 8 additions & 6 deletions hyper/hyper/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import os

import constants
import drive

import python_wrapper.python_wrapper


class Server:
def __init__(self):
self.write_path = os.path.join(
constants.CONSTANTS.FILE_PATH, "wspr_spot_data.csv"
)
self.write_path = os.path.join(constants.FILE_PATH, "wspr_spot_data.csv")

async def write_to_csv(self):
"""
Expand All @@ -20,7 +19,7 @@ async def write_to_csv(self):
"""

try:
output = await python_wrapper.python_wrapper.get_wspr_spots("10", "JSON")
output = await python_wrapper.python_wrapper.get_wspr_spots("1000", "JSON")
data = output.get_data()

# Display data that's being fetched for [DEBUG] purposes.
Expand All @@ -31,8 +30,8 @@ async def write_to_csv(self):
print("\nWrite path: \n", write_path)

# Check if directory exists.
if not os.path.exists(constants.CONSTANTS.FILE_PATH):
os.makedirs(constants.CONSTANTS.FILE_PATH)
if not os.path.exists(constants.FILE_PATH):
os.makedirs(constants.FILE_PATH)

with open(write_path, mode="w", newline="") as file:
writer = csv.writer(file)
Expand Down Expand Up @@ -86,6 +85,9 @@ async def write_to_csv(self):
record["code"],
]
)

# Upload [output] file to Google Drive.
drive.upload_to_drive(constants.FULL_PATH)
except Exception as e:
print("An [ERROR] occurred: ", e)

Expand Down
7 changes: 7 additions & 0 deletions scripts/bash/python_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ declare -A module_import_map=(
for module in "${!module_import_map[@]}"; do
verify_installation "${module}" "${module_import_map[${module}]}"
done


# An array modules contains the names of all the modules to be installed.
# The pip install command installs or upgrades all the modules listed in the array.
# A dictionary module_import_map maps module names to their respective import names.
# The verify_installation function takes a module name and its import name as arguments, checks if the module can be imported, and prints the appropriate message.
# The script iterates over the module_import_map dictionary to verify the installation of each module using the verify_installation function.

0 comments on commit 9d3fa0e

Please sign in to comment.