-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_to_drive.py
126 lines (99 loc) · 4.33 KB
/
markdown_to_drive.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
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
import argparse
import io
import markdown2
from googleapiclient.http import MediaIoBaseUpload
from google.oauth2 import service_account
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/drive.file']
def authenticate_google_drive(credentials_path):
"""
Authenticates and creates a Google Drive API service object.
Parameters:
credentials_path (str): Path to the Google Drive API credentials JSON file.
Returns:
service: Google Drive API service object.
"""
flow = InstalledAppFlow.from_client_secrets_file(credentials_path, SCOPES)
creds = flow.run_local_server(port=0)
service = build('drive', 'v3', credentials=creds)
return service
def convert_markdown_to_pdf(markdown_file):
"""
Converts a Markdown file to a PDF file.
Parameters:
markdown_file (str): Path to the Markdown file.
Returns:
bytes: PDF content as bytes.
"""
with open(markdown_file, 'r', encoding='utf-8') as f:
markdown_content = f.read()
# Convert Markdown to HTML
html_content = markdown2.markdown(markdown_content)
# Create PDF from HTML using WeasyPrint
pdf_content = io.BytesIO()
from weasyprint import HTML
HTML(string=html_content).write_pdf(pdf_content)
pdf_content.seek(0) # Reset the buffer position to the beginning
return pdf_content
def upload_pdf_to_drive(service, pdf_content, folder_id, file_name="output.pdf"):
"""
Uploads the PDF file to Google Drive in the specified folder.
Parameters:
service: Google Drive API service object.
pdf_content (bytes): The PDF content to upload.
folder_id (str): The Google Drive folder ID where the file will be uploaded.
file_name (str): The name of the file to be uploaded.
Returns:
dict: The uploaded file metadata including 'id' and 'webViewLink'.
"""
file_metadata = {
'name': file_name,
'parents': [folder_id],
'mimeType': 'application/pdf'
}
media = MediaIoBaseUpload(pdf_content, mimetype='application/pdf', resumable=True)
file = service.files().create(
body=file_metadata,
media_body=media,
fields='id, webViewLink').execute()
return file
def extract_folder_id(drive_folder_link):
"""
Extracts the folder ID from the Google Drive folder link.
Parameters:
drive_folder_link (str): The Google Drive folder link.
Returns:
str: The extracted folder ID.
"""
return drive_folder_link.split('/')[-1]
def main(markdown_file, drive_folder_link, credentials_path, output_name):
"""
Main function to convert a Markdown file to PDF and upload it to Google Drive.
Parameters:
markdown_file (str): Path to the Markdown file.
drive_folder_link (str): Google Drive folder link where the PDF file will be uploaded.
credentials_path (str): Path to the Google Drive API credentials JSON file.
"""
# Authenticate Google Drive API
service = authenticate_google_drive(credentials_path)
# Convert the Markdown file to PDF
pdf_content = convert_markdown_to_pdf(markdown_file)
# Extract folder ID from the provided Google Drive folder link
folder_id = extract_folder_id(drive_folder_link)
# Upload the PDF content to Google Drive
if 'pdf' not in output_name:
output_name += ".pdf"
uploaded_file = upload_pdf_to_drive(service, pdf_content, folder_id, file_name=output_name)
# Get file ID and webViewLink
file_id = uploaded_file['id']
web_view_link = uploaded_file.get('webViewLink', None)
print(f"File uploaded successfully. Web View Link: {web_view_link}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert Markdown to PDF and upload to Google Drive.")
parser.add_argument('markdown_file', type=str, help="Path to the Markdown file.")
parser.add_argument('drive_folder_link', type=str, help="Google Drive folder link where the PDF will be uploaded.")
parser.add_argument('credentials', type=str, help="Path to the Google Drive API credentials JSON file.")
parser.add_argument('output_name', type=str, help='The name of the outputed file.')
args = parser.parse_args()
main(args.markdown_file, args.drive_folder_link, args.credentials, args.output_name)