Skip to content

Commit

Permalink
environment variable for local env updated, allowing use between loca…
Browse files Browse the repository at this point in the history
…l deployment and gitHub actions deployment
  • Loading branch information
parcheesime committed May 20, 2024
1 parent 084fffb commit 30d68df
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 27 deletions.
6 changes: 3 additions & 3 deletions drive_utils/data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ def fetch_and_create_shapefile(api_url, district, parent_folder_id):
# Create GeoDataFrame
gdf = gpd.GeoDataFrame(properties_list, geometry=geometry_list)
if not gdf.empty:
print(gdf.head()) # Display some entries for logging
print(gdf.head())

creds = drive_operations.get_credentials()
district_folder_id = drive_operations.create_or_find_subfolder(parent_folder_id, district)

# Temporarily save the shapefile locally in the /tmp directory
# temporarily save the shapefile locally in the /tmp directory
with tempfile.TemporaryDirectory() as tmpdir:
shapefile_base_path = os.path.join(tmpdir, f"{district}.shp")
gdf.to_file(shapefile_base_path, driver='ESRI Shapefile')

# Upload each generated file component to Google Drive
for file_name in os.listdir(tmpdir):
file_path = os.path.join(tmpdir, file_name)
if os.path.isfile(file_path): # Ensure it's a file
if os.path.isfile(file_path):
drive_operations.upload_file_to_drive(district_folder_id, file_path, file_name)

print(f"Shapefiles for district {district} successfully uploaded to Google Drive.")
21 changes: 0 additions & 21 deletions drive_utils/drive_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def list_contents_of_folder(folder_id):


def create_or_find_subfolder(parent_folder_id, subfolder_name):
from google.oauth2 import service_account
from googleapiclient.discovery import build

creds = get_credentials()
Expand Down Expand Up @@ -131,26 +130,6 @@ def list_files_in_folder(folder_id):
return files


def upload_or_replace_file(folder_id, file_path, file_name, creds):
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

service = build('drive', 'v3', credentials=creds)
file_id = find_file_id(service, folder_id, file_name)

file_metadata = {'name': file_name, 'parents': [folder_id]}
media = MediaFileUpload(file_path, mimetype='application/octet-stream', resumable=True)

if file_id:
# If the file exists, update it
updated_file = service.files().update(fileId=file_id, body=file_metadata, media_body=media).execute()
print(f"Updated file with ID: {updated_file.get('id')}")
else:
# If the file does not exist, upload it as new
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
print(f"Uploaded new file with ID: {file.get('id')}")


def get_folder_size(folder_id):
"""
Calculates the total size of all files in a specified Google Drive folder.
Expand Down
6 changes: 3 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import os

parent_folder_id = os.getenv('FOLDER_ID')
# parent_folder_id = FOLDER_ID

print(f"FOLDER_ID: {parent_folder_id}")


api_urls = {
Expand All @@ -19,5 +20,4 @@
}

for district, api_url in api_urls.items():
fetch_and_create_shapefile(api_url, district, parent_folder_id)

fetch_and_create_shapefile(api_url, district, parent_folder_id)

0 comments on commit 30d68df

Please sign in to comment.