-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdrive_download.py
53 lines (46 loc) · 1.96 KB
/
gdrive_download.py
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
import json
import os
import io
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
import streamlit as st
# Scopes required to access Google Drive
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
# Path to the OAuth 2.0 credentials JSON file
# Function to authenticate and return the service
def authenticate(google_api_token):
creds_json = google_api_token
if creds_json:
creds_dict = json.loads(creds_json)
creds = Credentials.from_authorized_user_info(creds_dict, SCOPES)
return build('drive', 'v3', credentials=creds)
def get_secret_or_input(secret_key, secret_name, info_link=None):
if secret_key in st.secrets:
secret_value = st.secrets[secret_key]
else:
st.write(f"Please provide your {secret_name}")
secret_value = st.text_input(secret_name, key=f"input_{secret_key}", type="password")
if secret_value:
st.session_state[secret_key] = secret_value
if info_link:
st.markdown(f"[Get an {secret_name}]({info_link})")
return secret_value
# Function to download a file from Google Drive
def download_file(file_id, output_path, google_api_token=None):
service = authenticate(google_api_token)
request = service.files().get_media(fileId=file_id)
fh = io.FileIO(output_path, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
status, done = downloader.next_chunk()
print(f'Download {int(status.progress() * 100)}%.')
print(f'File downloaded to {output_path}')
if __name__ == '__main__':
# Replace 'your-file-id' with the actual file ID of the file you want to download
FILE_ID = '1Y54RvtIAt-MhVJIy_99wW-cv1T1zmSrz'
OUTPUT_PATH = 'zicrhon_drive_a.pdf'
download_file(FILE_ID, OUTPUT_PATH)