save a song to google drive
Parameters:
-
gdrive_folder_id
(str)
–
-
credentials_path
(str)
–
path to a credentials.json file
-
token_path
(str)
–
path to a google cloud token file
-
song_name
(str)
–
the name of the song (filename)
-
song_path
(str)
–
the path of the song locally
-
auth_port
(int)
–
the port to use for oauth
-
bind_addr
(Optional[str], default:
None
)
–
optionally specify the bind address, otherwise localhost is used. Defaults to None.
Returns:
-
str ( str
) –
the file id of the upload
Source code in songbirdcore/gdrive.py
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 | def save_song(
gdrive_folder_id: str,
credentials_path: str,
token_path: str,
song_name: str,
song_path: str,
auth_port: int,
bind_addr: Optional[str] = None,
) -> str:
"""save a song to google drive
Args:
gdrive_folder_id (str): google drive folder id
credentials_path (str): path to a credentials.json file
token_path (str): path to a google cloud token file
song_name (str): the name of the song (filename)
song_path (str): the path of the song locally
auth_port (int): the port to use for oauth
bind_addr (Optional[str], optional): optionally specify the bind address, otherwise localhost is used. Defaults to None.
Returns:
str: the file id of the upload
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
scopes = ["https://www.googleapis.com/auth/drive"]
if os.path.exists(token_path):
creds = Credentials.from_authorized_user_file(token_path, scopes)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(credentials_path, scopes)
creds = flow.run_local_server(
port=auth_port, bind_addr=bind_addr, open_browser=False
)
# Save the credentials for the next run
with open(token_path, "w") as token:
token.write(creds.to_json())
logger.debug(f"Loaded credentials: {creds}")
service = build("drive", "v3", credentials=creds)
file_metadata = {"name": song_name, "parents": [gdrive_folder_id]}
media = http.MediaFileUpload(song_path, mimetype="audio/jpeg")
file = (
service.files()
.create(body=file_metadata, media_body=media, fields="id")
.execute()
)
file_id = file.get("id")
logger.info(f"File creation successful -- ID: {file_id}")
return file_id
|