Spaces:
Sleeping
Sleeping
upload_content_directly
Browse files
app.py
CHANGED
|
@@ -96,6 +96,18 @@ def upload_to_drive(service, file_name, folder_id, content):
|
|
| 96 |
|
| 97 |
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
def process_file(file):
|
| 101 |
# 读取文件
|
|
@@ -172,7 +184,7 @@ def process_youtube_link(link):
|
|
| 172 |
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['zh-TW'])
|
| 173 |
transcript_text = "\n".join([f"{item['start']}: {item['text']}" for item in transcript])
|
| 174 |
# 上传到Google Drive
|
| 175 |
-
|
| 176 |
print("逐字稿已上传到Google Drive")
|
| 177 |
else:
|
| 178 |
print("逐字稿已存在于Google Drive中")
|
|
|
|
| 96 |
|
| 97 |
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 98 |
|
| 99 |
+
def upload_content_directly(service, file_name, folder_id, content):
|
| 100 |
+
"""
|
| 101 |
+
直接将内容上传到Google Drive中的新文件。
|
| 102 |
+
"""
|
| 103 |
+
file_metadata = {'name': file_name, 'parents': [folder_id]}
|
| 104 |
+
# 使用io.StringIO为文本内容创建一个内存中的文件对象
|
| 105 |
+
fh = io.BytesIO(content.encode('utf-8'))
|
| 106 |
+
media = MediaIoBaseUpload(fh, mimetype='text/plain', resumable=True)
|
| 107 |
+
|
| 108 |
+
# 执行上传
|
| 109 |
+
service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
| 110 |
+
|
| 111 |
|
| 112 |
def process_file(file):
|
| 113 |
# 读取文件
|
|
|
|
| 184 |
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['zh-TW'])
|
| 185 |
transcript_text = "\n".join([f"{item['start']}: {item['text']}" for item in transcript])
|
| 186 |
# 上传到Google Drive
|
| 187 |
+
upload_content_directly(service, file_name, folder_id, transcript_text)
|
| 188 |
print("逐字稿已上传到Google Drive")
|
| 189 |
else:
|
| 190 |
print("逐字稿已存在于Google Drive中")
|