-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdrive_download.py
More file actions
127 lines (103 loc) · 4.26 KB
/
gdrive_download.py
File metadata and controls
127 lines (103 loc) · 4.26 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
"""
Download files from Google Drive
"""
import os
import sys
import pickle
from pathlib import Path
from path_utils import get_token_path, ensure_in_project
def download_file(file_id_or_name, output_path=None):
"""Download file from Google Drive by ID or name"""
ensure_in_project()
try:
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
import io
# Load credentials (adaptive path)
token_path = get_token_path()
if not token_path.exists():
print("❌ Not authenticated!")
print("Run: python setup_google_drive.py authenticate")
return False
with open(token_path, 'rb') as token:
creds = pickle.load(token)
service = build('drive', 'v3', credentials=creds)
# If input looks like a name, search for it
if not file_id_or_name.startswith('1'): # Google Drive IDs start with 1
print(f"🔍 Searching for file: {file_id_or_name}")
query = f"name='{file_id_or_name}' and trashed=false"
results = service.files().list(
q=query,
fields='files(id, name, size)',
pageSize=10
).execute()
files = results.get('files', [])
if not files:
print(f"❌ File not found: {file_id_or_name}")
return False
if len(files) > 1:
print(f"⚠️ Multiple files found ({len(files)}). Using first:")
for i, f in enumerate(files, 1):
size_mb = int(f.get('size', 0)) / (1024**2)
print(f" {i}. {f['name']} ({size_mb:.1f} MB)")
print("")
file_id = files[0]['id']
file_name = files[0]['name']
else:
file_id = file_id_or_name
# Get file metadata
file_metadata = service.files().get(fileId=file_id, fields='name, size').execute()
file_name = file_metadata.get('name', 'downloaded_file')
# Determine output path
if output_path is None:
output_path = Path(file_name)
else:
output_path = Path(output_path)
if output_path.is_dir():
output_path = output_path / file_name
# Get file size
file_metadata = service.files().get(fileId=file_id, fields='size').execute()
file_size = int(file_metadata.get('size', 0))
file_size_mb = file_size / (1024**2)
print(f"\n📥 Downloading: {file_name} ({file_size_mb:.1f} MB)")
print(f" Saving to: {output_path}")
# Download
request = service.files().get_media(fileId=file_id)
with open(output_path, 'wb') as fh:
downloader = MediaIoBaseDownload(fh, request)
done = False
last_progress = 0
while done is False:
status, done = downloader.next_chunk()
progress = int(status.progress() * 100)
if progress >= last_progress + 10:
print(f" Progress: {progress}%")
last_progress = progress
print(f"\n✅ Download complete!")
print(f" File: {output_path.absolute()}")
print(f" Size: {file_size_mb:.1f} MB")
print("")
return True
except Exception as e:
print(f"❌ Download failed: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Main execution"""
if len(sys.argv) < 2:
print("Usage: python gdrive_download.py <file_id_or_name> [output_path]")
print("")
print("Examples:")
print(" python gdrive_download.py 1ABC123xyz456...")
print(" python gdrive_download.py model.ply")
print(" python gdrive_download.py model.ply output/")
print("")
return
file_id_or_name = sys.argv[1]
output_path = sys.argv[2] if len(sys.argv) > 2 else None
download_file(file_id_or_name, output_path)
if __name__ == "__main__":
main()