Spaces:
Running
Running
File size: 2,599 Bytes
aee2bfd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# src/utils/google_drive_service.py
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
import io
import os
class GoogleDriveService:
def __init__(self, credentials_path: str):
"""
Initialize Google Drive service
Args:
credentials_path (str): Path to service account credentials file
"""
self.credentials = service_account.Credentials.from_service_account_file(
credentials_path,
scopes=['https://www.googleapis.com/auth/drive.readonly']
)
self.service = build('drive', 'v3', credentials=self.credentials)
def get_folder_contents(self, folder_id: str):
"""
Get contents of a Drive folder
Args:
folder_id (str): ID of the folder to process
Returns:
List[Dict]: List of file metadata
"""
query = f"'{folder_id}' in parents and trashed=false"
results = self.service.files().list(
q=query,
fields="files(id, name, mimeType,modifiedTime)",
supportsAllDrives=True,
includeItemsFromAllDrives=True
).execute()
return results.get('files', [])
def download_file(self, file_id: str) -> bytes:
"""
Download a file from Drive
Args:
file_id (str): ID of the file to download
Returns:
bytes: File content
"""
request = self.service.files().get_media(fileId=file_id)
content = io.BytesIO()
downloader = MediaIoBaseDownload(content, request)
done = False
while not done:
_, done = downloader.next_chunk()
content.seek(0)
return content.read()
def export_file(self, file_id: str, mime_type: str) -> bytes:
"""
Export a Google Workspace file to a different format
Args:
file_id (str): ID of the file to export
mime_type (str): MIME type to export to
Returns:
bytes: Exported file content
"""
request = self.service.files().export_media(
fileId=file_id,
mimeType=mime_type
)
content = io.BytesIO()
downloader = MediaIoBaseDownload(content, request)
done = False
while not done:
_, done = downloader.next_chunk()
content.seek(0)
return content.read() |