84 lines
2.9 KiB
Python
84 lines
2.9 KiB
Python
import sys
|
|
import requests
|
|
import pandas as pd
|
|
import json
|
|
import re
|
|
|
|
def get_excel_download_url(google_sheet_url):
|
|
"""
|
|
Convert a Google Sheets URL to its Excel export URL.
|
|
"""
|
|
match = re.search(r'/d/([a-zA-Z0-9-_]+)', google_sheet_url)
|
|
if not match:
|
|
raise ValueError('Invalid Google Sheets URL')
|
|
sheet_id = match.group(1)
|
|
# Export the first sheet as Excel
|
|
return f'https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=xlsx&id={sheet_id}'
|
|
|
|
|
|
def download_excel(url, output_path):
|
|
"""
|
|
Download the Excel file from the given URL.
|
|
"""
|
|
response = requests.get(url)
|
|
if response.status_code != 200:
|
|
raise Exception(f'Failed to download file: {response.status_code}')
|
|
with open(output_path, 'wb') as f:
|
|
f.write(response.content)
|
|
|
|
|
|
def convert_excel_to_json(excel_path, json_path):
|
|
"""
|
|
Read the Excel file, extract columns from the 'Genesis Allocation' sheet, and save as JSON.
|
|
"""
|
|
df = pd.read_excel(excel_path, sheet_name='Genesis Allocation')
|
|
# Ensure columns exist
|
|
required_columns = [
|
|
'Placeholder',
|
|
'Laconic Address',
|
|
'Total LPS Allocation',
|
|
'Lock (months)',
|
|
'Vest (months)'
|
|
]
|
|
for col in required_columns:
|
|
if col not in df.columns:
|
|
raise Exception(f'Missing required column: {col}')
|
|
|
|
result = {}
|
|
for _, row in df.iterrows():
|
|
placeholder = str(row['Placeholder']) if not pd.isna(row['Placeholder']) else ''
|
|
laconic_address = str(row['Laconic Address']) if not pd.isna(row['Laconic Address']) else ''
|
|
# Use laconic_address as key if placeholder is missing or empty
|
|
key = placeholder if placeholder and placeholder.lower() != 'nan' else laconic_address
|
|
if not key or key.lower() == 'nan':
|
|
continue
|
|
entry = {
|
|
'total_lps_allocation': row['Total LPS Allocation'] if not pd.isna(row['Total LPS Allocation']) else None,
|
|
'lock_months': row['Lock (months)'] if not pd.isna(row['Lock (months)']) else None,
|
|
'vest_months': row['Vest (months)'] if not pd.isna(row['Vest (months)']) else None,
|
|
'laconic_address': row['Laconic Address'] if not pd.isna(row['Laconic Address']) else None
|
|
}
|
|
result[key] = entry
|
|
|
|
with open(json_path, 'w') as f:
|
|
json.dump(result, f, indent=2)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) != 2:
|
|
print('Usage: python download_and_convert_google_sheet.py <google_sheet_url>')
|
|
sys.exit(1)
|
|
google_sheet_url = sys.argv[1]
|
|
excel_url = get_excel_download_url(google_sheet_url)
|
|
excel_path = 'sheet.xlsx'
|
|
json_path = 'distribution.json'
|
|
print(f'Downloading Excel file from: {excel_url}')
|
|
download_excel(excel_url, excel_path)
|
|
print('Converting Excel to JSON...')
|
|
convert_excel_to_json(excel_path, json_path)
|
|
print(f'JSON saved to {json_path}')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|