-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upload pdf to s3 #38
Closed
Closed
Upload pdf to s3 #38
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b2651f
Handled Exceptions and Added upload_file method to build_pdf_book script
Cannon07 69af3ff
Added test_build_pdf_book script along with mock files
Cannon07 168dd9a
Added boto3 to requirements.txt
Cannon07 996db8d
Added test_build_pdf_book.py to test.yml workflow
Cannon07 e176822
Updated test.yml
Cannon07 5ce9d83
Added build_pdf_book.yml
Cannon07 16e2488
Added repository dispatch trigger in build_pdf_book.yml workflow
Cannon07 1ca4dc0
Added docstrings to test_build_pdf_book scipt
Cannon07 9768e22
Updated build_pdf_book.py docs and Added build_pdf_book.yml workflow …
Cannon07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
- pdfunite | ||
- pdfkit | ||
- mdpdf | ||
- boto3 | ||
|
||
Example Usage: | ||
- Run the script "fetch_tutorials.py" // It will fetch all the tutorials. | ||
|
@@ -23,42 +24,131 @@ | |
import pdfkit | ||
from utils import numeric_sorter | ||
from typing import List | ||
import signal | ||
import logging | ||
import boto3 | ||
from botocore.exceptions import ClientError | ||
|
||
INFO_PATH = "website-render-order/" | ||
DATA_PATH = "html-notebooks/" | ||
PDF_PATH = "storage/" | ||
|
||
INFO_PATH = "/workspaces/deepchem.github.io/new-website/utils/tutorials/website-render-order/" | ||
DATA_PATH = "/workspaces/deepchem.github.io/new-website/utils/tutorials/html-notebooks/" | ||
PDF_PATH = "/workspaces/deepchem.github.io/new-website/utils/tutorials/storage/" | ||
|
||
files = os.listdir(INFO_PATH) | ||
files = sorted(files) | ||
def timeout_handler(signum, frame): | ||
""" | ||
For terminating a function call. | ||
|
||
Raises | ||
------ | ||
Exception | ||
If the function is called. | ||
|
||
files_list = numeric_sorter(files) | ||
""" | ||
raise Exception("Coversion Timed out") | ||
|
||
|
||
def html_to_pdf(): | ||
def html_to_pdf(data_path=DATA_PATH, info_path=INFO_PATH, pdf_path=PDF_PATH): | ||
""" | ||
Converts HTML files to PDF files. | ||
|
||
Parameters | ||
---------- | ||
data_path: str | ||
Path of the html files to be converted. Defaults to DATA_PATH. | ||
info_path: str | ||
Path for Tutorial Render Order. Defaults to INFO_PATH. | ||
pdf_path: str | ||
Path where the converted pdf files will be stored. Defaults to PDF_PATH. | ||
|
||
Raises | ||
------ | ||
ProtocolUnknownError | ||
If it faces some unknown kind of graphic. | ||
IOError | ||
If the file specified in the website-render-order is not present in /html-notebooks. | ||
Exception | ||
If the Conversion takes longer than 60 seconds. | ||
|
||
""" | ||
files = os.listdir(info_path) | ||
files = sorted(files) | ||
files_list = numeric_sorter(files) | ||
|
||
for i in files_list: | ||
chapter = pd.read_csv(INFO_PATH + "-".join(i)) | ||
chapter = pd.read_csv(info_path + "-".join(i)) | ||
for j in chapter["File Name"]: | ||
print(i, j) | ||
pdfkit.from_file(DATA_PATH + j[:-5] + "html", PDF_PATH + j[:-5] + "pdf") | ||
signal.signal(signal.SIGALRM, timeout_handler) | ||
signal.alarm(60) | ||
try: | ||
print(i, j) | ||
pdfkit.from_file(data_path + j[:-5] + "html", pdf_path + j[:-5] + "pdf") | ||
print("Conversion Successful") | ||
except Exception as e: | ||
print("Exception occured: {}".format(e)) | ||
|
||
|
||
def upload_file(file_name, bucket, object_name=None): | ||
""" | ||
Upload a file to an S3 bucket | ||
|
||
Parameters | ||
---------- | ||
file_name: str | ||
Path of the File to be uploaded. | ||
bucket: str | ||
Name of the Bucket to upload the file to. | ||
object_name: str | ||
S3 object name. If not specified then file_name is used. | ||
|
||
Returns | ||
------- | ||
boolean: | ||
True if file was uploaded, else False | ||
|
||
""" | ||
|
||
# If S3 object_name was not specified, use file_name | ||
if object_name is None: | ||
object_name = os.path.basename(file_name) | ||
|
||
# Upload the file | ||
s3_client = boto3.client('s3') | ||
try: | ||
response = s3_client.upload_file(file_name, bucket, object_name) | ||
except ClientError as e: | ||
logging.error(e) | ||
return False | ||
return True | ||
|
||
|
||
def merge_pdf(info_path=INFO_PATH, pdf_path=PDF_PATH): | ||
""" | ||
Merges the compiled PDFs. | ||
|
||
Parameters | ||
---------- | ||
info_path: str | ||
Path for Tutorial Render Order. Defaults to INFO_PATH. | ||
pdf_path: str | ||
Path where the merged pdf file will be stored. Defaults to PDF_PATH. | ||
|
||
""" | ||
files = os.listdir(info_path) | ||
files = sorted(files) | ||
|
||
files_list = numeric_sorter(files) | ||
|
||
def merge_pdf(): | ||
"""Merges the compiled PDFs.""" | ||
command = "pdfunite " | ||
for i in files_list: | ||
chapter = pd.read_csv(INFO_PATH + "-".join(i)) | ||
print(i) | ||
chapter = pd.read_csv(info_path + "-".join(i)) | ||
for j in chapter["File Name"]: | ||
print(i, j) | ||
command = command + PDF_PATH + j[:-5] + "pdf " | ||
os.system(command + "merged.pdf") | ||
file_path = pdf_path + j[:-5] + "pdf" | ||
if (os.path.exists(file_path)): | ||
print(i, j) | ||
command = command + pdf_path + j[:-5] + "pdf " | ||
os.system(command + f"{pdf_path}merged.pdf") | ||
|
||
|
||
def merge_pdf_pages(a: List[str]): | ||
"""Merges the PDFs. | ||
|
@@ -73,7 +163,8 @@ def merge_pdf_pages(a: List[str]): | |
command = "pdfunite " | ||
for i in a: | ||
command = command + i + ' ' | ||
os.system(command + "storage/merged.pdf") | ||
os.system(command + "storage/full_pdf.pdf") | ||
|
||
|
||
def compile_information_pages(): | ||
"""Converts the Acknowledgent page and content page from | ||
|
@@ -85,9 +176,11 @@ def compile_information_pages(): | |
pdfkit.from_file('contents.html', 'storage/contents.pdf') | ||
pdfkit.from_file('acknowledgement.html', 'storage/acknowledgement.pdf') | ||
|
||
|
||
if __name__ == "__main__": | ||
os.system("mkdir " + PDF_PATH) | ||
html_to_pdf() | ||
merge_pdf() | ||
compile_information_pages() | ||
merge_pdf_pages(['storage/title.pdf', 'storage/acknowledgement.pdf', 'storage/contents.pdf', 'storage/full_pdf.pdf']) | ||
merge_pdf_pages(['storage/title.pdf', 'storage/acknowledgement.pdf', 'storage/contents.pdf', 'storage/merged.pdf']) | ||
upload_file('storage/full_pdf.pdf', 'deepchemtutorials', 'TutorialsBook.pdf') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls, keep an empy line at the end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add boto3 to requirements file