-
Notifications
You must be signed in to change notification settings - Fork 25
/
pydrive_utils.py
40 lines (30 loc) · 999 Bytes
/
pydrive_utils.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
import re
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
# Authentication + token creation
def create_drive_manager(cmd_auth):
gAuth = GoogleAuth()
if cmd_auth:
gAuth.CommandLineAuth()
else:
gAuth.LocalWebserverAuth()
gAuth.Authorize()
print("authorized access to google drive API!")
drive: GoogleDrive = GoogleDrive(gAuth)
return drive
def extract_files_id(drive, link):
try:
fileID = re.search(r"(?<=/d/|id=|rs/).+?(?=/|$)", link)[0] # extract the fileID
return fileID
except Exception as error:
print("error : " + str(error))
print("Link is probably invalid")
print(link)
def pydrive_download(drive, link, save_path):
id = extract_files_id(drive, link)
file_dir = os.path.dirname(save_path)
if file_dir:
os.makedirs(file_dir, exist_ok=True)
pydrive_file = drive.CreateFile({'id': id})
pydrive_file.GetContentFile(save_path)