import os import logging from google.oauth2 import service_account from googleapiclient.discovery import build from googleapiclient.http import MediaFileUpload SCOPES = ['https://www.googleapis.com/auth/drive'] SERVICE_ACCOUNT_FILE = os.getenv('GOOGLE_CREDS_FILE', '/app/config/google_creds.json' if os.path.exists('/app/config/google_creds.json') else '/home/matrixhasyou/domovoy_google_creds.json') PARENT_FOLDER_ID = '1lnHt9Os0L_SnBa8i2dHrI0lBepuVm0Om' GIS_FILE_NAME = "TOTAL_GIS_ARCHIVE.pdf" logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s") log = logging.getLogger("GISDriveSync") def upload_gis_archive(file_path): if not os.path.exists(file_path): log.error(f"File not found: {file_path}") return "ошибка: файл не найден" creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES) service = build('drive', 'v3', credentials=creds) query = f"name = '{GIS_FILE_NAME}' and '{PARENT_FOLDER_ID}' in parents and trashed = false" results = service.files().list(q=query, spaces='drive', fields='files(id, name)').execute() files = results.get('files', []) media = MediaFileUpload(file_path, mimetype='application/pdf', resumable=True) try: if files: file_id = files[0]['id'] log.info(f"Updating: {GIS_FILE_NAME}") service.files().update(fileId=file_id, media_body=media).execute() return "обновлен" else: log.info(f"Creating: {GIS_FILE_NAME}") file_metadata = {'name': GIS_FILE_NAME, 'parents': [PARENT_FOLDER_ID]} service.files().create(body=file_metadata, media_body=media, fields='id').execute() return "создан" except Exception as e: return f"ошибка API: {str(e)}" if __name__ == "__main__": pass